Repeat until: loops that decide when to stop
You don't know how many spoons of sugar a cup of tea needs until you taste it. How does a machine 'taste'?
About 25 min · 3 goals · Checked by automated tests; not yet reviewed by an educator
1.Why should I care?
Most real loops don't know in advance how many times to run. A robot drives forward until it reaches a wall; a kettle heats until it boils; a washing machine fills until the water level sensor says full. Repeat-until joins loops and conditions into one powerful idea.
- ◇ Explain how a repeat-until loop uses a condition to stop.
- ◇ Count how many passes a repeat-until loop makes.
- ◇ Spot a loop that would never stop.
2.Predict
x starts at 0. Repeat until x ≥ 10: x = x + 3. How many times does the body run?
Guess first. Being wrong here is how the surprise works.
3.Discover
Switch to 'Repeat until'. Start at 0, add 3, stop when x reaches 10. Step through it. Then set the start above the limit and see how many times the body runs.
Loop tracer
A loop repeats the same instructions. Step through one pass at a time and fill in the trace table, the way engineers check a loop by hand.
x = 0
repeat 4 times:
x = x + 3| Pass | x before | x after |
|---|---|---|
| No passes run yet. | ||
After 0 passes, x = 0.
Can't use the interactive? Read a description instead
The loop tracer can be replaced by this trace for x = 0, repeat until x ≥ 10, x = x + 3: pass 1, 0 → 3; pass 2, 3 → 6; pass 3, 6 → 9; pass 4, 9 → 12. Stops after 4 passes at 12. If x starts at 25 the condition is already true and the body runs 0 times.
4.Understand
Check, then repeat
A repeat-until loop checks a condition before each pass. If the condition is true, it stops. If not, it runs the body and checks again. The number of passes depends on what happens, not on a fixed count.
Something must change
The body must change something the condition depends on: x grows, the robot moves closer, the water rises. If nothing changes, the condition never becomes true and the loop runs for ever: an infinite loop, one of the most famous bugs.
Overshoot and zero passes
Two edge cases to test. Overshoot: if x jumps in big steps, it may pass the limit (9 → 12, not 10). Zero passes: if the condition is already true at the start, the body never runs at all.
Explain it another way
Filling a glass: pour until it's full. You don't count the seconds; you watch the level and stop when it reaches the top.
A condition-controlled loop (a while loop in most languages) tests its condition each time round. 'Repeat until done' is the same as 'while not done'. Its body must make progress towards the stopping condition.
Starting at a and adding s until x ≥ L, the number of passes is the smallest whole number n with a + n·s ≥ L. For a = 0, s = 3, L = 10: n = 4, because 3 × 3 = 9 < 10 but 4 × 3 = 12 ≥ 10.
A lift's levelling routine is a repeat-until loop: creep the cabin until the floor sensor says it's within a few millimetres of the floor. It doesn't know in advance how many tiny moves that takes.
Worked example 1
A tank holds 2 litres of water. A pump adds 5 litres a minute. Repeat until the water ≥ 20 litres. How many minutes does it run?
2 → 7 → 12 → 17 → 22. After 3 minutes it's 17 (not enough), after 4 it's 22 ≥ 20. 4 minutes, and it overshoots by 2 litres, so engineers would leave room at the top of the tank.
Common mix-ups
- A repeat-until loop always runs at least once.
✓ This kind checks first, so it can run zero times. (Some languages also have a check-after loop that always runs once.) - The loop stops exactly at the limit.
✓ It stops at the first value that meets the condition, which may be past the limit.
5.Practice
Practice, not a test. Wrong answers get a hint and another try.
Practice 1 · a fresh version every time
A robot's counter starts at x = 4. The loop is: repeat until x ≥ 21: x = x + 2. How many times does the loop body run?
Practice 2 · a fresh version every time
A robot's counter starts at x = 1. The loop is: repeat until x ≥ 12: x = x + 5. How many times does the loop body run?
Practice 3
Repeat until distance < 10: beep. The robot isn't moving. What happens?
Practice 4
x starts at 25. Repeat until x ≥ 20: x = x + 2. How many times does the body run?
6.Apply it
Write a repeat-until loop for a robot that drives towards a wall, checking its distance sensor each pass and moving 5 cm at a time. Start 37 cm away and stop when closer than 10 cm. How many passes, and where does it stop?
7.Reflect
Concepts: Decisions and loops · Variables, types and expressions