FutureLogic Blog → Programming → IGCSE 0478

How do FOR, WHILE and REPEAT UNTIL loops work?

FOR, WHILE and REPEAT UNTIL loops repeat instructions in a program. A FOR loop is usually used when the number of repetitions is known; a WHILE loop checks its condition before each repetition; and a REPEAT UNTIL loop checks its stopping condition after running the instructions.

Iteration is one of the three fundamental programming constructs in Cambridge IGCSE Computer Science (0478). This guide compares count-controlled, pre-condition and post-condition loops using Cambridge-style pseudocode, trace tables, worked examples and exam practice.

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.

Repeat the instructions. Check when to stop.

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

LoopControlCondition checkedMinimum runs
FORCount-controlledLoop counter determines repetitionsZero, if the range has no iterations
WHILEPre-conditionBefore each iterationZero
REPEAT UNTILPost-conditionAfter each iterationOne

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 Count

The 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.

Exam tip: In Cambridge pseudocode, a FOR loop uses 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
ENDWHILE

The output is 1 2 3. After the third iteration, Count becomes 4. Since 4 <= 3 is FALSE, the loop stops.

Common mistake: Forgetting to update Count inside the loop. If Count stays at 1, the condition never becomes FALSE, causing an infinite loop.

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 > 3

The output is again 1 2 3. The loop stops when Count becomes 4 and the condition Count > 3 is TRUE.

WHILE: continue while TRUE. REPEAT UNTIL: stop when 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 checkCount <= 3?OutputCount after update
1TRUE12
2TRUE23
3TRUE34
4FALSENone4

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

ScenarioSuitable loopWhy?
Display ten multiplication-table resultsFORTen repetitions are known
Process input while a file has more recordsWHILECheck whether more work remains first
Ask for a password, then check whether it is acceptedREPEAT UNTILAt 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

✗ Confusing WHILE with UNTIL.

WHILE continues when its condition is TRUE. UNTIL stops when its condition becomes TRUE.

✗ Missing the final iteration.

Trace inclusive FOR bounds carefully and check whether a counter is updated before or after output.

✗ Forgetting a stopping condition.

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 Count

Question 3 (3 marks)

What is the output of the following pseudocode?

Value ← 3
REPEAT
    OUTPUT Value
    Value ← Value - 1
UNTIL Value = 0
Reveal 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

  1. Which loop is count-controlled?
  2. Which loop checks its condition before the body?
  3. What is the minimum number of times REPEAT UNTIL runs?
  4. What can happen if a WHILE condition never becomes FALSE?
  5. What does NEXT do in a FOR loop?
Reveal quiz answers
  1. FOR.
  2. WHILE.
  3. Once.
  4. An infinite loop.
  5. 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.

Ready to practise programming?

Build confidence with FutureLogic's programming lessons and revision resources.