###### Bookmarks

Subjects and resources that you bookmark will appear here.

Light Mode

The AP Computer Science A 5-hour live stream review is here!Β π»

Join us on May 5, 2021 for the πΆοΈ AP Computer Science A Cram Finale for a last minute review to get all your questions answered!

π

All Subjects

Β >Β

π»Β

AP Comp Sci A

Β >Β

π₯

Unit 3

# 3.5 Compound Boolean Expressions

Peter Cao

Caroline Koffke

## 3.5: Compound Boolean Expressions

### Nested Conditionals

At times, we have an if statement, and then inside that if statement, we have multiple conditions to check, assuming the first condition in the outermost if statement is true. This is where nested conditionals help. This is as simple as placing a conditional statement inside another conditional statement. Here is how we can do this with the leap year code:
```public static boolean isLeap(int year) { if (year % 100 == 0) { if (year % 400 == 0) { return true; } return false; } else if (year % 4 == 0) { return true; } return false; }```

### Boolean Logical Operators

Before we return to the code above, we need to talk about boolean logical operators. These makeup long compound boolean statements and are used to evaluate combinations of smaller boolean statements. There are three which we will learn in this course:
• ! (NOT) negates whatever is in front of it
• && (AND) returns true if BOTH of the statements directly preceding or following it are true
• || (OR) returns true if at least one of the statements directly preceding or following it is true
There is an order of operations to these operators if there are multiple in one expression. The NOT operator has the highest precedence, followed by the AND operator and finally the OR operator.
Using these operators, we can combine multiple conditionals to clean up our code. However, this can lead to some complex boolean expressions... we'll learn how to simplify them in the next topic!
`twoB || !twoB, ``that is the question - Shakespeare`

### Compound Conditional Statements

Using logical boolean operators, we can make compound conditional statements such as:
`returns (a % 2 == 0) && (a % 3 == 0)`
This returns whether a number is divisible by both 2 and 3. Compound conditional statements can eliminate the use of nested conditional statements. Here is the leap year method without nested conditionals:
```public static boolean isLeap(int year) { if (year % 400 == 0 || year % 4 == 0 && 100 != 0) { return true; } return false; }```

##### πͺπ½ Are you ready for the Comp Sci exam?
Take this quiz for a progress check on what youβve learned this year and get a personalized study plan to grab that 5!
START QUIZ
Studying with Hours = the ultimate focus mode
Start a free study session
##### FREE AP comp sci a Survival Pack + Cram Chart PDF
Browse Study Guides By Unit
π
Exam Reviews
π±
Unit 10: Recursion
β
Unit 1: Primitive Types
π±
Unit 2: Using Objects
πΉ
Unit 4: Iteration
βοΈ
Unit 5: Writing Classes
βοΈ
Unit 6: Array
πΎ
Unit 7: ArrayList
π»
Unit 8: 2D Array
π²
Unit 9: Inheritance