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.
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 counter ← 0 TO 20 STEP 2
OUTPUT counter // Outputs 0, 2, 4, 6, ... 20
NEXT counter
FOR counter ← 10 TO 1 STEP -1
OUTPUT counter // Outputs 10, 9, 8, ... 1
NEXT counter
total ← 0
FOR i ← 1 TO 5
INPUT number
total ← total + number
NEXT i
OUTPUT total
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
INPUT age
WHILE age < 0 OR age > 120 DO
OUTPUT "Invalid age, please re-enter"
INPUT age
ENDWHILE
OUTPUT "Age accepted: " & age
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
OUTPUT "1. Start"
OUTPUT "2. Quit"
INPUT choice
UNTIL choice = 1 OR choice = 2
| Feature | FOR | WHILE | REPEAT...UNTIL |
|---|---|---|---|
| Number of runs known? | Yes | No | No |
| Condition checked | Implicit (counter) | Before (pre-condition) | After (post-condition) |
| Minimum runs | 0 (if end < start) | 0 | Always at least 1 |
| Closing keyword | NEXT counter | ENDWHILE | UNTIL condition |
| Typical use | Processing arrays, fixed repetitions | Waiting for valid input, searching | Menus, at-least-one validations |
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
4 questions · 11 marks
| Term | Definition |
|---|
10 minutes · mixed marks