Unit 3: Programming · Repetition

Article

REPEAT UNTIL

8 min read

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.
AP pseudocode
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:

CheckcountStop? (count > 3)What happens
11falsedisplay 1, count becomes 2
22falsedisplay 2, count becomes 3
33falsedisplay 3, count becomes 4
44truethe loop ends

The program displays 1 2 3 done. Here is the same thing in Python. Run it, then change the 3:

Runs in your browser

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 while c is false, stops when c is true.
  • Python while c: runs while c is true, stops when c is false.

To change one into the other, flip the condition:

AP REPEAT UNTIL(...)Python while ...
count > 3count <= 3
count ≥ 3count < 3
count = 3count != 3
count ≠ 3count == 3
a > 0 AND b > 0a <= 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:

AP pseudocode
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:

  1. The body does not change the variable the condition uses.
  2. The variable jumps over the value you test with =:
AP pseudocode
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?

AP pseudocode
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?

AP pseudocode
balance ← 100
weeks ← 0
REPEAT UNTIL(balance ≥ 150)
{
    balance ← balance + 20
    weeks ← weeks + 1
}
DISPLAY(weeks)
DISPLAY(balance)
CheckbalanceStop? (balance ≥ 150)Body
1100falsebalance 120, weeks 1
2120falsebalance 140, weeks 2
3140falsebalance 160, weeks 3
4160truestop

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:

Runs in your browser

Common mistakes

  • Using the Python condition in REPEAT UNTIL. REPEAT UNTIL(count ≤ 3) stops straight away when count is 1, because 1 ≤ 3 is 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.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.