📘 Paper 2 · Topic 8: Programming
8.1c Iteration: FOR, WHILE and REPEAT
Cambridge IGCSE Computer Science 0478 · ~16 min read · ⭐ Pro

Iteration — Repeating Code

Iteration (also called a loop) allows a section of code to be repeated multiple times without having to write the code more than once. Cambridge IGCSE pseudocode has three iteration constructs: FOR, WHILE, and REPEAT...UNTIL.

FOR...TO...NEXT (Count-controlled loop)

Used when the number of repetitions is known in advance. A counter variable counts from the start value to the end value, incrementing by 1 each iteration.

FOR counter ← 1 TO 10
    OUTPUT counter
NEXT counter

FOR loop with a step

FOR counter ← 0 TO 20 STEP 2
    OUTPUT counter   // Outputs 0, 2, 4, 6, ... 20
NEXT counter

Counting down

FOR counter ← 10 TO 1 STEP -1
    OUTPUT counter   // Outputs 10, 9, 8, ... 1
NEXT counter

FOR loop calculating a sum

total ← 0
FOR i ← 1 TO 5
    INPUT number
    total ← total + number
NEXT i
OUTPUT total

WHILE...DO...ENDWHILE (Pre-condition loop)

The condition is checked BEFORE each iteration. If the condition is FALSE on the very first check, the loop body never runs. Use when you don't know in advance how many times the loop should run, but you have a condition to check at the start.

WHILE answer <> "quit" DO
    INPUT answer
    OUTPUT "You entered: " & answer
ENDWHILE

WHILE loop for validation

INPUT age
WHILE age < 0 OR age > 120 DO
    OUTPUT "Invalid age, please re-enter"
    INPUT age
ENDWHILE
OUTPUT "Age accepted: " & age

REPEAT...UNTIL (Post-condition loop)

The condition is checked AFTER each iteration. The loop body ALWAYS runs at least once. Use when you need the loop to run at least once regardless of the condition (common for menus and validation).

REPEAT
    OUTPUT "Enter a positive number:"
    INPUT num
UNTIL num > 0

REPEAT for menu validation

REPEAT
    OUTPUT "1. Start"
    OUTPUT "2. Quit"
    INPUT choice
UNTIL choice = 1 OR choice = 2

Comparing the Three Loops

FeatureFORWHILEREPEAT...UNTIL
Number of runs known?YesNoNo
Condition checkedImplicit (counter)Before (pre-condition)After (post-condition)
Minimum runs0 (if end < start)0Always at least 1
Closing keywordNEXT counterENDWHILEUNTIL condition
Typical useProcessing arrays, fixed repetitionsWaiting for valid input, searchingMenus, at-least-one validations

Nested Loops

A loop placed inside another loop. The inner loop completes all its iterations for each single iteration of the outer loop.

FOR row ← 1 TO 3
    FOR col ← 1 TO 3
        OUTPUT row & "," & col
    NEXT col
NEXT row
// Outputs: 1,1 then 1,2 then 1,3 then 2,1 ... 3,3
Exam tip: If the question says "the user must enter a value at least once", use REPEAT...UNTIL. If it says "loop while a condition is true", use WHILE. For a fixed number of times, use FOR. NEXT must always be followed by the counter variable name in Cambridge pseudocode.
⚠️ Common Mistakes
  • Writing ENDFOR instead of NEXT counter — Cambridge uses NEXT counter
  • Forgetting ENDWHILE at the end of a WHILE loop
  • Using REPEAT when the loop should not run if the condition is already met
  • Off-by-one errors: FOR i ← 1 TO 10 runs 10 times; FOR i ← 0 TO 10 runs 11 times
  • Infinite loops: forgetting to update the loop variable inside a WHILE loop
Video coming soon
Click slide or press arrow keys to navigate
✍️

Worksheet — Iteration

4 questions · 11 marks

Q1Write pseudocode using a FOR loop to output the times table of 7 (7×1 through 7×10). [3]
✅ Mark scheme
FOR i ← 1 TO 10 [1]; OUTPUT 7 * i [1] (or OUTPUT i & " x 7 = " & 7*i); NEXT i [1]
Q2Write pseudocode using REPEAT...UNTIL to ask the user for a password until they enter "secret". [3]
✅ Mark scheme
REPEAT [1]; INPUT password [1]; UNTIL password = "secret" [1]
Q3State the key difference between WHILE and REPEAT...UNTIL. When must you use REPEAT...UNTIL? [2]
✅ Mark scheme
WHILE checks the condition BEFORE running the loop body (pre-condition); REPEAT...UNTIL checks AFTER (post-condition) [1]. Use REPEAT...UNTIL when the loop must run at least once [1].
Q4A WHILE loop uses the condition: WHILE count < 5 DO, starting with count ← 1. The loop body only contains OUTPUT count. How many times does the loop run? What is the error? [3]
✅ Mark scheme
The loop runs infinitely [1] because count is never incremented inside the loop body [1]. Fix: add count ← count + 1 inside the loop [1].
Quiz — Iteration
Q 1 of 7
Score
/ 7
Click to reveal
TermDefinition
🎯

Mini Test — Iteration

10 minutes · mixed marks

← 8.1b Selection Topic 8: Programming Next: 8.2 Arrays →
🔒
Pro Content
Upgrade to access all lessons.
£7.99/month
or £59/year
Subscribe now →