Article
REPEAT UNTIL
A robot walks forward until it reaches a wall. You do not tell it "walk 7 steps", because the room might change. You tell it a stopping rule. The AP exam has a loop for exactly this.
REPEAT UNTIL
REPEAT UNTIL(condition) repeats its body until the condition becomes true. Before every pass, the computer checks the condition:
- If it is false, the body runs once more, and then the computer checks again.
- If it is true, the loop stops, and the program carries on after the closing brace.
count ← 1
REPEAT UNTIL(count > 3)
{
DISPLAY(count)
count ← count + 1
}
DISPLAY("done")Trace it. The "Stop?" column is the condition, checked before each pass:
| Check | count | Stop? (count > 3) | What happens |
|---|---|---|---|
| 1 | 1 | false | display 1, count becomes 2 |
| 2 | 2 | false | display 2, count becomes 3 |
| 3 | 3 | false | display 3, count becomes 4 |
| 4 | 4 | true | the loop ends |
The program displays 1 2 3 done. Here is the same thing in Python. Run it, then change the 3:
The condition is the opposite in Python
This is the point that costs marks. The two loops use opposite conditions for the same job:
- AP
REPEAT UNTIL(c): runs whilecis false, stops whencis true. - Python
while c: runs whilecis true, stops whencis false.
To change one into the other, flip the condition:
AP REPEAT UNTIL(...) | Python while ... |
|---|---|
count > 3 | count <= 3 |
count ≥ 3 | count < 3 |
count = 3 | count != 3 |
count ≠ 3 | count == 3 |
a > 0 AND b > 0 | a <= 0 or b <= 0 |
NOT(done) | done |
The always-correct way is to put NOT around the whole condition. REPEAT UNTIL(c) is the same as while not (c). The table just writes the NOT out. For AND and OR, flipping each part also swaps AND with OR (the equivalence rule from the logical equivalence lesson).
Check your understanding
Which Python condition makes while ...: do the same job as REPEAT UNTIL(x > 10)?
Zero passes, or never stops
It can run zero times. The condition is checked first. If it is already true, the body never runs:
n ← 10
REPEAT UNTIL(n > 5)
{
n ← n + 1
DISPLAY(n)
}Here n > 5 is true at the start, so nothing is displayed and n stays 10.
It can run forever. If the condition can never become true, the program never ends. Two usual causes:
- The body does not change the variable the condition uses.
- The variable jumps over the value you test with
=:
x ← 2
REPEAT UNTIL(x = 10)
{
x ← x + 3
}x goes 2, 5, 8, 11, 14... It never equals 10, so the loop never stops. Using x ≥ 10 instead stops it at 11. When a loop counts by more than 1, ≥ or > is safer than =.
Check your understanding
What happens when this code runs?
x ← 2
REPEAT UNTIL(x = 9)
{
x ← x + 2
}Worked example: saving up
You have 100 coins and add 20 each week. You stop when you have at least 150. How many weeks, and how many coins?
balance ← 100
weeks ← 0
REPEAT UNTIL(balance ≥ 150)
{
balance ← balance + 20
weeks ← weeks + 1
}
DISPLAY(weeks)
DISPLAY(balance)| Check | balance | Stop? (balance ≥ 150) | Body |
|---|---|---|---|
| 1 | 100 | false | balance 120, weeks 1 |
| 2 | 120 | false | balance 140, weeks 2 |
| 3 | 140 | false | balance 160, weeks 3 |
| 4 | 160 | true | stop |
It displays 3 160. Notice that the balance went past 150 to 160, because the check only happens before a pass, never in the middle of one. In Python:
Common mistakes
- Using the Python condition in
REPEAT UNTIL.REPEAT UNTIL(count ≤ 3)stops straight away whencountis 1, because1 ≤ 3is already true. - Counting the last check as a pass. The final check (the one that is true) does not run the body.
- Expecting the loop to stop in the middle of the body. The check is only before each pass.
- Testing with
=when the value can jump past it. - Forgetting that it can run zero times.
On the exam
You may be asked what a REPEAT UNTIL loop displays, how many times it runs, or why it never stops. Check the condition against the starting values first: if it is already true, the body runs zero times. Use REPEAT n TIMES when you know the count and REPEAT UNTIL when a condition decides it.
Key words
REPEAT UNTIL: AP loop that repeats its body until its condition is true.
Stopping condition: the condition that makes a loop end.
Zero passes: a loop whose condition is already true at the start never runs its body.