Lite / Discovering How the World Works / Week 23
The forever loop inside every robot
Infinite loops are a bug, right? So why does every traffic light controller run one on purpose?
Why should I care?
A robot that stops checking its sensors is a robot that crashes. Every controller, from a thermostat to a self-driving car, is a loop that never ends: sense, think, act, again. Seeing that loop is the key to understanding how intelligent machines stay intelligent.
- Explain why control programs run in a forever loop.
- Combine a loop with sense–think–act and a condition.
- Describe a machine's cycle of states that repeats.
Predict
A traffic light runs green → amber → red → green… Where does the program end?
- After one cycle
- After 24 hours
- It doesn't end: it repeats as long as it's switched on
Guess first, then reveal
It doesn't end. A controller's job is never finished: there's always another car, another second, another reading. So it runs a deliberate forever loop. The bug is only when a loop that was *supposed* to stop doesn't.
Discover
Judge six moments at the crossroads as safe or a conflict, then say what the lights must show next. Notice how the cycle wraps round from the last state back to the first, for ever.
What the interactive shows (text version)
The traffic activity can be replaced by this description: at a crossroads, north–south and east–west roads take turns. A safe cycle is: north–south green, north–south amber, all red, east–west green, east–west amber, all red, walk, flashing don't-walk, all red, then back to the start. It is a conflict if one road shows green or amber while the other shows green, or if walk shows while any road has green.
Understand
Forever on purpose
A control program looks like this:
forever: sense → think → act
It never finishes because the world never stops changing. A forever loop is only a bug when you wanted the loop to stop.
Conditions inside loops
Inside the forever loop sit IF rules: forever: read temperature; IF too cold THEN heater on ELSE heater off. The loop makes sure the rule is checked again and again; the rule decides what to do each time.
Cycles of states
Some machines step through a fixed list of states and wrap round: green, amber, all red, other road green, and so on. The traffic light's forever loop moves from state to state, with rules saying when each change is allowed and what must never be on at the same time.
Explain it another way
Intuitive
Your heart is a forever loop: beat, rest, beat, rest, for your whole life. It also adjusts as it goes: faster when you run, slower when you sleep.
Technical
Embedded controllers usually have a 'main loop' that runs until power is removed. Each pass reads inputs, updates the current state and writes outputs. A state machine inside it decides which state comes next.
Applied
A robot vacuum's main loop runs many times a second: read bump, cliff and wheel sensors; update its map; choose a direction; set wheel speeds. It only stops when you switch it off or it docks to charge.
Worked example 1
Write the forever loop for a night light.
forever: read the light sensor; IF light level < 30 THEN lamp on ELSE lamp off. Each pass takes a fraction of a second, so when the sun sets the lamp turns on almost immediately, and it turns off again at dawn.
Common mix-ups
All infinite loops are bugs.
✓ Controllers run forever loops on purpose. It's a bug only when a loop that should stop doesn't.A robot checks its rules once when switched on.
✓ It checks them on every pass of its main loop, many times a second.
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 forever loop for a pedestrian crossing: the light stays green for cars until the button is pressed, then goes amber, then red with the walk sign, then back to green. Which rule would you add so that nobody can ever see walk while cars have green?
Reflect
Which machines around you are running a forever loop right now?
Write your answer on paper or in your own notes. Lite never stores what you write.