Recap
- Selection decides which statements run based on a Boolean condition.
IF(condition) { ... }runs the block only if the condition is true.IF(condition) { ... } ELSE { ... }runs exactly one block.- Flow of control normally goes top to bottom; a conditional skips blocks.
- Every algorithm can be built with sequencing, selection and iteration.
Key relationships
| Pattern | What runs | Example |
|---|---|---|
| IF only | Block if true; nothing if false | Apply a discount only on big orders |
| IF/ELSE | Exactly one of two blocks | Child price or adult price |
| Two separate IFs | Zero, one or both blocks | Two independent checks |
| Boolean variable as condition | IF(isOpen) |
No need to write isOpen = true |
| Default then override | Set value, IF changes it | discount ← 0 then IF raises it |
Assumptions
- The condition is a Boolean expression evaluated when the IF is reached.
- Statements after the conditional run whichever block was chosen.
- Blocks are marked by braces in the Text notation.
Method for tracing
- Write the values of all variables.
- Evaluate the condition and write
trueorfalse. - Run only the matching block; cross out the other.
- Continue from the first statement after the conditional.
Mistakes to avoid
- Running both the IF block and the ELSE block.
- Treating two IFs as IF/ELSE.
- Using
>instead of≥at the boundary. - Testing only one branch.
- Forgetting to give a variable a default value before an IF without ELSE.
Worked mini-example
n ← 14
IF(n MOD 2 = 0)
{
DISPLAY("even")
}
ELSE
{
DISPLAY("odd")
}
Output: even
14 MOD 2 is 0, so the condition is true and the first block runs. With n equal to 15 the remainder is 1, the condition is false and the ELSE block runs.
Quick self-check
- In an IF/ELSE, how many blocks run?
- After
x ← 7,IF(x > 5) {DISPLAY("a")},IF(x > 6) {DISPLAY("b")}, what is displayed? - Which test value checks the boundary of
score ≥ 500?
Answers
- Exactly one.
a b: both conditions are true and the IFs are independent.- 500 itself (and 499 to confirm it fails).
Where marks are won and lost
Tracing questions give values and ask what a segment displays or what variable holds at the end. The classic trap is independent IF statements versus IF/ELSE. Writing questions ask which condition matches a description, or which segment is correct, so test boundaries. On the exam you may also be asked to say which test values would reach every block. In the Create task you must include selection in your own procedure and explain what it does. Before you commit to an answer, ask two questions: what happens exactly at the boundary value, and should exactly one block run or possibly several?
Back to the full study guide for worked examples and explanations.