Grade by GradeEssential student suppliesSign in
Applied Design, Skills, and Technologies ยท Grade 4 Design and Make: Working as a Design Team

Variables: A Program's Memory Box

BC ADST 4: use familiar tools BC ADST 4: choose technologiesAbout 45 minutes
Learning goals
  • I can explain what a variable does in a program.
  • I can trace a program step by step to find a variable's value.
  • I can write a short program that keeps count with a variable.

Words to know

variable
A labelled spot in a program's memory holding a value that can change as the program runs.
value
What is stored in a variable right now, such as the number 5.
set
A block that puts a starting value into a variable.
change by
A block that adds to a variable (or takes away, if the number is negative).
trace
To follow a program one step at a time and write down what happens.

A box with a label ๐Ÿงฎ

You already know loops, events and if-then blocks. Now meet the variable.

Picture a box with a label on it. The box holds one value, such as a number. The program can look inside the box and can swap what's inside.

  • set inside to 0 empties the box and drops a 0 in.
  • change inside by 1 adds 1 to what's there.
  • change inside by -1 takes 1 away.

Games use variables for points, lives and time left. Room 12 is helping at their school's winter fun day. The warming tent safely holds 12 people, so they want a counter that tracks how many people are inside. Their tool is a pocket-sized coding board, such as a micro:bit, with an A button and a B button.

when program starts
set inside to 0
show inside
when button A pressed
change inside by 1
show inside
if inside = 12 then
show "Full"
when button B pressed
change inside by -1
show inside
if inside = 0 then
show "Empty"

Room 12's tent counter. Press A when someone goes in, and B when someone comes out.

Tracing inside: start, A, A, A, B0start1after A2after A3after A2after B+1+1+1-1The box called inside holds one number at a time.
Following the value of inside as A is pressed three times and then B once.
Worked example

Trace the tent counter

The program starts. Then the buttons are pressed in this order: A, A, A, B, A, B, B, B. What is on the screen after the last press?

  1. Start: inside = 0.
  2. A, A, A: 0 โ†’ 1 โ†’ 2 โ†’ 3.
  3. B: 3 โˆ’ 1 = 2. A: 2 + 1 = 3.
  4. B, B, B: 3 โ†’ 2 โ†’ 1 โ†’ 0.
  5. After the last B, inside = 0, so the if-then is true and the screen shows "Empty".

The final value of inside is 0, so "Empty" appears. Everyone who went into the tent has come out.

Pressinside beforeChangeinside afterScreen
A0+111
B1โˆ’100, then Empty
A0+111
A1+122
B2โˆ’111
A trace table follows the variable one press at a time.

Reset at the start

Without set inside to 0, the counter might begin from an old number left over from last time, and every count would be off. When a program with a variable acts strangely, check the starting value first. That is a key debugging step.

Hands-on activity

Make a lap counter

On a computer or tablet: Open a block-coding app such as Scratch or MakeCode and create a variable named laps. Give it a starting value of 0 in the start event. Add 1 each time a key or button is pressed, and show laps. Add an if-then that shows "Done!" when laps reaches 5.

Unplugged: Write laps on a sticky note with a box under it. Write 0 in pencil. A partner calls "plus one" or "minus one" while you erase and write the new value. Compare your final value with your partner's list.

Puzzle

A variable starts at 12. Halve it. Add 4. Then subtract 7. What is the value now? ๐Ÿค”

12 โ†’ 6 โ†’ 10 โ†’ 3. The answer is 3!

Let's practise

1

What is a variable?

2

inside is 7. The program runs change inside by 1. What is inside now?

3

inside is 7. The program runs change inside by -1 three times. What is inside now?

4

Start at 0. Press A five times. What is inside?

5

Start at 0. Press A, then B. What does the screen show?

6

What job does set inside to 0 do at the very beginning?

More practice

1

From 0, the presses are A, A, B, A, A, A, B. What is inside now?

2

A game starts coins at 10, and you spend 1 coin on three different turns. Name the block that runs each turn and give the value at the end.

3

Start at 0. Press A nine times and B four times. What is inside?

4

Start at 0. Someone presses A 12 times. What does the screen show after the 12th press?

5

inside is 12, and someone presses A again. What does it show? Is that a problem?

Check your understanding

Quiz

1.

A variable is like...

2.

score holds 5. Then change score by 1 runs 4 times. What does score hold now?

3.

Start at 0. Press A, A, B, A, A. What is inside?

4.

When does Room 12's program show "Full"?

For parents and teachers

How to help at home: Play "human variable": write a name on a sticky note, call out changes, and have your child keep the value. Look at scores and lives in games your child plays and ask, "Where is that number stored? What changes it?"

Common mistakes: Forgetting the starting value, losing track during a long trace (a trace table helps), and mixing up "set to" (replace) with "change by" (add or take away).