Session 6
Traffic lights: understanding variables and the if... do... else if... statement
Objectives
Understand how to use if… else if... statements in a new context — traffic lights
Incorporate a wait command
Analyse how the traffic lights work within the app
Understand that the light colour is a variable
Resources | Vocabulary |
---|---|
• Levels 44 to 47 in Rapid Router • Resource sheets • Levels 44 to 47 from the Levels Guide • Video: 'Will discusses variables' | • Wait, variable |
Let's get started
Variables are used in programming languages to store a value that you might want to use later. You can change the value held in a variable and check its value. In the Traffic Lights levels in Rapid Router, traffic light is a variable that either contains red or green.
Show level 44 [fig S6.1] on screen. Explain that there are now some traffic lights on the road [fig S6.2]. We don’t know how they work. How can we find out?
Try driving along the road just using move forwards. Discuss the error message (this will say: you directed a van through a red light. Stick to the Highway Code!).
What did we notice about the traffic light colours? How do they vary? (They are just red and green).
What would we do in real life? (Stop if they are red and wait until they change to green).
Unplugged activity
Instruct a student to move around the class along a masking tape road. Ask students to hold up green/red cards to signify the traffic lights using Resource sheet 1 [fig S6.3].
Discuss what question the driver has to ask at the traffic lights, before moving forward safely through the lights. Explain the concept of a variable in programming i.e. it is something which can be more than one thing; something that can change the way it looks or its value.
What is it about a traffic light that is a variable? Can anyone think of some code that we have already used which might come in handy (if lights are red)?
Look at the list of command blocks of code, and notice the new traffic lights block where you choose the value of the variable colour.
What do you think this new block of code and the wait command will do?
Demonstrate how to use the traffic light variable and the wait command.
How do we get the computer to check the lights all the time?
What have we used before? (repeat until)
Lead a group discussion so that you come up with this code:
Practical
Give students a few minutes to test this out themselves on level 44 and 45.
Mini review
Bring everyone back together and choose a student to describe what they had tried at level 45.
Remind them the else if statement is a way of making the code neater.
What statements did we use to decide whether to move forward, right or left?
Look at level 46 [fig S6.4]. This is more like the wiggly roads we looked at before.
Can the students streamline their program using the else if blocks?
Can you explain how the else if code works?
Main activity
Students work on levels 44 to 47 individually or in pairs.
Provide screenshots of each level from the Levels Guide for those who need to trace out the sequence on paper, and resource sheet 2 for those who would find it helpful.
Share and review
Bring the students together and ask them to describe what they have learnt.
Can we work out an algorithm that will work on any single road, with any number of traffic lights?
Can you explain what a variable is?
Follow-up unplugged activity
Watch the video: ‘Will discusses variables’ about variables. Discuss what computer systems you have in school or at home which operate by monitoring the value of variables like light, temperature, movement.
Can you think of any computer systems in the supermarket which use variables? (till conveyor belts, automatic doors, fridges...)
Extension
For the more advanced pupils who completed Session 5 extension activities, set them level 48 [fig S6.5] as a challenge.
Can you create a program using repeat until at destination loop which gets the van to the house?
Last updated