Study CommonsRead · reason · practise

Home / Reasoning / Repeat Until: Loops That Stop When a Condition Is True

Reasoning & Coding · Coding

Repeat Until: Loops That Stop When a Condition Is True

Trace repeat-until loops step by step, count how many passes run, spot loops that never stop, and fix off-by-one bugs in counting programs.

Learning objectiveTrace a repeat-until loop with a trace table, decide how many times its body runs (including zero times), recognise a loop whose condition can never become true, and fix a loop that stops one step too early or too late.

The idea

In the robot-path lesson, Repeat 4 [move] meant 'do this exactly 4 times'. That works when you know the number in advance. But sometimes you do not. You shovel until the driveway is clear. You add snow to a snowball until it is big enough. A program can do the same thing with a repeat-until loop.

In this lesson a repeat-until loop is written like this: Repeat until n == 3 [n = n + 1]. The part after Repeat until is a condition, like the conditions in the if–else lesson. The commands inside the square brackets are the body of the loop.

Here is the rule. Before each pass, the computer checks the condition. If the condition is false, it runs the body once and then checks again. If the condition is true, the loop stops and the program moves on. So the body keeps running until the condition becomes true. (This is how the Repeat until block works in Scratch: it checks first.)

Because the check comes first, a repeat-until body can run zero times: if the condition is already true at the start, the body is skipped. And if the condition can never become true, the loop never stops. Programmers call that an infinite loop, and it is one of the most common bugs.

The best tool is the trace table from the variables lesson. Make a column for each variable and a column for the condition. Write a new row each time the condition is checked.

Worked examples

Example 1 · Trace and count the passes

Program: n = 0; Repeat until n >= 6 [n = n + 2]; say n. What does it say, and how many passes run?

  1. Start: n = 0. Check: is 0 >= 6? No, so run the body: n becomes 2. (Pass 1)
  2. Check: is 2 >= 6? No. n becomes 4. (Pass 2)
  3. Check: is 4 >= 6? No. n becomes 6. (Pass 3)
  4. Check: is 6 >= 6? Yes, so the loop stops. The program says n.

It says 6, after 3 passes.

Example 2 · Zero passes

Program: temp = -4; Repeat until temp < 0 [temp = temp - 1]; say temp. What does it say?

  1. Start: temp = −4.
  2. Check before the first pass: is −4 < 0? Yes, it is already true.
  3. So the body never runs, and temp stays −4.

It says −4. The body ran 0 times.

Example 3 · A loop that never stops

Program: n = 1; Repeat until n == 10 [n = n + 2]. Will it stop?

  1. Trace n: 1, 3, 5, 7, 9, 11, 13 ...
  2. n jumps from 9 straight to 11. It is never exactly 10.
  3. The condition n == 10 never becomes true, so the loop runs forever.
  4. Fix it by changing the condition to n >= 10, which becomes true when n reaches 11.

No. It is an infinite loop. Using n >= 10 makes it stop.

Example 4 · A robot that stops at the wall

The robot starts at S facing right. Program: Repeat until wall ahead [move 1 square]. How many squares does it move?

A 4-by-3 coding grid. The robot S is at the left of the middle row, then two open squares, then a wall square.S
A 4-by-3 coding grid. The robot S is at the left of the middle row, then two open squares, then a wall square.
  1. Check: is there a wall right ahead of S? No, so move 1 square. (1)
  2. Check: wall ahead? No, so move again. (2)
  3. Check: wall ahead? Yes, so stop.

It moves 2 squares and stops next to the wall.

Common mistakes

Thinking the body always runs at least once.

Why this fails: A repeat-until loop checks the condition first. If it is already true, the body is skipped completely.

Using == when the variable can jump past the value.

Why this fails: If n goes up by 2 from 1, it is never exactly 10. Use >= (or <= when counting down) so the loop still stops.

Stopping one step too early.

Why this fails: Repeat until n == 5 [say n; n = n + 1] stops before saying 5. To include 5, the condition must be n > 5 (or n == 6).

Confusing 'repeat until' with 'repeat while'.

