Mansori

If/Else: Run the Traffic Lights

About this lesson, for parents and teachers

A traffic light has no brain. It just follows a few if/else rules, over and over. Can you write rules that keep every car safe?

Ages 9–11 · about 7 minutes · builds on Variables Are Labeled Boxes · leads to Logic Gates: AND, OR, NOT, The Game of Life: Simple Rules, Complex Worlds

Try this

  1. Tap a coloured chip in the code to change it, then press Run.
  2. Make the light go RED, GREEN, YELLOW and back to RED. Watch the little diagram draw your rules as arrows.
  3. Run the buggy rules and watch what goes wrong. Then fix them.
  4. Give the side street its own light with an else, then predict the colour after 5 ticks.

How it works

An if statement checks a question that is either true or false, like light == RED. If it is true, the computer runs the indented line under it. If it is false, it skips to the next check. elif means "else, if": only check this one when the checks above were false. else catches everything left over.

The traffic light's program runs once every tick. Each time, exactly one branch matches the current colour, so exactly one rule runs and changes the colour. RED goes to GREEN, GREEN goes to YELLOW, YELLOW goes to RED, and then it starts again. Because the checks happen in order, the computer stops at the first branch that is true and skips the rest, even if a later check would also match.

Something that is always in one of a few states, and follows rules to move from one state to the next, is called a state machine. The diagram in the corner is a state machine drawing: circles are states and arrows are the rules. A red arrow shows a rule that is legal code but unsafe.

Code can run perfectly and still be wrong. light = RED after GREEN works, but drivers get no warning. Leaving out the else for the side street lets both streets go at once. Engineers test rules like this in simulators before real cars ever use them. Finding these mistakes before anyone gets hurt is called testing, and it is a huge part of a programmer's job.

Where engineers use this

Real traffic controllers, elevators, vending machines, washing machines and video game characters are all programmed as state machines. Engineers add safety rules so two crossing roads can never be green at the same time.

Words to know

Conditional
An if statement: code that only runs when a question is true.
else
The branch that runs when none of the if checks above it were true.
State
The situation something is in right now, like RED, GREEN or YELLOW.
State machine
A system that is always in one state and follows rules to move to the next state.

Talk about it

Goal: Write if/else rules that run a traffic light safely, and see what happens when a rule is wrong.

  • What would happen at a real crossing if both lights were green at once? Which rule stops that?
  • Can you think of another machine at home that has states? What are its states?
  • Why do you think yellow comes between green and red?

Without a screen: Cut out red, yellow and green paper circles and write the three rules on a card. One person is the light and must follow only the card, flipping to the next colour each time a clap sounds. Toy cars or people must obey. Then secretly change one rule and see what goes wrong.

Curriculum links: CSTA K-12 1B-AP-10 (create programs that include sequences, events, loops and conditionals) and 1B-AP-15 (test and debug a program)

Questions

What is an if else statement for kids?
It is a rule in code: if something is true, do this; else, do that. The computer checks the question and runs only one of the two branches.

What is a state machine?
A state machine is something that is always in exactly one state, like a traffic light being red, yellow or green. Rules decide which state comes next, and the machine moves from state to state forever.

What is the difference between if, elif and else?
if starts the checks. elif means "otherwise, if" and is only checked when the earlier checks failed. else has no question and runs when nothing above matched.