Session 4
Objectives
Understand selection using the if… do... statement
Understand that you can use an if statement inside a repeat loop
Use this knowledge to create short efficient programs to complete simple single routes
Resources | Vocabulary |
---|---|
• Levels 36 to 38 in Rapid Router • Resource sheets 1-4 • Interactive Whiteboard (IWB) • Video: 'Helen explains if else statements' | • General solution • Predict, evaluate • Else if |
Let's get started
Ask the students to recall what they learnt in the last session.
Show level 36 [fig S4.1] on the IWB with the following code blocks in the workspace:
Can we write a program that will drive the van along this route?
Can you explain which blocks of code to choose and why?
Create the code together and test it.
Give out resource sheet 1 [fig S4.2] to help the students discuss and test the algorithm, by tracing the route with the van card.
Would this work on any route?
Look at the van’s journey, when does the computer ask the if questions?
Practical
Give students a little time to test out their general algorithm on levels 36 to 38 of Rapid Router.
Then, bring them together to explain that they are now going to make their own maps to test the general rule. Demonstrate how to make your own route.
Mark the beginning and end. Ask the students to test the code that you have come up with on their routes using resource sheet 2 [fig S4.3].
Ask each student to save their route, take a screen shot and paste it into a text (e.g Word) document. Some will be able to add their explanation of why this general program works and save this to their portfolio.
Share and review
Notice that you don’t get the full score — why is this? Well, there is a neat way of asking lots of questions in one go… if we have lots of choices we can use the else if statement.
Explain using an example:
if it is hot do put on your sandals
else if it is cold do put on your shoes
else if it is rainy do put on your wellies
Show the general program to drive along any single route using this new code:
Demonstrate how to create this code by clicking the cog icon in the if block of code to drag across as many else if blocks as you need.
Can you explain what the computer will do in this loop, when the road turns to the right?
Explain that you will come back to the idea of using else if later on in Session 6. You could give out resource sheet 3 [fig S4.4] in pairs for students to discuss and understand the else if concept.
Note: There is a difference between the use of multiple if statements and the if… else if code.
The computer will run through the if statements in order, asking each question in turn and acting on the answer.
In the if… else if statement, the first if question is asked and only if that condition is not satisfied, does the else if question get asked.
Extension
Use resource sheet 4 [fig S4.5] unplugged activity for general algorithm using else if to consolidate this idea. Some students will then be able to test this algorithm on the routes they created and saved.
Follow-up unplugged activity
Look at the video: ‘Helen explains if else statements’ to help the students learn about the if… else statement, and discuss other examples in real life where you might use the if… else if statements.
Make up an algorithm together for a food sorting robot along these lines:
if item is fruit, place in green basket
else if item is dairy product, place in cool bag
else if item is cereal, place in box
Students can take turns to play the robot and sort the food types into the appropriate containers.
Last updated