Why this fails: Repeat until keeps going while the condition is false and stops when it becomes true. A while loop is the opposite: it keeps going while its condition is true.

Forgetting to change the variable inside the loop.

Why this fails: If nothing in the body changes n, the condition stays the same forever, so the loop never stops.

Foundation

Foundation practice

Check the condition before every pass. Stop as soon as it is true.

Trace

1. Program: n = 0; Repeat until n == 3 [n = n + 1]; say n. What does it say?

  1. 2
  2. 4
  3. 3
  4. 0
Count passes

2. In the same program (n = 0; Repeat until n == 3 [n = n + 1]), how many times does the body run?

  1. 3
  2. 2
  3. 4
  4. 1
Trace

3. Program: x = 10; Repeat until x < 5 [x = x - 2]; say x. What does it say?

  1. 5
  2. 6
  3. 2
  4. 4
Count passes

4. Program: snow = 0; Repeat until snow >= 5 [snow = snow + 1]. How many passes run?

  1. 4
  2. 5
  3. 6
  4. 1
Zero passes

5. Program: n = 7; Repeat until n > 5 [n = n + 1]; say n. What does it say?

  1. 8
  2. 6
  3. 7
  4. 5

Practice

Practice practice

Use a trace table with a column for each variable.

Trace two variables

6. Program: n = 0; total = 0; Repeat until n == 4 [n = n + 1; total = total + n]; say total. What does it say?

  1. 10
  2. 6
  3. 4
  4. 15
Share it out

7. Program: cookies = 20; Repeat until cookies < 6 [cookies = cookies - 6]; say cookies. What does it say?

  1. 8
  2. −4
  3. 0
  4. 2
Robot

8. The robot starts at S facing right. Program: Repeat until wall ahead [move 1 square]. How many squares does it move?

  1. 2
  2. 3
  3. 4
  4. 5
A 5-by-3 coding grid. The robot S is at the left of the middle row, then three open squares, then a wall square.S
A 5-by-3 coding grid. The robot S is at the left of the middle row, then three open squares, then a wall square.
Doubling

9. Program: x = 1; Repeat until x > 20 [x = x * 2]; say x. What does it say?

  1. 16
  2. 20
  3. 32
  4. 64
Will it stop?

10. Which program never stops?

  1. n = 0; Repeat until n == 5 [n = n + 2]
  2. n = 0; Repeat until n == 6 [n = n + 2]
  3. n = 0; Repeat until n >= 5 [n = n + 2]
  4. n = 10; Repeat until n < 5 [n = n - 2]
Count passes

11. Program: t = 12; Repeat until t == 0 [t = t - 3]. How many passes run?

  1. 3
  2. 5
  3. 12
  4. 4

Think

Think practice

Find the bug and explain why the tempting answer fails.

Fix the bug

12. Goal: say 1, 2, 3, 4, 5. Program: n = 1; Repeat until n == 5 [say n; n = n + 1]. It says only 1, 2, 3, 4. Which change fixes it?

  1. Start with n = 0.
  2. Change the condition to n > 5.
  3. Change the condition to n == 4.
  4. Swap the order of say n and n = n + 1.
Choose the loop

13. When is Repeat until a better choice than Repeat 5?

  1. When you know the exact number of passes
  2. When the program has no variables
  3. When you do not know in advance how many passes you need
  4. Always, because it is faster
Boundary

14. Program: n = 0; Repeat until n > 3 [n = n + 1]; say n. What does it say?

  1. 4
  2. 3
  3. 5
  4. 2
Explain

15. Program: n = 10; Repeat until n == 0 [n = n + 1]. Explain what goes wrong and give one fix.

Challenge

Challenge practice

Combine variables, conditions and loops. Show a trace table.

Shovel trips

16. A driveway has 13 cm of snow (an invented example). Program: snow = 13; trips = 0; Repeat until snow <= 0 [snow = snow - 4; trips = trips + 1]; say trips. What does it say?

  1. 3
  2. 5
  3. 13
  4. 4
Pattern

