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

Variables and values

Week 8 · Unit 4: Be a storyteller · 3 lessons
1

Boxes with names

Computing 5: Unit 4, pp. 44–57
GoalExplain what a variable is, how it differs from a constant and how an algorithm uses one.
New words
variable · a named place in a program that stores a value that can changevalue · the number or the text that a variable holds at a momentconstant · a value that does not change while the program runsstore · to keep data so that the program can use it later
Explanation

A variable is like a labelled jar. The label is the name of the variable, and the paper inside is its value. When the program runs, it can take out the old value and put in a new one. Variables can store numbers, such as a score or a count, or text, such as a name or a word. A constant is a value that does not change while the program runs, for example the number 4 for the sides of a square. Variables let a program deal with different answers each time it runs. An algorithm that asks for something must store the answer in a variable if it wants to use it again.

food = ricefood = pears
Worked examples
StepInstruction
1Ask: ‘What is your favourite food?’
2Store the answer in the variable Food
3Say ‘I like ’ + Food + ‘ too!’
This algorithm uses a variable called Food. In step 2 the answer is stored in it. The first time, the user types ‘rice’. The second time, the user types ‘pears’. The variable has a different value each time, but its name stays the same.
whenclickedaskWhat is your favourite food?and waitsetfoodtoanswersayjoinjoinI like food too!for2seconds
The same algorithm in Scratch. The reporter (answer) holds what the user typed. The block ‘set food to (answer)’ copies it into the variable. One join block sits inside another, so three pieces of text are put together. If the user types ‘rice’, the sprite says ‘I like rice too!’.
ThingVariable or constant?
the player’s scorevariable
the days in a weekconstant
the name typed by the playervariable
the text ‘Game over’constant
Look at these things from a game. The player’s score goes up as they play, so it is a variable. The number of days in a week is always 7, so it is a constant. The name typed by the player is a variable, but the text ‘Game over’ in a say block is a constant.
Class activity

Jar game (a cup, paper slips and a pencil, 2 players). Label a cup ‘Food’. This is the variable. One player asks, ‘What is your favourite food?’. The other writes the answer on a slip and puts it in the cup. This is the value. Now the first player reads the slip aloud in a sentence, such as ‘I like … too!’. Run the game again with a different answer, but first take out the old slip. What changed, the label or the value? Why must the label stay the same?

Practice
1
Which of these are variables? (a) the score in a game, (b) the number of sides on a triangle, (c) a name typed in by the player, (d) the word ‘Hello’ typed into a say block.
2
The user types ‘noodles’ when the program runs. What does the sprite say? What is the value of the variable food?
whenclickedaskWhat is your favourite food?and waitsetfoodtoanswersayjoinjoinI like food too!for2seconds
3
Say whether each variable stores a number or text: age, city name, price, favourite colour.
4
Explain in your own words the difference between a variable and a constant. Give one example of each.