AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Variables hold data

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.

Startask: how many bags of apples?set bags to the answerset cost to 3 × bagssay the costEnd
Worked examples
Stepbagscost
start——
ask: how many bags?——
set bags to the answer4—
set cost to 3 × bags412
say the cost412
The apples flowchart multiplies the price of one bag, 3, by the number of bags. The table traces it when the user types 4.
Startask: what is 6 × 7?set reply to the answerreply = 42?yesnosay: Correct!say: Try again!End
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?
Startask: favourite fruit?set fruit to the answersay: Good choice, and the fruitEnd
2
What does the program say when the user types 10? What does it say when the user types 12?
Startask: how many cakes?set cakes to the answercakes > 10?yesnosay: big ordersay: small orderEnd
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?
4
Which variable is used in the cakes flowchart, and what is its data type?