CSPIQ · AP Computer Science Principles · Lesson 9 of 25
CSPIQ · AP Computer Science Principles

Lesson 09: Boolean Expressions & Conditionals

Big Idea 3 (AAP) · Phase 3

Objectives

Warm-Up

A theme park's ride rule: riders must be at least 48 inches tall AND at least 8 years old. Four kids at the gate:

Kid Height Age Rides?
P 50 9 ?
Q 50 7 ?
R 45 10 ?
S 45 6 ?

With AND, both conditions must hold: P rides; Q, R, S don't — failing one requirement is failing.

Now the gift shop's discount: customers get 10% off if they have a coupon OR it's their birthday. One is enough; both is also fine.

You already reason with AND/OR flawlessly in daily life. The exam's only trick is making you do it precisely — in code, with NOTs mixed in, three layers deep. Let's build the precision.


Core Concept

Boolean values and relational operators

A Boolean value is true or false — nothing else. Relational operators compare values and produce Booleans:

a = b (equals) · a ≠ b (not equals) · a > b · a < b · a ≥ b · a ≤ b

So 7 ≥ 7 → true (≥ includes equality), 3 ≠ 3 → false. These feed directly into...

Logical operators

a b a AND b a OR b NOT a
T T T T F
T F F T F
F T F T T
F F F F T

Memory hooks: AND is true on exactly one row (both true); OR is false on exactly one row (both false).

Evaluation order: parentheses → relational operators → NOT → AND → OR. When in doubt, the exam almost always parenthesizes for you; when it doesn't, NOT binds tightest: NOT a AND b means (NOT a) AND b.

Evaluating a compound expression

With x ← 10, y ← 3, evaluate NOT (x < 5) AND (y = 3 OR x = 5):

x < 5            → false
NOT false        → true
y = 3            → true
x = 5            → false
true OR false    → true
true AND true    → TRUE

Inside-out, one operator at a time, writing each intermediate value. This is the trace-table discipline (Lesson 7) applied to logic.

IF and IF/ELSE

IF (condition)
{
    <do this when condition is true>
}
ELSE
{
    <do this when condition is false>
}

Rules: exactly one branch runs — never both, never neither (when ELSE exists). Without an ELSE, a false condition means the block is skipped and execution continues after it. Code after the whole IF/ELSE runs regardless.

score ← 85
IF (score ≥ 90)
{
    grade ← "A"
}
ELSE
{
    grade ← "B"
}
DISPLAY (grade)          → B  (85 ≥ 90 is false → ELSE branch)

Equivalent expressions — the exam's favorite disguise

Two Boolean expressions are equivalent if they produce the same value for every input. The equivalences worth knowing cold:

Expression Equivalent Why
NOT (x > 5) x ≤ 5 Flipping > gives ≤ (the boundary switches sides!)
NOT (x = y) x ≠ y Direct opposite
NOT (a AND b) (NOT a) OR (NOT b) De Morgan: NOT distributes, AND↔OR flip
NOT (a OR b) (NOT a) AND (NOT b) De Morgan, other direction

The > flip is the money detail: NOT of "greater than" includes equality. NOT (x > 5) is true when x is exactly 5. Choosing x < 5 as the equivalent is the planted error.

Universal fallback: when unsure whether two expressions are equivalent, test values — especially the boundary value. One input where they differ = not equivalent.

Conditionals with lists and MOD (integration)

The pieces from Lessons 7–8 now combine, exactly as exam items do:

nums ← [12, 7, 20, 3]
IF (nums[2] MOD 2 = 1)
{
    DISPLAY ("odd")
}
ELSE
{
    DISPLAY ("even")
}

nums[2] = 7 (1-indexed!); 7 MOD 2 = 1 → true → displays odd.


Worked Examples

Example 1 (easy): Truth-table drill

Problem: With a ← true, b ← false, evaluate: (i) a AND b (ii) a OR b (iii) NOT b (iv) NOT a OR b

Solution: (i) false (AND needs both). (ii) true (OR needs one). (iii) true. (iv) NOT binds first: (NOT a) OR b = false OR false = false.

Interpretation: (iv) is the precedence check — students who read it as NOT (a OR b) also get false here, but for the wrong reason; the exam will separate those readings with different values.

Example 2 (medium): Trace with conditionals

Problem: What is displayed?

x ← 4
y ← 9
IF (x > 3 AND y MOD 3 = 0)
{
    x ← x + y
}
ELSE
{
    y ← y − x
}
DISPLAY (x + y)

Solution: Condition: 4 > 3 → true; 9 MOD 3 = 0 → true; true AND true → true → run the IF branch: x ← 4 + 9 = 13. (ELSE skipped.) Display 13 + 9 = 22.

Interpretation: The distractor 9 (= 4 + 5) comes from running the ELSE branch too. One branch. Only one.

Example 3 (medium): The NOT-flip

Problem: A program contains IF (NOT (age ≥ 13)). For which ages does the IF branch run?

Solution: NOT (age ≥ 13) is true when age ≥ 13 is false — i.e., when age < 13 (12 and under). Note the flip: NOT of ≥ is <, equality jumping to the other side. Age exactly 13 → 13 ≥ 13 true → NOT false → branch skipped.

Interpretation: Test the boundary (13): it's the entire question. NOT(≥) = <, NOT(>) = ≤, NOT(<) = ≥, NOT(≤) = >.

Example 4 (AP-style): Equivalence hunt

Problem: Which is equivalent to NOT (raining AND cold)?

(A) NOT raining AND NOT cold (B) NOT raining OR NOT cold (C) raining OR cold (D) NOT raining AND cold

Solution: (B) — De Morgan: NOT over AND becomes OR of NOTs. Verify with raining = true, cold = false: original = NOT(true AND false) = NOT false = true. (A): false AND true = false ✗. (B): false OR true = true ✓. One test case eliminated (A); check another case if two choices survive.

Interpretation: "Not both" (the original) means "at least one is missing" (B). (A) says "neither" — stronger, different. English intuition backs up the algebra here; use both.


Common Mistakes

  1. Exclusive-OR intuition. Boolean OR is true when both sides are true. "Coupon OR birthday" with both = still discounted.
  2. The NOT-flip landing on the wrong comparison. NOT (x > 5)x ≤ 5, NOT x < 5. The boundary value (x = 5) always exposes it — test it.
  3. Running both branches. IF/ELSE executes exactly one branch. Trace tables should show one branch's effects only.
  4. Forgetting no-ELSE means skip. A false condition with no ELSE = nothing happens; execution continues below. The variable keeps its old value — a favorite trace question.
  5. De Morgan half-applied. Flipping AND→OR but forgetting to NOT the operands (or vice versa). NOT(a AND b) = (NOT a) OR (NOT b): flip the operator AND negate both sides.

Practice Problems

Question 1
With p ← false, q ← true, what is p OR NOT q?
Question 2
Which expression is true when n = 10?
Question 3

What is displayed?

temp ← 65
IF (temp ≥ 70)
{
    DISPLAY ("warm")
}
DISPLAY ("done")
Question 4
Which is equivalent to NOT (score < 60)?
Question 5
A ride requires: height at least 48 inches AND (age at least 8 OR riding with an adult). Who may ride?
Question 6
Which is equivalent to NOT (a OR b)?
Question 7

What is displayed?

L ← [5, 12, 8]
IF (L[1] > L[3])
{
    DISPLAY (L[2])
}
ELSE
{
    DISPLAY (L[3])
}
Question 8
For which values of x are x > 4 AND x < 10 and x ≥ 5 AND x ≤ 9 equivalent — assuming x is an integer?
Question 9
With a ← 6, b ← 2, evaluate NOT (a MOD b = 0) OR a < b:
Question 10
(Select two answers.) Which expressions are true for EVERY pair of Boolean values a, b?
Question 11
A program should display "valid" when a password's length is between 8 and 16, inclusive. Which condition is correct?
Question 12

What is displayed?

x ← 3
IF (x MOD 2 = 0)
{
    x ← x * 10
}
ELSE
{
    x ← x + 10
}
IF (x > 12)
{
    x ← x − 1
}
DISPLAY (x)

Create PT Connection

Selection is a hard requirement of your Create PT procedure — the rubric demands a student-developed procedure containing sequencing, selection (an IF), and iteration. This lesson is the selection half.

