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.
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.
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.
| Structure | What happens | Python keyword | Java keyword |
|---|---|---|---|
| IF alone | Run the block only if the condition is true, otherwise do nothing | if | if |
| IF / ELSE | Run one block if true, a different block if false, exactly one of the two always runs | if / else | if / else |
| Chained ELSE IF | Test conditions in order, run the block under the first one that's true, skip the rest | elif | else 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.
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.
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.
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.
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.
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.
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.
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
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
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.
[2]Explain the difference between = and == in a program.
Marking points
评分要点
- A single = is assignment: it stores the value on the right into the variable on the left.
- A double == is a comparison: it tests whether two values are equal and evaluates to true or false.
- 单个 = 是赋值:它把右边的值存入左边的变量。
- 双 == 是比较:它检验两个值是否相等,求值为真或假。
[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.
Marking points
评分要点
- and is evaluated before or, so the expression is has_fast_pass or (age >= 12 and with_adult).
- age >= 12 and with_adult is True and False, which is False.
- So the expression becomes has_fast_pass or False, which is False or False, which is False.
- and 在 or 之前求值,所以表达式是 has_fast_pass or (age >= 12 and with_adult)。
- age >= 12 and with_adult 是 True and False,即 False。
- 所以表达式变成 has_fast_pass or False,即 False or False,即 False。
[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.
Marking points
评分要点
- A distinction is awarded if grade >= 90.
- A merit (the second outcome) is awarded if grade >= 70 and attendance is above the threshold.
- As a single structure: if grade >= 90 award distinction; elif grade >= 70 and attendance > threshold award merit; else no award.
- The two nested conditions that both had to be true are joined with and.
- 如果 grade >= 90 授予 distinction。
- 如果 grade >= 70 且出勤高于阈值,授予 merit(第二个结果)。
- 作为单一结构:if grade >= 90 授予 distinction;elif grade >= 70 and attendance > 阈值 授予 merit;else 不授予。
- 两个都必须为真的嵌套条件用 and 连接。
Flashcards
Active recall for this statement’s key terms.
Keep this statement’s practice with you
Free account: unlock every revision question and flashcard for B2.3.2, and every other IB CS 2027 statement.