Week 8 · Unit 4: Variables in programming · 3 lessons
2
Variables in flowcharts
Computing 6: Unit 4, pp. 52–67
GoalRead and trace flowcharts that ask for input, store it in variables and use it in calculations and decisions.
New words
assign · to put a value into a variableuser input · data that a person types or chooses while a program runsvalue · the data that a variable holds at one momentcondition · a question in a decision, such as reply = 42
Explanation
Flowcharts show where a program uses variables. The step ‘set bags to the answer’ puts whatever the user typed into the variable bags. This is called assigning. Later steps can use the name bags in a calculation, such as 3 × bags. A decision can also use a variable. The condition ‘reply = 42?’ compares the value in reply with 42, and the flowchart follows the yes branch or the no branch. To trace a flowchart, write down the value of each variable after every step. The same variable names can be used again in a different program.
Worked examples
Step
bags
cost
start
—
—
ask: how many bags?
—
—
set bags to the answer
4
—
set cost to 3 × bags
4
12
say the cost
4
12
The apples flowchart multiplies the price of one bag, 3, by the number of bags. The table traces it when the user types 4.
The quiz flowchart stores the user’s reply in a variable and then tests it. If the user types 36, the condition reply = 42 is false, so the no branch runs and the program says ‘Try again!’. If the user types 42, it says ‘Correct!’.
A flowchart does not show how the variables are created. In Scratch you click make a variable before you write the code. The flowchart only shows how the variables are used.
Class activity
Be the computer (no computer): draw two boxes on paper, one labelled bags and one labelled cost. An adult plays the user and answers the question ‘How many bags of apples?’ with a number from 1 to 9. Write the answer in the bags box, rubbing out the old one. Work out cost = 3 × bags and write it in the cost box. Say the cost out loud. Do five rounds. Then swap, so you ask the questions and the adult writes in the boxes. If you have a computer or tablet, build the program in Scratch with the variables bags and cost.
Practice
1
The user types mango. What does the program say?
It says Good choice, mango, because the variable fruit holds mango.
2
What does the program say when the user types 10? What does it say when the user types 12?
For 10 it says small order, because 10 is not more than 10. For 12 it says big order.
3
Describe a flowchart that asks for a first number and a second number, stores them in variables a and b, sets total to a + b and says the total. What is the output for 15 and 27?
Start, ask for the first number, set a to the answer, ask for the second number, set b to the answer, set total to a + b, say total, stop. The output for 15 and 27 is 42.
4
Which variable is used in the cakes flowchart, and what is its data type?
The variable is cakes, and its data type is integer, because the user types a whole number.