Design guidance: the strongest PT selections decide something meaningful about the data — IF (score > highest), IF (item = searchTerm), IF (LENGTH(entry) = 0) — not decorative conditions bolted on to satisfy the rubric. Scorers (and Written Response 2(a), Algorithm Development) reward conditions you can explain the purpose of in one sentence.

Mini practice (WR 2(a) style): "Explain how the selection statement in your procedure affects your program's behavior." Model, for a high-score finder: My procedure compares each score in the list to the variable highest; the IF statement IF (scores[i] > highest) updates highest only when the current score exceeds it, so after the loop, highest holds the maximum — without the selection, every score would overwrite highest and the program would just report the last score.


Show answer key & explanations

(g) Answer Key

1. (B). NOT first: NOT q = false. Then false OR false = false. (C): Booleans aren't numbers on this exam.

2. (C). 10 ≥ 10 true (inclusive) AND 10 MOD 2 = 0 true → true. (A) strict > fails at equality. (B) = "n > 10" in disguise (NOT-flip) — false. (D): both halves false.

3. (B). 65 ≥ 70 false, no ELSE → skip the block; the DISPLAY after the IF runs regardless → done. (D) forgets post-IF code runs; (A) runs a skipped branch.

4. (B). NOT of < is ≥ — equality flips sides. (A) > 60 wrongly excludes exactly-60: test score = 60 → original: NOT(false) = true; (A): false. Differ → not equivalent.

5. (C). Check each against height ≥ 48 AND (age ≥ 8 OR withAdult). (C): 48 ✓ ("at least 48" is inclusive) AND (age 8 ✓) → rides. (A): height 50 ✓ but age 6 ✗ and alone ✗ → the OR fails. (B): height 45 fails the AND's first requirement — the adult can't rescue a failed height check. (D): 47 fails height. Two traps in one problem: inclusive boundaries and AND's veto power.

6. (B). De Morgan: NOT(a OR b) = NOT a AND NOT b — "neither one." (A) flips the operator but is the equivalent of NOT(a AND b) — the other De Morgan.

7. (C). L[1] = 5, L[3] = 8; 5 > 8 false → ELSE → display L[3] = 8. (B) is the IF branch; (A) misindexes; (D) invents concatenation.

8. (A). For integers, x > 4x ≥ 5 and x < 10x ≤ 9 — the ranges coincide exactly (5..9 both ways). (D) is a red herring: the question stipulates integers. This "integer-equivalence" move appears on real forms.

9. (B). 6 MOD 2 = 0 → true; NOT true → false. 6 < 2 → false. false OR false = false.

10. (A) and (C). (A): a or its negation — always true (one of them must be). (C): either both are true (first clause) or at least one is false (covered by a NOT clause) — every case lands somewhere; test all four rows if unconvinced. (B) is always FALSE; (D) fails when both false.

11. (A). Inclusive bounds need ≥ and ≤ joined by AND. (B) excludes 8 and 16. (C): OR makes it always true (every number satisfies at least one side). (D): tempting De Morgan bait, but NOT(len<8 AND len>16) — the inner AND is never true (can't be both), so the NOT is always true. The correct De Morgan form would be NOT (len < 8 OR len > 16).

12. (C). First IF: 3 MOD 2 = 1 ≠ 0 → ELSE → x = 13. Second IF: 13 > 12 → x = 12. Display 12. (B) stops after the first conditional — both conditionals run in sequence; they're separate statements, not IF/ELSE partners.

Answer letter distribution check: B, C, B, B, C, B, C, A, B, A+C, A, C — singles: A×2, B×5, C×5, D×0 + multi (A,C). D absent this lesson (flagged); L10's robot answers will carry D weight. Note: problem 5's key discussion shows the verification process concluding (C) — key letter is (C).


Exam tip: For any NOT-heavy expression, don't philosophize — build a two-second truth check: pick the boundary value or one concrete true/false pair, evaluate both expressions, compare. On equivalence questions, a single well-chosen test value (usually the boundary) eliminates two or three choices instantly.

← All lessons
Lesson 10 ›
Score: 0/0 correct