First exams 2027 · SL & HL

B2.3.2 · Programming constructs

Selection: letting the program choose

A ride operator doesn't run the same script for every visitor. Selection is what lets a program do the same thing: check a condition, then choose which instructions actually run.

Core for SL & HL Typically 2–5 marks · AO3 Command term: construct

Explainer

Before letting anyone onto Thornbury Theme Park's new drop tower, the ride operator checks a short list of rules. Meet them and the gate opens. Miss even one, and it doesn't matter how much you want to ride, you're turned away.

Df

Selection (or branching) is the construct that picks which instructions actually run, by testing one or more conditions first. Sequence (B2.3.1) always runs every line, no exceptions. Selection is the first construct that lets the program genuinely choose.

=

Every selection structure is built from a condition, and a condition is written with a relational operator: == equal to, != not equal to, < less than, > greater than, <= less than or equal to, >= greater than or equal to.

A single = and a double == mean two completely different things. = assigns a value to a variable. == compares two values and produces True or False. Writing age = 12 inside a condition doesn't ask a question, it overwrites age with 12.

Explainer

IF on its own, IF/ELSE, and chained ELSE IF

An IF on its own only ever adds a branch, it never removes the default: if the condition is false, nothing in that block runs, and the program simply continues.

StructureWhat happensPython keywordJava keyword
IF aloneRun the block only if the condition is true, otherwise do nothingifif
IF / ELSERun one block if true, a different block if false, exactly one of the two always runsif / elseif / else
Chained ELSE IFTest conditions in order, run the block under the first one that's true, skip the restelifelse if

Python and Java handle a chain of alternatives the same way, they just spell it differently: Python has a single keyword, elif, while Java writes it as two words, else if.

!

A chain of elif / else if tests each condition in order and stops at the first one that's true. Once one branch runs, every later condition in the same chain is skipped, even if it would also have been true.

Explainer

Combining conditions: AND, OR, NOT

A single relational operator can only ask one question. Real rules usually need more than one question answered at once, which is what the three Boolean operators are for.

&

AND is true only when both conditions are true. If either one is false, the whole thing is false.

|

OR is true when at least one condition is true. It's only false when both conditions are false.

!

NOT flips a single condition: true becomes false, and false becomes true.

AND is evaluated before OR

When a condition mixes AND and OR with no brackets, AND is always worked out first, exactly like multiplication is worked out before addition in arithmetic. a or b and c means a or (b and c), never (a or b) and c. The worked example below shows a case where getting this backwards genuinely changes the answer.

Worked example

Who can ride the drop tower?

Thornbury Theme Park's rule: a visitor may ride if they hold a Fast Pass, or they are 12 or older and riding with an adult.

The same rule, checked for three different visitors Step 1 of 3

Step 1. Priya, age 10, no Fast Pass, but with an adult. She's under 12, so the AND on the right fails no matter what the adult flag says.

has_fast_pass = False age = 10 with_adult = True eligible = has_fast_pass or (age >= 12 and with_adult) print("Eligible:", eligible)
Eligible: False

Step 2. Sam, age 14, no Fast Pass, no adult. Sam is old enough, but the AND still needs both sides true, and with_adult is False.

has_fast_pass = False age = 14 with_adult = False eligible = has_fast_pass or (age >= 12 and with_adult) print("Eligible:", eligible)
Eligible: False

Step 3. Jordan, age 10, holds a Fast Pass, no adult. The age/adult AND is still false, but the Fast Pass alone satisfies the OR. Jordan rides.

has_fast_pass = True age = 10 with_adult = False eligible = has_fast_pass or (age >= 12 and with_adult) print("Eligible:", eligible)
Eligible: True

Try it yourself

Run the eligibility check

Change the three values at the top and press Run to check a different visitor. Try setting has_fast_pass and age both to values that would make the rule fail, then flip just one back.

main.py
Loading Python engine…
Output
Press Run to see the output here.

Running in offline fallback mode. The full Python engine couldn't load (blocked school network, or no internet), so this console switched automatically to a small hand-written engine. It supports this page's conditions and print statements, but it isn't full Python.

Worked example

One rule, two ways to write it

A wristband check comes first, before anything else: NOT wearing a valid wristband means no entry, whatever the Fast Pass or age says. After that gate, the same eligibility rule from above can be written as one AND/OR condition, or spelled out as nested IF statements. Both produce identical results, one is just more compact.

1

Spelled out as nested IF statements:

if not has_wristband:
    eligible = False
else:
    if has_fast_pass:
        eligible = True
    else:
        if age >= 12 and with_adult:
            eligible = True
        else:
            eligible = False
2

Written as one condition, meaning exactly the same thing:

eligible = has_wristband and (has_fast_pass or (age >= 12 and with_adult))

Every nested branch above corresponds to one part of the single condition: the outer if not has_wristband becomes the leading has_wristband and (...), and the inner Fast-Pass-or-age-and-adult branches become the OR/AND inside the brackets. Neither version is "more correct," the nested version can be easier to read while a rule is still being worked out, the single condition is shorter once it's settled.

Check your understanding

Jordan has a Fast Pass (has_fast_pass = True), is 10 years old, and has no adult with them (with_adult = False). The rule is written as: eligible = has_fast_pass or age >= 12 and with_adult. What does this evaluate to?

Common mistake

Markers reported this pattern on Boolean-expression questions

RecurringWhen asked to evaluate a mixed AND/OR expression by hand, a common wrong answer comes from evaluating strictly left to right, treating OR as if it happens first, instead of applying the real rule that AND binds tighter than OR.

Answer frame that scores: work out every AND first, treating each x and y as a single True/False value, then work out the remaining ORs across whatever is left.

✕

Left-to-right, OR first: has_fast_pass or age >= 12 is worked out as one step first (True or False = True), then True and with_adult (True and False = False). Gives False, the wrong answer for Jordan.

✓

AND first, as the language actually evaluates it: age >= 12 and with_adult is worked out first (False and False = False), then has_fast_pass or False (True or False = True). Gives True, matching what the console above actually prints.

Revision questions

Past-paper style. Write an answer, then reveal the marking points and mark yourself.

Question 1 · 2 marks

[2]Explain the difference between = and == in a program.

Question 2 · 3 marks

[3]For has_fast_pass = False, age = 20 and with_adult = False, explain why has_fast_pass or age >= 12 and with_adult evaluates to False even though age >= 12 is True.

Question 3 · 4 marks

[4]Rewrite the nested IF statements below as a single condition using AND/OR: if a student's grade is at least 90, award a distinction; otherwise, if the grade is at least 70 and their attendance is above 80%, award a merit; otherwise, award nothing.

Flashcards

Active recall for this statement’s key terms.