17. Program: a = 0; b = 1; Repeat until b > 10 [c = a + b; a = b; b = c]; say b. What does it say?

  1. 8
  2. 13
  3. 10
  4. 21
Write a loop

18. Write a repeat-until program that says the multiples of 3 from 3 to 30.

Answer key

  1. 1. 3
    n goes 0, 1, 2, 3. When n is 3, the condition n == 3 is true, so the loop stops and says 3. 2 stops one check too early, and 4 runs one pass too many.
  2. 2. 3
    The body runs when n is 0, 1 and 2: 3 passes. On the fourth check n is 3, so it stops. 4 counts the checks instead of the passes.
  3. 3. 4
    x goes 10, 8, 6, 4. When x is 4, x < 5 is true, so it stops. 5 is not reached because x jumps by 2, and 6 stops too early because 6 < 5 is false.
  4. 4. 5
    snow goes 0 to 5, one pass for each step: 5 passes. After the fifth pass, snow >= 5 is true. 6 would be one pass too many.
  5. 5. 7
    The condition is checked first: 7 > 5 is already true, so the body never runs and n stays 7. 8 assumes the body runs once before checking.
  6. 6. 10
    The passes add n = 1, 2, 3 and 4, so total = 1 + 2 + 3 + 4 = 10. 6 stops after n = 3, and 4 reports n instead of total.
  7. 7. 2
    cookies goes 20, 14, 8, 2. At 2, cookies < 6 is true, so it stops. 8 stops too early (8 < 6 is false), and −4 runs one extra pass. The answer is the remainder of 20 ÷ 6.
  8. 8. 3
    There are three open squares between S and the wall. After 3 moves, the wall is right ahead, so the loop stops. 4 would put the robot into the wall.
  9. 9. 32
    x doubles: 1, 2, 4, 8, 16, 32. 16 > 20 is false, so it doubles once more to 32, and 32 > 20 is true. 20 is never reached exactly, and 16 stops too early.
  10. 10. n = 0; Repeat until n == 5 [n = n + 2]
    n goes 0, 2, 4, 6 ... and is never exactly 5, so the condition never becomes true. The others stop: n == 6 is reached, n >= 5 becomes true at 6, and counting down by 2 from 10 reaches 4, which is less than 5.
  11. 11. 4
    t goes 12, 9, 6, 3, 0: four subtractions. 12 ÷ 3 = 4 gives the same answer. 5 counts the values instead of the passes.
  12. 12. Change the condition to n > 5.
    With n > 5, the loop still runs when n is 5, says 5, then stops at 6. Starting at 0 would say 0 first, and swapping the two commands would say 2 to 5. Using n == 4 stops even earlier.
  13. 13. When you do not know in advance how many passes you need
    Repeat 5 is perfect when you know the count. Repeat until fits jobs like 'shovel until the path is clear', where the count depends on what happens. Both can use variables, and neither is always faster.
  14. 14. 4
    3 > 3 is false, so the loop runs once more and n becomes 4; then 4 > 3 is true. Choosing 3 treats > as if it were >=.
  15. 15. n goes up (11, 12, 13 ...) and never reaches 0, so the loop never stops. Fix: change the body to n = n − 1.
    The body moves n away from the stopping value. Either count down with n = n − 1, or change the condition to match the direction the variable moves.
  16. 16. 4
    snow goes 13, 9, 5, 1, −3. After the fourth pass, snow <= 0 is true. 3 stops when 1 cm is still left; 13 ÷ 4 rounds down to 3, but the last centimetre needs one more trip.
  17. 17. 13
    Trace (a, b): (0, 1), (1, 1), (1, 2), (2, 3), (3, 5), (5, 8), (8, 13). 8 > 10 is false, so it runs again, and 13 > 10 is true. Each new number is the sum of the two before it.
  18. 18. n = 3; Repeat until n > 30 [say n; n = n + 3]
    It says 3, 6, 9, ..., 30 (ten numbers). Using n == 30 as the condition would stop before saying 30. Other correct versions are fine if a trace shows 3 to 30.

Related practice