10.1 What is REPEAT...UNTIL?
REPEAT...UNTIL is a post-condition loop — the body of the loop runs first, and the condition is checked afterwards. This means the loop body is always executed at least once, even if the condition is already TRUE when the program reaches the loop.
- 1Post-condition: The condition is tested after each pass through the body, not before.
- 2Runs at least once: Because the test happens at the end, the body is guaranteed to execute one or more times.
- 3Exit condition: The loop stops when the condition becomes TRUE (opposite of WHILE, which stops when FALSE).
- 4Manual update required: You must update the loop control variable inside the body — otherwise the loop runs forever (infinite loop).
Key Idea
Think of REPEAT...UNTIL as "do this task, then check if you can stop." The checking happens at the end of each iteration.
Common Use
REPEAT...UNTIL is perfect for input validation and menu systems — situations where the user must be prompted at least once before you can check their response.
Concepts — What REPEAT...UNTIL Means
10.2 Syntax
The CIE pseudocode syntax for REPEAT...UNTIL is straightforward. The body sits between the REPEAT keyword and the UNTIL keyword, with the condition appearing on the same line as UNTIL.
REPEAT
<statements to repeat>
UNTIL <condition>DECLARE Counter : INTEGER
Counter <- 1
REPEAT
OUTPUT Counter
Counter <- Counter + 1
UNTIL Counter > 5REPEAT Keyword
Marks the start of the loop body. No condition is written here.
UNTIL Keyword
Marks the end of the loop body. The exit condition follows on the same line.
Watch Out
Unlike WHILE...DO...ENDWHILE, REPEAT...UNTIL has no closing ENDREPEAT keyword — the UNTIL line itself closes the loop. Forgetting this is a common exam mistake.
The condition can be any expression that produces a BOOLEAN result (TRUE or FALSE), such as Counter > 5, Password = "open", or (Age >= 18) AND (HasID = TRUE).
Syntax — Writing REPEAT...UNTIL Correctly
10.3 Tracing
A trace table is the most reliable way to follow a loop's behaviour. Each row records the state of the variables after one complete iteration of the body, plus the result of evaluating the UNTIL condition.
DECLARE Counter : INTEGER
DECLARE Total : INTEGER
Counter <- 1
Total <- 0
REPEAT
Total <- Total + Counter
Counter <- Counter + 1
UNTIL Counter > 4
OUTPUT TotalTrace table:
| Iteration | Counter (before) | Total (after) | Counter (after) | Counter > 4? |
|---|---|---|---|---|
| 1 | 1 | 1 | 2 | FALSE — loop again |
| 2 | 2 | 3 | 3 | FALSE — loop again |
| 3 | 3 | 6 | 4 | FALSE — loop again |
| 4 | 4 | 10 | 5 | TRUE — exit loop |
The loop exits when Counter becomes 5 (which is > 4). The final value of Total is 10 (1 + 2 + 3 + 4), which is what the OUTPUT prints.
Tracing Rule
In REPEAT...UNTIL, the condition is tested after the body. So even if the condition would be TRUE at the start, the body still runs once before any check.
DECLARE X : INTEGER
X <- 100
REPEAT
OUTPUT "X is ", X
X <- X + 1
UNTIL X > 50
OUTPUT "Done"Here X starts at 100, which is already > 50. But because REPEAT runs the body first, "X is 100" is still printed once before the loop exits. The program then prints "Done".
Tracing — Step Through the Loop
10.4 REPEAT...UNTIL vs WHILE
Both REPEAT...UNTIL and WHILE are indefinite loops — the number of iterations is not known in advance. But they differ in two crucial ways: whenthe condition is checked, and what value ends the loop.
| Feature | REPEAT...UNTIL | WHILE...DO...ENDWHILE |
|---|---|---|
| Condition checked | After body (post-test) | Before body (pre-test) |
| Body runs at least once | Yes — always | No — may be skipped entirely |
| Loop continues while condition is | FALSE | TRUE |
| Loop exits when condition is | TRUE | FALSE |
| Best for | Input validation, menus | Sentinel-controlled sums, reading data |
| Closing keyword | UNTIL (with condition) | ENDWHILE |
// REPEAT...UNTIL version (body always runs once)
DECLARE Age : INTEGER
REPEAT
OUTPUT "Enter age (18+): "
INPUT Age
UNTIL Age >= 18
// WHILE version (must initialise Age first, body may not run)
DECLARE Age : INTEGER
Age <- 0
WHILE Age < 18 DO
OUTPUT "Enter age (18+): "
INPUT Age
ENDWHILEKey Distinction
The UNTIL condition is the EXIT condition (loop stops when TRUE). The WHILE condition is the CONTINUE condition (loop stops when FALSE). Getting this backwards is the #1 student error.
When to Choose REPEAT
Pick REPEAT...UNTIL whenever you must ask the user (or read a value) at least once before you can evaluate the condition — for example password prompts, "press Y to continue" menus, or "enter a positive number" validation.
REPEAT vs WHILE — Knowing the Difference
10.5 Practical Examples
REPEAT...UNTIL shines in three classic scenarios: input validation, menu systems, and sentinel-controlled accumulation. Let's look at a worked example of each.
Example 1 — Input Validation
DECLARE Num : INTEGER
REPEAT
OUTPUT "Enter a positive number: "
INPUT Num
UNTIL Num > 0
OUTPUT "Thanks! You entered ", NumThe prompt is shown at least once. If the user enters 0 or a negative value, the condition Num > 0 is FALSE so the loop repeats — asking again.
Example 2 — Sentinel-Controlled Sum
DECLARE Value : INTEGER
DECLARE Total : INTEGER
Total <- 0
REPEAT
OUTPUT "Enter a value (-1 to stop): "
INPUT Value
IF Value <> -1 THEN
Total <- Total + Value
ENDIF
UNTIL Value = -1
OUTPUT "Total is ", TotalThe sentinel value -1 is not added to the total — the IF inside the loop guards against that. The loop exits when Value = -1 becomes TRUE.
Example 3 — Simple Menu
DECLARE Choice : INTEGER
REPEAT
OUTPUT "1. Play"
OUTPUT "2. Settings"
OUTPUT "3. Quit"
INPUT Choice
CASE OF Choice
1 : OUTPUT "Starting game..."
2 : OUTPUT "Opening settings..."
3 : OUTPUT "Goodbye!"
OTHERWISE OUTPUT "Invalid choice"
ENDCASE
UNTIL Choice = 3The menu always displays at least once. It only stops when the user picks option 3 (Quit), which sets Choice = 3 to TRUE.
Update Inside
In every example above, the loop control variable (Num, Value, Choice) is updated inside the body via INPUT. If you forget to update it, the loop runs forever — an infinite loop.
Practical Examples — Apply What You Know
10.6 Key Points Summary
Here is a quick recap of everything we've covered on REPEAT...UNTIL:
- Post-condition loop — The condition is checked AFTER the body executes.
- Runs at least once — The body is always executed one or more times.
- Exit when TRUE — The loop stops when the UNTIL condition becomes TRUE.
- No ENDREPEAT — UNTIL <condition> closes the loop — there is no ENDREPEAT keyword.
- Manual update — You must update the loop control variable inside the body.
- Best for validation — Ideal for input validation and menus — anywhere a prompt must appear at least once.
- Opposite of WHILE — WHILE continues on TRUE; REPEAT continues on FALSE. Pick the loop that matches your logic.
Exam Tip
When a CIE question says "the user must be prompted at least once" or "validate the input", reach for REPEAT...UNTIL. When it says "while the value is not equal to X, keep reading", reach for WHILE. Match the keyword to the English phrasing in the question.