A condition-controlled loop repeats until a condition says to stop. The count is not fixed in advance. Cambridge-style pseudocode has two forms: WHILE with ENDWHILE, and REPEAT with UNTIL.
This lesson follows tracing a count-controlled loop in repetition and arrays. The new idea is that the test, not a counter, decides when the loop ends.
When does each loop check its condition?
A WHILE loop tests first. If the test is false on the very first check, the body never runs.
A REPEAT loop runs the body first and tests afterwards. The body always runs at least once. The loop stops when the UNTIL condition is true, which is the opposite way round to WHILE.
For every trace, add a column for the test and write down true or false each time it is checked.
Worked example 1: a WHILE loop
Balance ← 100
Years ← 0
WHILE Balance < 150 DO
Balance ← Balance + 20
Years ← Years + 1
ENDWHILE
OUTPUT Years, Balance
| Test: Balance < 150 | Balance after | Years after |
|---|---|---|
| 100 < 150 true | 120 | 1 |
| 120 < 150 true | 140 | 2 |
| 140 < 150 true | 160 | 3 |
| 160 < 150 false | loop ends |
The output is 3, 160. The last test is false, so there is no fourth pass.
Worked example 2: a REPEAT loop
N ← 64
Steps ← 0
REPEAT
N ← N DIV 2
Steps ← Steps + 1
UNTIL N < 5
OUTPUT Steps, N
DIV is whole-number division.
| Pass | N after | Steps after | Test: N < 5 |
|---|---|---|---|
| 1 | 32 | 1 | false, repeat |
| 2 | 16 | 2 | false, repeat |
| 3 | 8 | 3 | false, repeat |
| 4 | 4 | 4 | true, stop |
The output is 4, 4. If N started at 3, a WHILE loop with the same test written as WHILE N >= 5 would run zero times, but this REPEAT loop would still run once and change N to 1.
The mistake to watch for
A common slip is to read UNTIL as “keep going while”.
Mistaken answer: “UNTIL N < 5 means the loop continues while N is less than 5, so it stops straight away.”
The student treated the UNTIL condition like a WHILE condition.
The correction is to read UNTIL N < 5 as “stop as soon as N is less than 5”. At the start N is 64, which is not less than 5, so the loop carries on. Put the words “stop when” in your margin next to every UNTIL.
Check yourself
1. What are the final value of X and the number of passes?
X ← 5
Passes ← 0
WHILE X > 0 DO
X ← X - 2
Passes ← Passes + 1
ENDWHILE
Show answer
X goes 5, 3, 1, then -1. Test results: 5 > 0 true, 3 > 0 true, 1 > 0 true, -1 > 0 false. The loop ran 3 passes and X is -1.
2. What is Count at the end?
Count ← 10
REPEAT
Count ← Count + 1
UNTIL Count >= 10
Show answer
The body runs once before any test, so Count becomes 11. Then 11 >= 10 is true and the loop stops. Count is 11.
3. Why does this loop never finish?
N ← 10
WHILE N <> 0 DO
N ← N - 3
ENDWHILE
Show answer
N takes the values 10, 7, 4, 1, -2, -5 and so on. It steps over 0 and never equals it, so N <> 0 stays true forever. Using WHILE N > 0 would fix it.
Where this leads next
Loops go wrong in a predictable way at the edges, which is the subject of repairing an off-by-one error. To watch a condition change step by step, use the pseudocode trace trainer, which stops runaway loops at a safety limit.
A teacher in Computer Science tuition can give you several new loops to trace until the stopping test is the first thing you check.