Grade by GradeEssential student suppliesSign in
Applied Design, Skills, and Technologies ยท Design and Make: Grade 4 Design Teams

Coding with Variables: Keep Count

BC ADST 4: use familiar tools BC ADST 4: choose technologiesAbout 45 minutes
Learning goals
  • I can explain what a variable is in code.
  • I can trace a program to find the value of a variable.
  • I can write a simple program that counts with a variable.

Words to know

variable
A named place in a program that stores a value that can change.
value
The number or word stored in a variable.
set
To give a variable a starting value.
change by
To add to (or subtract from) a variable's value.
trace
To follow a program step by step and keep track of what happens.

A box with a name ๐Ÿ“ฆ

You have coded with loops, events and if-then. Now meet the variable.

A variable is like a box with a name on it. Inside the box is a value, like a number. The program can look in the box, and it can change what's inside.

  • set balls_out to 0 puts 0 in the box at the start.
  • change balls_out by 1 adds 1.
  • change balls_out by โˆ’1 takes 1 away.

Games use variables for scores, lives and timers. Room 7 wants to use one to count how many balls are out at recess. They will use a small coding board with two buttons, A and B (such as a micro:bit).

when program starts
set balls_out to 0
show balls_out
when button A pressed
change balls_out by 1
show balls_out
when button B pressed
change balls_out by -1
show balls_out
if balls_out = 0 then
show "All back!"

Room 7's ball counter. Press A when a ball goes out. Press B when one comes back.

The variable balls_out changes with each button press01232AAABstart+1+1+1โˆ’1A = a ball goes out. B = a ball comes back.
Tracing the variable: start at 0, press A three times, then B once.
Worked example

Trace the ball counter

The program starts. Then these buttons are pressed: A, A, B, A, B, B. What does the screen show at the end?

  1. Start: balls_out = 0.
  2. A: 0 + 1 = 1. A: 1 + 1 = 2.
  3. B: 2 โˆ’ 1 = 1. A: 1 + 1 = 2.
  4. B: 2 โˆ’ 1 = 1. B: 1 โˆ’ 1 = 0.
  5. After the last B, balls_out = 0, so the if-then is true. The screen shows "All back!"

balls_out ends at 0, and the screen shows "All back!" Every ball that went out came back.

Pressballs_out beforeChangeballs_out after
A0+11
A1+12
A2+13
B3โˆ’12
B2โˆ’11
A trace table keeps track of the variable one step at a time.

Always set a starting value

If you forget set balls_out to 0, the count might start from an old number and every answer will be wrong. Setting the starting value is one of the first things to check when you debug a program with a variable.

Hands-on activity

Build a counter

With a computer or tablet: In a block-coding app (like Scratch or MakeCode), make a variable called score. Set it to 0 when the program starts. Add 1 when a key is pressed. Show the score. Add an if-then that shows a message when the score reaches 10.

Unplugged: Draw a box labelled score on paper and write 0 in it with pencil. A partner calls out "plus one" or "minus one". Erase and write the new value each time. Check your final value with your partner.

Puzzle

A variable starts at 5. Double it. Subtract 3. Double it again. What is the value now? ๐Ÿค”

5 โ†’ 10 โ†’ 7 โ†’ 14. The answer is 14!

Let's practise

1

What is a variable?

2

balls_out is 4. The program runs change balls_out by 1. What is it now?

3

balls_out is 4. The program runs change balls_out by -1 twice. What is it now?

4

Start at 0. Press A, A, A, A. What is balls_out?

5

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

6

Why does the program need set balls_out to 0 at the start?

More practice

1

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

2

A game sets lives to 3. You lose a life twice. What block runs each time, and what is the final value?

3

Name two things in games that are often stored in variables.

4

Start at 0. Press A five times and B three times. What is balls_out?

5

Someone presses B when balls_out is already 0. What value will it show? Is that a problem?

Check your understanding

Quiz

1.

A variable is like...

2.

score is 7. The program runs change score by 1 three times. What is score?

3.

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

4.

When does Room 7's program show "All back!"?

For parents and teachers

How to help at home: Play "human variable": write a name like coins on a sticky note with a starting number, then take turns calling out changes (+2, โˆ’1). Use free block-coding apps such as Scratch or MakeCode together and look for variables in games your child already plays (scores, lives, timers).

Common mistakes: Forgetting to set a starting value, losing track when tracing many steps (a trace table helps), and confusing set to (replace the value) with change by (add to the value).