Selection, Iteration
& Code Tracing
IF statements, loops, and the hand-tracing method that turns every code-tracing question into a solvable table exercise.
§1 Selection (IF Statements)
1.1 IF and IF-ELSE
IF runs a block of code only if a condition is true. IF-ELSE picks one of two blocks:
IF(score ≥ 60)
{
DISPLAY("Pass")
}
ELSE
{
DISPLAY("Fail")
}
1.2 Nested IF (Chained Conditions)
For multi-way decisions, nest IF-ELSE statements. Order matters: the first matching condition wins.
IF(score ≥ 90)
{
grade ← "A"
}
ELSE
{
IF(score ≥ 80)
{
grade ← "B"
}
ELSE
{
grade ← "C"
}
}
If you check score ≥ 60 before score ≥ 90, everyone scoring 90+ gets caught by the first condition and assigned a lower grade. Always order conditions from most specific (highest threshold) to most general.
§2 Iteration (Loops)
2.1 REPEAT n TIMES
Runs a block exactly n times. Use when you know how many iterations you need.
count ← 0
REPEAT 5 TIMES
{
count ← count + 1
}
After this loop: count = 5.
2.2 REPEAT UNTIL
Runs until a condition becomes true. Use when you don't know how many iterations are needed.
n ← 1
REPEAT UNTIL(n > 100)
{
n ← n * 2
}
This doubles n until it exceeds 100. Final value: 128.
If nothing inside the loop changes the condition, the loop runs forever. Every REPEAT UNTIL must have something inside that moves the condition toward being true.
2.3 Choosing Between Loops
- Fixed count: use REPEAT n TIMES.
- Every list element: use FOR EACH.
- Until something happens: use REPEAT UNTIL.
§3 Tracing Code — The Method
The single most valuable skill for the MCQ. The method: list every variable as a column, and write down its value after each line.
PROCEDURE mystery(n)
{
result ← 1
i ← 1
REPEAT UNTIL(i > n)
{
result ← result * i
i ← i + 1
}
RETURN(result)
}
| Step | n | result | i | Action |
|---|---|---|---|---|
| 1 | 3 | 1 | 1 | Initialize |
| 2 | 3 | 1×1 = 1 | 2 | Loop iter 1 |
| 3 | 3 | 1×2 = 2 | 3 | Loop iter 2 |
| 4 | 3 | 2×3 = 6 | 4 | Loop iter 3 |
| 5 | 3 | 6 | 4 | 4 > 3 → exit, return 6 |
Return value: 6. This procedure computes n factorial.
Don't trust your head — write down each variable's value as it changes. A neat table with a line per iteration catches off-by-one errors that mental tracing misses.
§4 Exam Patterns
4.1 "What is displayed / returned?"
Build a trace table. Track every variable per line. Check the loop condition at each iteration.
4.2 "Which change fixes the bug?"
Trace the broken code to see what it actually produces. Compare to what it should produce. The fix is the smallest change that makes the trace match.
4.3 "Which Boolean expression is equivalent?"
Build a truth table for the original. Then check each candidate against the same table. De Morgan's laws are your shortcut.
§5 Boolean Expressions in Depth
Topic 3.5 is examined far more often than its one page in the framework suggests, because every conditional and every REPEAT UNTIL is built on one.
NOT condition | true exactly when the condition is false |
a AND b | true only when both are true |
a OR b | true when at least one is true — including both |
Everyday English uses "or" to mean one or the other but not both. In programming, OR is true when both are true as well. Questions describing "either … or …" and offering both an inclusive and an exclusive reading are testing exactly this.
5.1 Negating a compound condition
To negate an AND, negate each part and swap to OR — and vice versa. Reading it aloud is the fastest check:
// "not (raining and cold)"
NOT (raining AND cold) // same as: (NOT raining) OR (NOT cold)
// "not (weekend or holiday)"
NOT (weekend OR holiday) // same as: (NOT weekend) AND (NOT holiday)
If it is not the case that it is both raining and cold, then at least one of those failed. If it is neither a weekend nor a holiday, then both failed. Test any candidate answer with one concrete case where the two expressions might disagree.
5.2 Ranges are two conditions, not one
// "between 10 and 20" written correctly
(x >= 10) AND (x <= 20)
Writing (x >= 10) OR (x <= 20) is true for every number, and (x < 10) AND (x > 20) is true for none. Both appear as distractors. Check a boundary value and a value far outside the range before choosing.
§6 Iteration — What Actually Gets Tested
6.1 Counting the passes
REPEAT n TIMES is decided before it starts: the body runs exactly n times regardless of what happens inside. REPEAT UNTIL (condition) is decided as it goes, checking before each pass — so if the condition is already true, the body never runs at all.
An answer choice claiming a REPEAT UNTIL body "always runs at least once" is wrong. That is the most common single mistake on iteration questions.
6.2 Nested loops
REPEAT 3 TIMES
{
REPEAT 4 TIMES
{
DISPLAY("*")
}
}
// 12 stars — the inner loop restarts on every outer pass
Nesting multiplies; sequencing adds. Two loops written one after the other run 3 + 4 = 7 times; nested they run 3 × 4 = 12.
6.3 Why a loop fails to terminate
| Nothing updates the variable | the condition tests something the body never changes |
| It moves the wrong way | the counter goes away from the stopping value instead of toward it |
| The target is skipped | testing = 10 while stepping by 3 from 0 — it passes 9 then 12 and never equals 10 |
Section II asks you to describe a condition or error that would make your own first iteration statement never terminate. The three rows above are the three answers — and if your loop genuinely cannot fail, you are asked how you would modify it so that it could.
6.4 Tracing with a table
Write one column per variable and one row per pass. Update every variable on each row before moving on, and evaluate the loop condition at the top of the next row. When the question asks "how many times", count rows; when it asks "what is displayed", read the display column downward. This is slower than tracing in your head for two passes and far faster for six.
§7 Key Terms
| Term | Definition |
|---|---|
| Condition | A Boolean expression that controls an IF or loop |
| Iteration | Repetition using a loop |
| Selection | Choosing between branches using IF / IF-ELSE |
| Sequencing | Executing statements in order |
§8 Self-Check
- Trace this code and give the final value of total:
total ← 0, i ← 1, REPEAT UNTIL(i > 5) { total ← total + i, i ← i + 1 } - A program checks IF(x > 10) before IF(x > 20). For x = 25, which branch executes? Is this correct if x > 20 should get a different result?
- Write pseudocode that counts the number of even integers in a list called nums.
Show Answers
- total = 1 + 2 + 3 + 4 + 5 = 15.
- The x > 10 branch executes (since 25 > 10 is true, and it's checked first). If x > 20 needs a different result, the conditions are in the wrong order — check x > 20 first.
- count ← 0
FOR EACH n IN nums
{
IF(n MOD 2 = 0)
{
count ← count + 1
}
}