REPEAT … UNTIL construct.A post‑condition loop executes its body first and then tests a termination condition. Because the test occurs after the statements, the loop body is guaranteed to run at least once, even if the condition is true before the first iteration.
REPEAT/* statements */
UNTIL condition
Some teachers add END REPEAT for visual clarity, but the Cambridge specification only requires the three lines shown above. Use whichever style you choose consistently throughout the exam.
REPEAT and updated inside the loop; otherwise the loop may become infinite.WHILE loop would require duplicate code to handle the first iteration.WHILE)| Aspect | Pre‑condition loop (WHILE) | Post‑condition loop (REPEAT … UNTIL) |
|---|---|---|
| When is the condition evaluated? | Before the first execution of the body. | After each execution of the body. |
| Guarantee of at least one execution | No – the body may never run. | Yes – the body always runs at least once. |
| Typical Cambridge syntax | WHILE condition DO … END WHILE | REPEAT … UNTIL condition |
| Common exam‑preferred situations | When the loop can be legitimately skipped (e.g. counting down from a known start). | When the first action must be performed before the test (e.g. read‑until‑sentinel, input validation). |
REPEAT line.REPEAT block.UNTIL. Remember the condition is true when the loop should stop.true?UNTIL.REPEAT.UNTIL condition stops when the condition becomes true. Use i > n rather than i <= n if you want to stop after the last required iteration.REPEAT when WHILE is simpler – choose the construct that matches the problem description; unnecessary use of REPEAT can waste marks.Compute S = 1 + 2 + … + n where n is entered by the user.
READ nsum ← 0
i ← 1
REPEAT
sum ← sum + i // add current i
i ← i + 1 // update loop‑control variable
UNTIL i > n
OUTPUT sum
Why a post‑condition loop? The first addition must be performed even if n is 0. The condition i > n is checked after the addition, guaranteeing exactly n iterations.
Read integer values from the user until a negative number is entered. Store the total of the non‑negative numbers.
total ← 0REPEAT
READ x
IF x ≥ 0 THEN
total ← total + x
END IF
UNTIL x < 0
OUTPUT total
This is a classic sentinel‑controlled situation: the first value must be read before we can test whether it is the sentinel.
Ask the user for a password. Stop after three failed attempts or when the correct password (“ABC123”) is entered.
attempt ← 0REPEAT
READ password
attempt ← attempt + 1
UNTIL password = "ABC123" OR attempt = 3
IF password = "ABC123" THEN
OUTPUT "Access granted"
ELSE
OUTPUT "Access denied"
END IF
The body (reading a password and counting attempts) must run at least once, making REPEAT … UNTIL the natural choice.
REPEAT … UNTIL structure, with proper initialisation, update and termination, demonstrates design and implementation skills.REPEAT, UNTIL).This visualises the “body → test → back or exit” flow that distinguishes post‑condition loops.
The above note fully satisfies the Constructs sub‑section of the A‑Level specification (Topic 11.2). It provides the required knowledge (definition, syntax, characteristics), the analytical skills (when to choose the construct – AO2) and the design/implementation skills (writing correct pseudocode and flowchart – AO3). Other syllabus areas (e.g., data representation, networking, hardware) are outside the scope of this specific topic but are covered elsewhere in the full course notes.
WHILE NOT endoffile DOREAD line
PROCESS line
END WHILE
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.