Skip to main content

Pilot · L03 · 50 minutes

Decisions and Boolean reasoning

Evaluate compound Boolean conditions, trace IF, IF ELSE and nested conditionals, and test a club eligibility checker at its boundaries to expose an OR that should be AND.

Topics: 3.5 Boolean Expressions · 3.6 Conditionals · 3.7 Nested Conditionals

Goals and vocabulary

By the end you can:

  • Translate a written rule into a correct relational and Boolean condition.
  • Evaluate AND, OR and NOT, and find an input where AND and OR give different results.
  • Trace which branch runs for a given input, including at a boundary value.
  • Explain why two independent IF statements can both run while IF ELSE runs exactly one block.
  • Trace a nested conditional by following only the branches actually taken.
Boolean value
A value that is either true or false.
Relational operator
An operator that compares two values and produces a Boolean, such as ≥ or ≠.
Logical operator
AND, OR or NOT, which combine or flip Boolean values.
Conditional statement
A statement that runs a block only when a condition is true (IF), optionally with an alternative (ELSE).
Boundary value
An input exactly at, or just either side of, the point where a rule changes its answer.
Nested conditional
A conditional statement placed inside a branch of another conditional.

Boolean values and relational operators

A Boolean value is either true or false. Relational operators compare two values and produce a Boolean: =, ≠, >, <, ≥ and ≤ in AP pseudocode (==, !=, >, <, >=, <= in Python).

Our running example is a club eligibility checker. The rule from the club is: a student may join if they are at least 14 and the club has more than 0 places. “At least 14” means age ≥ 14, so 14 itself is allowed. “More than 0” means places > 0, so 0 is not.

AND, OR and NOT

a AND b is true only when both are true. a OR b is true when at least one is true (including when both are). NOT a flips a single value.

Truth table for the eligibility rule, old = (age ≥ 14), room = (places > 0)
oldroomold AND roomold OR roomNOT (old AND room)
truetruetruetruefalse
truefalsefalsetruetrue
falsetruefalsetruetrue
falsefalsefalsefalsetrue

AND and OR agree on the first and last rows and disagree on the middle two. Those middle rows are exactly the inputs that reveal which operator a program uses.

NOT (old AND room) means “not both”. It is the same as (NOT old) OR (NOT room). It is not the same as (NOT old) AND (NOT room), which means “neither”.

IF and IF ELSE

AP CSP pseudocode
IF ((age ≥ 14) AND (places > 0)){  DISPLAY("You can join")}ELSE{  DISPLAY("Not eligible")}

The condition is evaluated once. If it is true, only the first block runs; otherwise only the ELSE block runs. Exactly one of the two messages is displayed.

Two separate IF statements are different: each condition is checked on its own, so both blocks can run, or neither.

Independent IFs compared with IF ELSE for score 85
StructureCodeDisplays
Two independent IFsIF (score ≥ 50) { DISPLAY("Pass") } IF (score ≥ 80) { DISPLAY("Merit") }Pass Merit
IF ELSEIF (score ≥ 80) { DISPLAY("Merit") } ELSE { DISPLAY("Pass") }Merit

Worked example: testing the checker at the boundary

Worked example

A student wrote this checker. Predict what it displays for a 14-year-old when the club has 1 place.

Buggy checker
IF ((age > 14) OR (places > 0)){  DISPLAY("You can join")}ELSE{  DISPLAY("Not eligible")}
Tests chosen on and either side of each boundary
age, placesRule saysBuggy checkerWhy
14, 1joinjoinage > 14 is false, but places > 0 is true and OR needs only one.
13, 2not eligiblejoinOR lets a too-young student in. This test exposes the OR bug.
14, 0not eligiblenot eligibleBoth parts false. Agrees by luck.
15, 0not eligiblejoinAlso exposes OR.

Fix 1: replace OR with AND. Re-test (14, 1): now age > 14 is false, so the fixed-OR version wrongly says not eligible. Fix 2: replace > with ≥ for age. The repaired condition is (age ≥ 14) AND (places > 0), and it gives the rule’s answer for all four tests.

Nested conditionals

A conditional can sit inside another. The inner condition is only checked when the outer branch containing it runs.

AP CSP pseudocode
IF (member){  IF (places > 0)  {    DISPLAY("Booked")  }  ELSE  {    DISPLAY("Waitlist")  }}ELSE{  DISPLAY("Join the club first")}

For a non-member, the places check never happens and “Join the club first” is displayed, even if places is 10. To trace a nested conditional, follow the outer condition first, then only the inner block on the path you took.

Explore each concept

Choose a concept, change its inputs and follow the explanation. Each example models the stated rule or scenario; completing it is practice rather than evidence of mastery.

  • All six relational operators
  • AND, OR and NOT
  • De Morgan equivalence and assignment
Open this example on its own

Change it · step through · explain

Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.

Ready · Make a prediction

Ready to explore: Relations, truth tables and NOT
  1. a (0–100)14Waiting
  2. b (0–100)14Waiting
  3. ExplorerelationsWaiting

Ready to explore: Relations, truth tables and NOT

Why: Change an input, predict the first effect, then advance one step.

Try this: Change one input or choose another listed case. Predict the effect, reset and compare the states and governing rule.

Step through and experiment

Change it · step through · explain

Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.

Ready · Make a prediction

Ready to experiment
  1. Input 114Waiting
  2. Input 20Waiting

Ready to experiment

Why: Predict the first change, then advance one step.

Try this: Compare ages 13 and 14 with places 0 and 1.

Step through and experiment

Change it · step through · explain

Change an input, predict what will happen, then advance one step. Read why the state changes, reset and experiment. This fixed teaching model does not save progress or award XP.

Ready · Make a prediction

Ready to experiment
  1. Input 1yesWaiting
  2. Input 2noWaiting

Ready to experiment

Why: Predict the first change, then advance one step.

Try this: Try all four yes/no combinations. These fictional labels are not accessibility guarantees.

Common misconceptions

  • AND and OR are interchangeable

    The belief: Both join two conditions, so either works.

    What is actually true: They differ exactly when one part is true and the other false. Test those inputs to see which one the program uses.

    Check yourself: Can you give an input where your condition is true with OR but false with AND?

  • Only one IF can run

    The belief: Two IF statements in a row work like IF ELSE.

    What is actually true: Independent IF statements each check their own condition, so both can run. Only IF ELSE guarantees exactly one block.

    Check yourself: Pick an input that makes both conditions true. What is displayed?

  • One passing test proves the condition

    The belief: It worked for my test, so the checker is correct.

    What is actually true: A wrong condition often passes some tests. Test on and either side of each boundary and where the parts disagree.

    Check yourself: Which of your tests would fail if ≥ were changed to >?

Guided practice

Guided: complete the truth table and test the checker

With a partner, complete a truth table for the repaired rule, then test the buggy checker from the worked example with your own inputs.

Repaired condition
(age ≥ 14) AND (places > 0)
  1. Write the four rows of the truth table for (age ≥ 14) and (places > 0) and the AND result.
  2. Choose one concrete (age, places) pair for each row, using boundary values where you can.
  3. Run each pair through the buggy checker and the repaired condition by hand and record both results.
  4. Circle the pairs where the two disagree and explain which bug each one exposes.

Independent practice

Independent: two structures that disagree

A library shows messages about overdue books. Compare the two versions.

Version 1
IF (days > 7){  DISPLAY("Overdue")}IF (days > 28){  DISPLAY("Fine due")}
Version 2
IF (days > 28){  DISPLAY("Fine due")}ELSE{  IF (days > 7)  {    DISPLAY("Overdue")  }}
  1. Trace both versions for days = 3, 7, 8, 28 and 30.
  2. Give an input for which the two versions display different things, and state both outputs.
  3. Explain the difference using the words “independent” and “nested”.
  4. The library wants “Overdue” and “Fine due” both shown after 28 days. Which version does that?

Exit check

  1. Give an input (age, places) for which (age ≥ 14) OR (places > 0) and (age ≥ 14) AND (places > 0) give different results.
  2. Write a condition for “at most 30 students and at least 1 teacher”, using students and teachers.

Check yourself

Practice only: answers are checked in your browser and nothing is saved or sent to your teacher. Each explanation links back to the section it practises.

Lesson practice

  1. Question 1 of 4Condition A is (age ≥ 14) AND (places > 0). Condition B is (age ≥ 14) OR (places > 0). For which input do A and B give different results?

    Select one answer.

  2. Question 2 of 4Which two expressions always have the same value as NOT (raining AND cold)? Select two.

    Select exactly two answers (0 of 2 selected).

  3. Question 3 of 4What does this program display?

    Select one answer.

    AP CSP pseudocode
    age ← 15places ← 4IF ((age ≥ 14) AND (places > 0)){  DISPLAY("Join")}ELSE{  DISPLAY("Waitlist")}
  4. Question 4 of 4A club accepts students who are at least 14. What does this program display for a student who is exactly 14?

    Select one answer.

    AP CSP pseudocode
    age ← 14IF (age > 14){  DISPLAY("Eligible")}ELSE{  DISPLAY("Not eligible")}

Your practice so far

Check an answer to see its rationale.

Apply it somewhere new

  1. Question 1 of 2Program A uses two independent IF statements. Program B uses IF ELSE. For which score do they display different things?

    Select one answer.

    AP CSP pseudocode
    Program AIF (score ≥ 50) { DISPLAY("Pass") }IF (score ≥ 80) { DISPLAY("Merit") }Program BIF (score ≥ 50) { DISPLAY("Pass") }ELSE{  IF (score ≥ 80) { DISPLAY("Merit") }}
  2. Question 2 of 2What does this program display?

    Select one answer.

    AP CSP pseudocode
    member ← trueplaces ← 0IF (member){  IF (places > 0)  {    DISPLAY("Booked")  }  ELSE  {    DISPLAY("Waitlist")  }}ELSE{  DISPLAY("Join first")}

Your practice so far

Check an answer to see its rationale.