Topic 11.2 – Constructs: Post‑condition Loop (REPEAT … UNTIL)
Learning objectives
- Write correct Cambridge‑style pseudocode for a post‑condition loop using the
REPEAT … UNTIL construct. - Analyse a problem to decide when a post‑condition loop is the most appropriate control structure.
- Design and implement an algorithm that satisfies AO2 (analysis) and AO3 (design/implementation) of the A‑Level specification.
- Translate the algorithm into a clear flowchart and explain its operation in words.
What is a post‑condition loop?
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.
Cambridge pseudocode syntax
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.
Key characteristics
- The condition is evaluated after each execution of the loop body.
- The loop body is executed at least once, regardless of the initial truth value of the condition.
- A loop‑control variable must be initialised before the
REPEAT and updated inside the loop; otherwise the loop may become infinite. - Typical use‑cases: input validation, sentinel‑controlled loops, “do‑something‑until‑the‑user‑says‑stop” scenarios, or any situation where the first action must be performed before a test can be made.
When to choose a post‑condition loop
- You need to guarantee that the body runs at least once (e.g. read the first value before you can test for a sentinel).
- The condition depends on data that is produced inside the loop (e.g. a running total, a counter that is incremented each pass).
- The problem description explicitly says “repeat … until …” or “do … while …”.
- Using a
WHILE loop would require duplicate code to handle the first iteration.
Comparison with pre‑condition loops (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). |
Design & implementation checklist
- Identify a task that must be performed at least once.
- Initialise every loop‑control variable before the
REPEAT line. - Place all statements that achieve the task inside the
REPEAT block. - Update the loop‑control variable(s) inside the block so the termination condition will eventually become true.
- Write the termination condition after
UNTIL. Remember the condition is true when the loop should stop. - Check for the three “termination checklist” items:
- Variable initialised?
- Variable updated each iteration?
- Condition moves toward
true?
Step‑by‑step construction of a REPEAT‑UNTIL loop
- Task identification: what must happen at least once?
- Initialisation: set up counters, totals, or flags before the loop.
- Body writing: code the statements that accomplish the task.
- Update: modify the control variable(s) inside the body.
- Termination condition: decide when the loop should stop and express it after
UNTIL. - Review: verify the loop cannot become infinite and that the body will run the required number of times.
Common pitfalls and how to avoid them
- Missing initialisation – the loop may start with an undefined value. Initialise all variables before
REPEAT. - No update inside the loop – leads to an infinite loop. Ensure the control variable is changed each pass.
- Wrong condition direction – remember
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. - Using
REPEAT when WHILE is simpler – choose the construct that matches the problem description; unnecessary use of REPEAT can waste marks.
Example 1 – Summing the first n positive integers
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.
Example 2 – Input validation (sentinel loop)
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.
Example 3 – Password entry with a maximum of three attempts
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.
Link to Assessment Objectives
- AO2 – Analyse problems: Deciding that a task must be performed at least once (e.g., input validation) shows you have analysed the problem’s requirements.
- AO3 – Design and implement solutions: Translating the analysis into a correct
REPEAT … UNTIL structure, with proper initialisation, update and termination, demonstrates design and implementation skills.
Exam relevance (Paper 2)
- Typical command‑style question: “Write a REPEAT‑UNTIL loop that …”. You must include the three‑line structure, correct indentation, and a condition that will inevitably become true.
- Mark‑scheme expectations:
- Correct keywords (
REPEAT, UNTIL). - Loop‑control variable initialised before the loop.
- Variable updated inside the loop.
- Termination condition logically guarantees exit.
- Clear, indented pseudocode (usually 1‑2 marks per element).
- Students may also be asked to explain why a post‑condition loop is preferred; refer to the “When to choose a post‑condition loop” bullet list and the comparison table.
Suggested flowchart (text description)
- Start.
- Process box – REPEAT block (the statements that form the loop body).
- Decision diamond – labelled “condition?”.
- If the condition is False, arrow back to the process box (repeat).
- If the condition is True, arrow to the End node.
This visualises the “body → test → back or exit” flow that distinguishes post‑condition loops.
Alignment with the Cambridge 9618 syllabus (2026)
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.
Further practice