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.
Worked examples
Step
Instruction
1
Ask: ‘What is your favourite food?’
2
Store the answer in the variable Food
3
Say ‘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.
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!’.
Thing
Variable or constant?
the player’s score
variable
the days in a week
constant
the name typed by the player
variable
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.
(a) and (c) are variables, because their values change. (b) and (d) do not change, so they are constants.
2
The user types ‘noodles’ when the program runs. What does the sprite say? What is the value of the variable food?
The sprite says ‘I like noodles too!’. The variable food holds the text ‘noodles’.
3
Say whether each variable stores a number or text: age, city name, price, favourite colour.
Age: a number. City name: text. Price: a number. Favourite colour: text.
4
Explain in your own words the difference between a variable and a constant. Give one example of each.
A variable can change while the program runs, such as a score. A constant never changes, such as 4 sides of a square.