This lesson can be used by Mathematics teachers keen to include programming and logical thinking as part of their course. However, it is suggested that students complete The Basics page to familiarise themselves with relevant skills on which this lesson builds.
Use the context of ordering food for lunch to explain (in a practical way) the use of branching.
You and your friends are having lunch at a local food court.
You know you have enough money for at least the small plate. How do you decide what to order? Write down some conditions. These are questions that result in a yes or no answer, for example:
After ordering your food, you discover that drinks cost $2.95. Write down some conditions for this second decision.
sugree/flickr, Creative Commons BY-SA 2.0
BEGIN If I am very hungry and myMoney >= 10.95 then Say “I’ll have the large plate.” If I am thirsty and myMoney >= (10.95 + 2.95) then Say “And a drink too, please.” End If Else Say “I’ll have the small plate.” If I am thirsty and myMoney >= (8.95 + 2.95) then Say “And a drink too, please.” End If End If END
In this lesson, students will:
As a class, or in teams, design the calculator program as a flowchart. Effective designs may vary, for example, the program might ask for the operation first, in the middle, or last.
Here is one solution:
Image: Flow chart for calculator (simplified)
In the following revised flowchart, we break the big decision into four yes or no decisions. This is closer to how we will code the program.
Image: Flow chart for calculator
BEGIN Display “Choose your operation - a for add, s for subtract, m for multiply, d for divide:” operation ← input from user Display “Enter the first number:” firstNumber ← input from user Display “Enter the second number:” secondNumber ← input from user If operation = “a” then Display “The result is “, firstNumber + secondNumber Else If operation = “s” then Display “The result is “, firstNumber - secondNumber Else If operation = “m” then Display “The result is “, firstNumber * secondNumber Else If operation = “d” then Display “The result is “, firstNumber / secondNumber End if END
For more on setting up and choosing a language, see Setting Up.
Decision making is called branching or conditionals.
In general-purpose programming, we do this with IF, THEN and ELSE code structures.
BEGIN a ← 20 Display a b ← 14 Display b c ← a + b If c > 34 then Display “You win $1 000 000!” Else Display “Better luck next time.” End If If (a + c) = 54 then Display “YOLO” End If END
The following video is a real-world example of how branching is used in a more advanced program. If there is connection to the internet ( if (response.status_code == 220) then proceed, otherwise print(' no connection') and the program stops.
It’s time to code our calculator. The video above covers the entire process of building the code.
Modify your calculator program to allow a fifth operation: power. If the user enters ‘p’ for the operation, the first number is raised to the power of the second number.
eg: if first number is 5 and second number is 3, result is 53 = 125
Note: there is no power block (xy) in Scratch.
These challenges use the skills covered so far. By writing or modifying their own programs, students have an opportunity to demonstrate Application and Creation.
Hi, what’s your name? Dani You’re the best, Dani! Hi, what’s your name? Wei Welcome, Wei!
Challenge early finishers to add a second question about the number of Wi-Fi devices in their home, then give a smart comment based on the value.
Code a maths quiz, for example:
What is 2 + 5? 7 That’s right! What is 10 – 5? 5 That’s right! What is 3 * 5? 25 Incorrect. Your total score is 2 out of 3.
Here’s a way to break down this challenge into smaller chunks: