AP Computer Science A Unit 3: Boolean Expressions & if Statements
Study boolean logic, conditionals, nested ifs, De Morgan's law with exam-format practice and rubric-based scoring.
Start AP CSA Practice · Full Study Guide
AP and Advanced Placement are trademarks of College Board. AimFive is not affiliated with or endorsed by College Board.
Inside This Unit: The Full Breakdown
Boolean expressions evaluate to true or false and control program flow through if, else if, and else statements. This unit covers comparison operators, logical operators, and nested conditionals.
Why it matters
Conditional logic drives every non-trivial program. The AP CSA exam tests your ability to trace through complex nested conditionals, understand short-circuit evaluation, and identify equivalent boolean expressions using De Morgan's laws.
Key concepts
- Comparison operators (==, !=, <, >, <=, >=) compare primitive values and return boolean results.
- Logical operators && (and), || (or), and ! (not) combine boolean expressions.
- Short-circuit evaluation: && stops if the left side is false; || stops if the left side is true.
- De Morgan's laws: !(A && B) equals !A || !B, and !(A || B) equals !A && !B.
Boolean Expressions and Comparisons
A boolean expression evaluates to true or false. Comparison operators compare two values: == checks equality, != checks inequality, and <, >, <=, >= compare magnitude. For objects like Strings, use .equals() instead of == for content comparison. Boolean variables can store the result of a comparison: boolean isAdult = age >= 18. Complex conditions combine comparisons with && (both must be true), || (at least one must be true), and ! (negates the value).
If-Else Statements
An if statement executes its body only when the condition is true. An if-else provides an alternative path when the condition is false. An if-else-if chain tests multiple conditions in sequence — only the first true condition executes. Once a branch executes, the remaining conditions are skipped. Nested if statements place one conditional inside another. The AP exam tests tracing through these structures with specific values and identifying which branches execute.
Compound Boolean Expressions
Complex conditions use && and || to combine simpler boolean expressions. Java uses short-circuit evaluation: in A && B, if A is false, B is never evaluated because the result must be false; in A || B, if A is true, B is never evaluated. This matters when B has side effects or could cause an error. De Morgan's laws describe how negation distributes over && and ||. These laws are directly tested on the AP exam, often asking which expressions are equivalent.
AP exam tip
When tracing conditionals on the AP exam, evaluate each condition completely before deciding which branch executes. Watch for else-if chains where only the first true condition runs.
Connections to other units
- Unit 4: Loop conditions are boolean expressions that determine when iteration continues or stops.
- Unit 5: Methods often use conditionals to validate input or branch on object state.
- Unit 9: The instanceof operator returns a boolean used in polymorphic conditionals.