Unit 6: Loops That Watch
Keep going until something changes.
Unit 6 of 8 in Robotics coding for kids. Its 4 lessons are While, Until You Get There, Round the Edge and The Maze — below is everything each one explains, and a question or two from it to try.
Every program on this page was driven round the simulated room before it shipped, and prints exactly what it says it prints.
This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.
⏳ While
Keep going while…
for needs to know how many times before it starts. while does not — it checks something before every pass and keeps going as long as that stays true.
So this creeps forward in 5 cm nudges and stops as soon as the wall is close, without anyone measuring the room first.
Python
while robot.distance() > 20:
robot.forward(5)
robot.say(robot.distance())
It prints
17.0
Try it yourself
Why does it stop at 17 rather than exactly 20?
- It checks before each nudge, and the last 5 cm nudge took it from 22 to 17
- The sensor is wrong by 3
- while always overshoots by 3
- It stopped because it bumped
What happens if the thing a while is waiting for never becomes false?
- It keeps going until the robot runs out of power, and the robot says so
- Python skips the loop
- It runs exactly 100 times
- The robot bumps and stops
Answer them in the app
🎯 Until You Get There
Waiting for the flag
robot.at_goal() is True once the robot is standing in the green circle. Put not in front and you have "keep going until you arrive".
The robot has no idea how far away the flag is. It does not need to.
Python
while not robot.at_goal():
robot.forward(10)
robot.say("Made it")
It prints
Made it
Try it yourself
The flag is 140 cm up the room and its circle has a 16 cm radius. How many nudges does this take?
Python
steps = 0
while not robot.at_goal():
robot.forward(10)
steps = steps + 1
robot.say(steps)
What does not do in while not robot.at_goal():?
- It flips it: keep looping while the robot is not there yet
- It stops the loop immediately
- It makes the robot drive backwards
- Nothing — it is optional
Answer them in the app
🧱 Round the Edge
A loop inside a loop
Put a while inside a for and you get "do this four times, and each time keep going until…".
Here the inner loop runs the robot up to a wall, and the outer one turns it and does the whole thing again. Four walls, one lap of the room — and it works in a room of any size.
Python
for wall in range(4):
while robot.distance() > 12:
robot.forward(5)
robot.right(90)
robot.say(robot.x(), robot.y())
It prints
20.0 180.0
Try it yourself
Which line belongs to the outer loop, not the inner one?
- robot.right(90) — it runs once per wall, after the inner loop has finished
- robot.forward(5)
- while robot.distance() > 12:
- All of them belong to both
Answer it in the app
🏆 The Maze
One rule, any maze
A maze looks like it needs a route. It does not — it needs a rule, applied over and over:
If something is close in front, turn towards whichever side has more room. Then edge forward.
That is four lines, and it never mentions this maze. Watch it find its own way out.
Python
while not robot.at_goal():
if robot.distance() < 20:
if robot.distance_left() > robot.distance_right():
robot.left(90)
else:
robot.right(90)
robot.forward(5)
robot.say("Out!")
It prints
Out!
Try it yourself
What can a while loop do that a for loop cannot?
- Stop when something happens, without knowing beforehand how many turns that takes
- Run more than ten times
- Move the robot
- Contain an if
Answer it in the app