Lite / Discovering How the World Works / Week 23
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'?
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.
Predict
x starts at 0. Repeat until x ≥ 10: x = x + 3. How many times does the body run?
- 3
- 4
- 10
Guess first, then reveal
4 times: 0 → 3 → 6 → 9 → 12. After 3 passes x is 9, which is not yet ≥ 10, so it goes once more and overshoots to 12. Overshooting the limit like this is very common, and engineers have to plan for it.
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.
What the interactive shows (text version)
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.
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
Intuitive
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.
Technical
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.
Mathematical
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.
Applied
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.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
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?
Reflect
What's something you do 'until' something happens? What would make you stop?
Write your answer on paper or in your own notes. Lite never stores what you write.