A repeat-until loop executes its body of code at least once, and then keeps repeating until a specified condition becomes true. Unlike a while loop, which checks its condition before each iteration, a repeat-until loop checks its condition after — this guarantees the body runs at minimum one time, making it ideal for input validation and menu-driven programs.
What is a post-condition loop?
Loops can be classified by when they check their condition:
- Pre-condition loop (while): the condition is checked before each iteration. If the condition is false from the start, the body never runs.
- Post-condition loop (repeat-until): the condition is checked after each iteration. The body always runs at least once.
Analogy: a pre-condition loop is like checking the weather before leaving home — if it is raining, you might not go out at all. A post-condition loop is like going outside first, then looking up to decide whether to come back in — you are always outdoors at least once.
How does a repeat-until loop work in pseudocode?
In AQA pseudocode, the syntax is:
REPEAT
[statements]
UNTIL [condition]
The statements between REPEAT and UNTIL are executed once. Then the condition is evaluated. If it is true, the loop ends. If it is false, the statements execute again.
Note the logic carefully: you continue while the condition is false and stop when it is true. This is the opposite of a while loop, where you continue while the condition is true.
Worked example 1: input validation
A common use of repeat-until is to keep asking for input until a valid value is entered.
REPEAT
OUTPUT "Enter a number between 1 and 10:"
number ← USERINPUT
UNTIL number >= 1 AND number <= 10
OUTPUT "You entered: " + number
Trace for input sequence [−3, 0, 7]:
| Iteration | Input | Condition (num ≥ 1 AND num ≤ 10) | Loop continues? |
|---|---|---|---|
| 1 | −3 | False | Yes |
| 2 | 0 | False | Yes |
| 3 | 7 | True | No — loop exits |
Output: You entered: 7
This could be written as a while loop, but that would require an initial input before the loop and then a second input inside the loop — the repeat-until is more natural here.
Worked example 2: a menu-driven program
REPEAT
OUTPUT "1. Start game"
OUTPUT "2. High scores"
OUTPUT "3. Quit"
choice ← USERINPUT
IF choice = 1 THEN
startGame()
ELIF choice = 2 THEN
showHighScores()
ENDIF
UNTIL choice = 3
OUTPUT "Goodbye!"
The menu must always display at least once — a repeat-until guarantees this without any special setup outside the loop.
What is the difference between a while loop and a repeat-until loop?
| Feature | While loop | Repeat-until loop |
|---|---|---|
| Condition checked | Before each iteration | After each iteration |
| Minimum iterations | 0 (may never run) | 1 (always runs at least once) |
| Continues while | Condition is true | Condition is false |
| Stops when | Condition becomes false | Condition becomes true |
| Best for | When you may not need to run at all | When you always need at least one run |
Crucial exam point: a while loop and a repeat-until loop check opposite states. WHILE x < 10 continues while x is less than 10; the equivalent REPEAT … UNTIL x >= 10 continues until x reaches or exceeds 10. The loop body is the same; the condition in the UNTIL is the logical negation of the condition in the WHILE.
What is the Python equivalent of repeat-until?
Python has no built-in repeat-until or do-while keyword. The standard Python pattern uses while True combined with an if/break at the end:
while True:
number = int(input("Enter a number between 1 and 10: "))
if 1 <= number <= 10:
break # exit the loop when condition is met
print(f"You entered: {number}")
The while True creates an infinite loop; the break exits it. Placing the break at the end of the loop body (after the main code) mimics the post-condition behaviour of repeat-until.
An alternative Python pattern uses a flag variable:
valid = False
while not valid:
number = int(input("Enter a number between 1 and 10: "))
if 1 <= number <= 10:
valid = True
Both produce the same result. The while True / break pattern is more concise and commonly used in practice.
Frequently asked questions
Can a repeat-until loop run forever?
Yes — if the condition is never satisfied, the loop runs indefinitely. For example, REPEAT … UNTIL 1 = 2 would never exit because 1 = 2 is permanently false. In practice, this is called an infinite loop and is usually a bug, caused by either a logical error in the condition or a failure to update the variable the condition depends on. Good loop design always ensures there is a path through which the condition will eventually become true.
Is repeat-until the same as do-while in other languages?
Yes, they are the same concept. do { ... } while (condition) in languages such as Java and C++ executes the body first, then checks the condition — continuing as long as the condition is true (not false, as in GCSE pseudocode's UNTIL). The direction of the condition check differs by convention. The underlying behaviour — guaranteed first execution, condition check after — is identical. When you see do-while in coursework or A-level material, think "repeat-until with the condition logic flipped".
When should I choose a repeat-until loop over a while loop in an exam answer?
Choose repeat-until when the body of the loop must execute at least once before the condition can meaningfully be evaluated — typically for user input, menu systems, or games where at least one round must occur. Choose while when it is possible (and correct) to skip the loop entirely — for example, processing a list that might be empty. In many cases both structures work; the examiners award marks for correctly written pseudocode, so pick whichever you feel most confident writing without errors.
Does AQA use REPEAT … UNTIL or WHILE … ENDWHILE?
AQA's pseudocode specification includes both. WHILE … ENDWHILE is a pre-condition loop; REPEAT … UNTIL is the post-condition loop. Both appear in past paper questions and mark schemes. OCR and some other boards use slightly different syntax (such as while … end while), but the logical structure is the same. Always state which pseudocode convention you are using if your school's lessons use a different style from the exam board's official one.
Professor Turing can practise pseudocode loops with you — including repeat-until, while, and for loops — with Socratic questions at aitutors.me.