A “repeat 10” loop counts. But often we don’t know how many repeats we need: keep asking until the password is right; keep moving until you touch the wall. For that, coders use a loop with a stopping condition.
What your child will learn
- Know that “repeat until” stops when its condition becomes true
- Know that a “forever” loop never stops by itself
- Trace a variable through a loop to find when it stops
- Spot a loop that can never end
Check, then repeat
A “repeat until” loop checks its condition before each round. If the condition is already true, the loop doesn’t run at all. If nothing inside the loop ever changes the condition, it will run forever.
Skipping past the finish
A classic bug: “repeat until x = 0” while x goes 5, 3, 1, −1… It jumps straight past 0, so the loop never ends. Using “x < 1” instead catches it. Text languages write the same idea the other way round, as “while x > 0”.
Example questions
Here are a few questions like the ones in the first quest. Can you solve them?
1. How many times does the loop run?
- 5
- 1
- 4
- 6
Show answer
Answer: 5. Count goes up by 1 each time, from 0 to 5: five rounds.
2. What is x when the loop stops?
- 0
- 4
- 3
- 2
Show answer
Answer: 3. x goes 1, 2, 3 — and at 3 the condition is true, so the loop stops.
3. How many times does the loop run?
- 4
- 0
- 2
- 3
Show answer
Answer: 3. Lives goes 2, 1, 0 — three rounds.
Tips for grown-ups
- Act out a loop: “clap until I say stop” versus “clap five times”.
- Trace loops with a table: write the variable’s value after every round.
- In Scratch, try “repeat until touching edge” and watch when it stops.