What is iteration?
Iteration means repeating a group of instructions. The repeated instructions are called the loop body. A loop either repeats a specified number of times or uses a condition to decide whether to continue.
Imagine running laps on a track: you might complete exactly five laps, keep running while time remains, or finish one lap before deciding whether to stop. This is the analogy used in the FutureLogic Iteration lesson.
Compare the three loop types
| Loop | Control | Condition checked | Minimum runs |
|---|---|---|---|
| FOR | Count-controlled | Loop counter determines repetitions | Zero, if the range has no iterations |
| WHILE | Pre-condition | Before each iteration | Zero |
| REPEAT UNTIL | Post-condition | After each iteration | One |
Key distinction: WHILE repeats while its condition is TRUE; REPEAT UNTIL repeats until its condition becomes TRUE. These are not interchangeable conditions.
1. FOR — repeat a known number of times
Use a count-controlled loop when you know the number of repetitions in advance. This example displays the numbers 1 through 5.
FOR Count ← 1 TO 5
OUTPUT Count
NEXT CountThe loop counter takes the values 1, 2, 3, 4 and 5. Output: 1 2 3 4 5. The upper bound is included in this example.
FOR ... TO ... NEXT. Do not copy Python's range(1, 6) syntax into a pseudocode answer.2. WHILE — check before running
A WHILE loop checks the condition before executing its body. If the condition is FALSE at the start, the body runs zero times.
Count ← 1
WHILE Count <= 3 DO
OUTPUT Count
Count ← Count + 1
ENDWHILEThe output is 1 2 3. After the third iteration, Count becomes 4. Since 4 <= 3 is FALSE, the loop stops.
3. REPEAT UNTIL — run before checking
A REPEAT UNTIL loop executes its body first and checks its stopping condition afterwards. That means the body always runs at least once.
Count ← 1
REPEAT
OUTPUT Count
Count ← Count + 1
UNTIL Count > 3The output is again 1 2 3. The loop stops when Count becomes 4 and the condition Count > 3 is TRUE.
Worked trace table: follow every change
Trace the WHILE example above. Write the value of the counter at the start of each check, the condition result, and any output.
| Count at check | Count <= 3? | Output | Count after update |
|---|---|---|---|
| 1 | TRUE | 1 | 2 |
| 2 | TRUE | 2 | 3 |
| 3 | TRUE | 3 | 4 |
| 4 | FALSE | None | 4 |
The final FALSE check is important: it explains why the loop finishes. You can revise the method further in our trace table guide.
When the initial condition is already FALSE
Consider a variable starting at 10 and a condition of Number < 5. A WHILE loop with this condition does not run at all. A REPEAT UNTIL loop with Number >= 5 still runs its body once before checking and stopping.
Check your understanding
Which loop type guarantees that its body executes at least once?
Reveal the answer
REPEAT UNTIL, because its condition is checked after the body runs.
How to choose the right loop
| Scenario | Suitable loop | Why? |
|---|---|---|
| Display ten multiplication-table results | FOR | Ten repetitions are known |
| Process input while a file has more records | WHILE | Check whether more work remains first |
| Ask for a password, then check whether it is accepted | REPEAT UNTIL | At least one attempt must be made |
More than one loop type may solve some tasks. Choose one whose condition and control flow are clear.
Common Cambridge exam mistakes
WHILE continues when its condition is TRUE. UNTIL stops when its condition becomes TRUE.
Trace inclusive FOR bounds carefully and check whether a counter is updated before or after output.
Ensure that values change so the loop can eventually terminate.
Exam focus — original FutureLogic practice
Try each question before revealing the suggested answer. Mark values are illustrative, not an official Cambridge mark scheme.
Question 1 (2 marks)
Explain why a WHILE loop may execute zero times.
Reveal model answer
The condition is checked before the loop body. If it is FALSE initially, the body is skipped.
Question 2 (3 marks)
Write pseudocode using FOR to output the numbers 2, 4, 6, 8 and 10.
Reveal model answer
FOR Count ← 1 TO 5
OUTPUT Count * 2
NEXT CountQuestion 3 (3 marks)
What is the output of the following pseudocode?
Value ← 3
REPEAT
OUTPUT Value
Value ← Value - 1
UNTIL Value = 0Reveal model answer
3, 2, 1. The condition becomes TRUE after Value is reduced to 0.
Question 4 (3 marks)
Describe two differences between WHILE and REPEAT UNTIL.
Reveal model answer
WHILE tests before executing and can run zero times; REPEAT UNTIL tests after executing and runs at least once. WHILE continues while its condition is TRUE, whereas REPEAT UNTIL stops when its condition is TRUE.
Quick quiz
- Which loop is count-controlled?
- Which loop checks its condition before the body?
- What is the minimum number of times REPEAT UNTIL runs?
- What can happen if a WHILE condition never becomes FALSE?
- What does NEXT do in a FOR loop?
Reveal quiz answers
- FOR.
- WHILE.
- Once.
- An infinite loop.
- Advances the loop to its next iteration (or finishes when the range is complete).
Final summary
FOR
Best for a known number of repetitions.
WHILE
Checks first; may run zero times.
REPEAT UNTIL
Checks last; runs at least once.
Trace carefully
Follow updates, outputs and the stopping test.