Unit 2 Cycle 1 Day 7: Compound Boolean with AND/OR
Share
Compound Boolean with AND/OR
Section 2.5 — Compound Boolean
Key Concept
Compound boolean expressions use logical AND (&&) and OR (||) to combine multiple conditions. With &&, both conditions must be true for the overall expression to be true. With ||, at least one must be true. Operator precedence matters: ! has highest precedence, then &&, then ||. The expression a || b && c evaluates as a || (b && c), not (a || b) && c. Use parentheses to make intent clear.
Consider the following code segment.
What is printed as a result of executing the code segment?
Answer: (B) eligible
age >= 16 && hasLicense: true && false = false. age >= 16 || hasLicense: true || false = true. Prints "eligible".
Why Not the Others?
(A) AND requires BOTH conditions to be true. hasLicense is false, so the AND fails.
(C) The OR condition is true because at least one operand (age >= 16) is true.
(D) This is an else-if chain, so only one branch executes.
Common Mistake
&& (AND) requires both sides to be true. || (OR) requires at least one side to be true. A common error is confusing which operator requires both vs. either.
AP Exam Tip
Evaluate each side of && and || independently first. Then apply: AND = both true; OR = at least one true.