Unit 2.5
Compound Boolean Expressions
AP Computer Science A
Learning to combine Boolean expressions using logical operators
Learning Objectives:
2.5.A: Develop code to represent compound Boolean expressions and determine the result of these expressions.
Logical Operators
Logical operators allow us to combine simple Boolean expressions into more complex ones.
NOT
Reverses the Boolean value
AND
True when both conditions are true
OR
True when at least one condition is true
Essential Knowledge 2.5.A.1:
The order of precedence for evaluating logical operators is:
! (not), then && (and), then || (or)
The NOT Operator (!)
The NOT operator reverses a Boolean value. If the expression is true, ! makes it false, and vice versa.
Syntax:
Examples:
Truth Table:
| a | !a |
|---|---|
| true | false |
| false | true |
The AND Operator (&&)
The AND operator returns true only when both expressions are true.
Syntax:
Examples:
Truth Table:
| a | b | a && b |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
The OR Operator (||)
The OR operator returns true when at least one of the expressions is true.
Syntax:
Examples:
Truth Table:
| a | b | a || b |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
Order of Operations
When multiple logical operators are used, Java follows a specific order of precedence:
Precedence (highest to lowest):
- ! (NOT) - evaluated first
- && (AND) - evaluated second
- || (OR) - evaluated last
Examples:
Short-Circuit Evaluation
Essential Knowledge 2.5.A.2: Short-circuit evaluation occurs when the result of a logical operation can be determined by evaluating only the first Boolean expression.
AND (&&) Short-Circuit:
OR (||) Short-Circuit:
Practical Example:
Why is this important?
- Prevents errors (like NullPointerException)
- Improves performance by avoiding unnecessary operations
- Allows for safe conditional checking
Interactive Demo
Try different combinations and see the results:
Try These Combinations:
- A = true, B = false: What does A && B return?
- A = false, B = true: What does A || B return?
- A = true, B = false: What does !A && B || A return?
Common Patterns
Range Checking:
Multiple Conditions:
Exclusion Pattern:
Safe Navigation:
Best Practices:
- Use parentheses for clarity
- Put simpler conditions first for short-circuiting
- Check for null before calling methods
Practice Problems
Problem 1:
What is the result of each expression?
Problem 2:
Write a compound Boolean expression for each condition:
- A student passes if their score is 60 or above AND they attended at least 80% of classes
- A movie is suitable if it's rated G OR it's rated PG and the viewer is 10 or older
- A password is strong if it's at least 8 characters AND contains both letters and numbers
Unit 2.5 Review
Key Concepts:
- ! (NOT): Reverses Boolean value
- && (AND): True only when both expressions are true
- || (OR): True when at least one expression is true
- Precedence: ! then && then ||
- Short-circuit evaluation: Second expression may not be evaluated