BIG IDEA 3: ALGORITHMS AND PROGRAMMING · TOPIC 3.5
3.5 Boolean Expressions
Every conditional and loop depends on a Boolean expression. You have to evaluate them exactly, including the ones designed to be misread.
What you need to know
- A Boolean value is either
trueorfalse. A Boolean expression evaluates to one of those two. - Relational operators compare values:
=,≠,>,<,≥,≤. Note: in AP pseudocode=is comparison, not assignment (assignment is←). - Logical operators:
NOT ais true when a is false.a AND bis true only when both are true.a OR bis true when at least one is true (including both). - Precedence:
NOTis evaluated beforeAND, which is beforeOR. Use parentheses when in doubt — the exam does. - De Morgan's laws show up as "equivalent expression" questions:
NOT (a AND b)=(NOT a) OR (NOT b), andNOT (a OR b)=(NOT a) AND (NOT b). - Two Boolean expressions are equivalent if they have the same value for every combination of inputs. A truth table proves it.
- The negation of
x > 5isx ≤ 5, notx < 5. Missing the boundary case is a classic distractor.
Worked example
Let age ← 16 and hasLicense ← false.
age ≥ 16 AND hasLicense→ true AND false → falseage ≥ 16 OR hasLicense→ true OR false → trueNOT (age < 18)→ NOT true → falseNOT (age ≥ 16 AND hasLicense)→ NOT false → true, which by De Morgan equalsage < 16 OR NOT hasLicense→ false OR true → true. ✓
Exam tip: For "which expression is equivalent" questions, don't reason abstractly — plug in all four combinations of true/false (or a few boundary numbers) and compare outputs. It takes 30 seconds and is essentially error-proof.
Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- Truth tables are the reliable tool. For two Booleans a and b there are four rows: TT, TF, FT, FF. Evaluate both expressions on each row; if all four match, they're equivalent. For three Booleans, eight rows. It's mechanical and it works.
- Short-circuit evaluation isn't formally in the CSP CED, but understanding that
false AND anythingis false andtrue OR anythingis true speeds up evaluation and helps you see equivalences. - De Morgan's laws in practice: to negate "x is between 1 and 10" — which is
x ≥ 1 AND x ≤ 10— you getx < 1 OR x > 10. Flip AND to OR, flip each comparison to its opposite (including the boundary). - The negation of a comparison flips to the opposite operator, which always includes the boundary the original excluded: NOT(>) is ≤, NOT(≥) is <, NOT(=) is ≠. Students who write NOT(x > 5) as x < 5 miss the case x = 5.
- Relational operators return a Boolean, so
x > 5by itself is a complete Boolean expression that can be assigned:isBig ← x > 5. ThenIF (isBig)works without writingIF (isBig = true). - Precedence: relational operators evaluate first, then NOT, then AND, then OR. So
NOT a AND bis(NOT a) AND b. The exam uses parentheses generously, but knows students misgroup when it doesn't.
Mistakes that cost points
- Dropping the boundary when negating. NOT(score ≥ 90) is score < 90, not score ≤ 90 or score < 89.
- De Morgan half-done. NOT(a AND b) becomes (NOT a) OR (NOT b). Students negate the parts but forget to flip AND to OR (giving NOT a AND NOT b, which is wrong).
- Treating OR as exclusive.
a OR bis true when both are true, too. It's inclusive or. - Grouping NOT loosely.
NOT a OR bis(NOT a) OR b. NOT applies only to the thing right after it unless parentheses say otherwise.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 Which of the following expressions is equivalent to
NOT (x > 10 AND y = 3)?Show answer
Answer: B. De Morgan: NOT (A AND B) = (NOT A) OR (NOT B). NOT (x > 10) is x ≤ 10 (include the boundary), NOT (y = 3) is y ≠ 3.
Q2 The variables
a and b are Boolean. For which values is the expression (a OR b) AND (NOT a) true?Show answer
Answer: C. NOT a requires a = false. Then (a OR b) requires b = true. Only option C satisfies both.
Key vocabulary
- Boolean
- a value that is either true or false
- Relational operator
- an operator that compares two values, such as = or <
- Logical operator
- NOT, AND, or OR — combines or negates Boolean values
- Equivalent expressions
- expressions that evaluate to the same value for every possible input