Introducingvariables
Introducingvariables
Before we look at ways to introduce aspects of variables, let us examine the range of information a
programmer will need to fully understand variables in three simple examples. A simple score that
might be used in a quiz or game, a user input word such as a name that could be used to personalise
almost any project and a variable that changes within a loop.
my_num name
4 value
Text example
Number example
Stand up
Bow 3 times
Sit down
Wave 3 times
Say 3
We know that the harder we are forced to think about something then the more it will be likely to
make it into our long-term memory, so after acting out the teacher’s everyday algorithms with
variables a great next step is to write their own. This also helps the teacher to spot any pupils who
have faked understanding by copying their classmates’ actions earlier.
Well done if you spotted that the name doesn’t follow the naming convention. Spotting something
like this can be a great opportunity for a brief next-steps to improve their work conversation.
An easy first step uses text assigned variables in say or think commands or exploring all the ways you
can replace number values using a variable.
These are great opportunities for formative assessment questions. What is the value of your
variable? What have you named your variable? Why did you name it that? When the program gets
to that variable what will it read and what will it act on? Why have you chosen to initialise the
variable with that value?
Pupils enjoy writing their own examples and challenging their neighbours to act them out.
This is great for introducing the basic concept on changing a variable using maths, but it doesn’t
encourage pupils to think about variables that change in real contexts. Shuchi Grover2, a computing
education research scientist from Stanford University has worked with collaborators (Nicholas Jackiw
& Patrik Lundh of SRI) for some great research results exploring variables in a pictorial story context
with middle school pupils before coding in Scratch. Pictures on the left include a display of drinks
which a customer is purchasing, T-shirts for different ages, weather patterns over a day and a
basketball match. The questions prompt thought and discussion before planning how to use these in
a program.
Taken from Concepts before coding: non-programming interactives to advance learning of introductory programming concepts in middle
school Shuchi Grover et al 2019 with permission.
I adapted this idea to use with my year (9-10) year old pupils after we had worked with variables in a
more supported environment, see Parsons problems and modelling the four levels of abstraction
below. In pairs they had to answer the top four questions for three stories before choosing one to
convert into an algorithm and then code.
Thinking about the concept before programming using a story was really effective and something I
will definitely use again. You can see the ones they turned into projects here. My improved planner
can be found here.
Parsons problems can also be used within block based programming languages, asking pupils to
rearrange completed sections of code to complete a project.
You may notice in this counting program that we have chosen to avoid using the change variable by Scratch block. As we
used all four operations it was easier to teach all of them using varable = variable which is much closer to later text-based
programming examples such as Python.
PRIMM
Primm is a variation on Use, modify, create developed by Sue Sentence, chief learning officer for the
Raspberry Pi foundation. PRIMM stands for Predict, run, investigate, modify, make. Pupils are
encouraged to think deeply about the programming and predict what it might do before running it.
Investigation might involve lots of activities designed to maximise pupils understanding. These could
include
‘trace through the code, comment the code, answer questions about it, label particular concepts,
highlight it, draw the flow of control, etc.’3
Although designed for secondary pupils it can be adapted and used with primary pupils. You can find
out more about it here.
We used these levels when modelling writing a program to calculate the perimeter of an equilateral
triangle.
Idea Level
Create a program to calculate the perimeter of an equilateral triangle, where all three sides are the
same length, by asking the user to input the length of one side and multiplying that length by three.
Planning Level
In the planning level we identified the key variables such as the perimeter_of_eq_tri and the
length_of_one_side as well as the maths we would need to create to calculate the perimeter of an
equilateral triangle length_of_one_side x by 3.
We also decided to initialise both variables to 0
Assign 0 to length_of_one_side
Assign 0 to perimeter_of_eq_tri
Ask the user what the length of one side of their equilateral triangle is?
Assign their answer to length_on_one_side variable
Multiply_length_of_one_side by 3 and assign result to perimeter_of_eq_tri
Say the perimeter_of_eq_tri
Programming level
In this we demonstrated turning each line of the algorithm into Scratch code.
Execution level
Next steps
Pupils then created a similar program to calculate the perimeter of a regular 2d shape of their
choosing. Pupils subsequent work definitely benefitted from seeing the process modelled. Making
them aware that they would need to observe closely during the process as they would be building a
similar program themselves kept them very focussed. Over subsequent weeks this support was
gradually reduced until pupils were creating all parts of the process themselves. This gradual fading
of support is an important principle of cognitive load theory which you can read more about here.
My variables cheat sheet with algorithm and coding examples has also been useful for pupils as a
reference when reducing scaffolding.
Conclusion
In conclusion, the opening table outlines the amount of information pupils need to know to fully
understand how variables work. If you are using block-based programming, then you can clearly see
how much less pupils need to know by looking at all the additional red text needed to understand
variables in text-based programming. Shuchi Grover et al.’s (2019) paper is called ‘Concepts before
coding’ and it is an important principle that my own informal findings teaching programming to
primary pupils bear witness to. Leaving the programming process to learn about and explore an idea
can seem like a loss of precious programming time but as it develops deeper understanding and
enables greater agency it is worth it.
References
1 See Article What is in the box Hello World 7th Edition by Jane Waite, Felienne Hermans and
Efthimia Aivaloglou
2 Shuchi Grover, Nicholas Jackiw & Patrik Lundh (2019): Concepts before coding: non-programming
interactives to advance learning of introductory programming concepts in middle school, Computer
Science Education, DOI: 10.1080/08993408.2019.1568955
4 Jane Waite, Abstraction in Action: K-5 teachers' uses of levels of abstraction, particularly the design
level, in teaching programming, 2017 Jane names the levels as shown above but calls the last level
running the code