KS3 · Computer Science

Variables: storing and changing values

Your game shows score: 10. You grab a coin and it shows 15. So where did the 10 go?

Computer Science · Algorithms

Trace table — dry run the code

Each line sets a variable: it gives a named variable a value. Press Start, then Next step, and guess what score and lives will hold before each row appears.

1set score to 0
2set lives to 3
3set score to 10
4set lives to 2
5set score to score + 5
score
lives
Press Start to run the first line.
·

Ready when you are — step through one line at a time.

Computer Science · Programming

Name or value?

Pick a piece, then pick the box it belongs in.

Still to sort

Variable name (0)

What the program calls the variable, so it can get at the data.

Where the line is: The name tells the program which variable to use. It is not the data itself.

Value (0)

The data the variable holds at a particular time.

Where the line is: The value is the data being stored. It is not the label used to find it.

6 of 6 still to sort.

Three lines of a program: set cookies to 40, set speed to 12, set level to 2. Each line holds one name and one value. Can you tell which is which?

Computer Science · Programming

What does it hold now?

A program runs two lines: set coins to 4, then set coins to 9.

Right now, what does coins hold? Pick the idea closest to yours.
How sure are you?

Computer Science · Programming

Your turn to trace

Trace this program one line at a time: 1 set fuel to 9, 2 set distance to 0, 3 set fuel to 6, 4 set distance to 40, 5 set fuel to 3. What do fuel and distance hold after each line?

  1. After line 1: fuel holds 9 (distance is not set yet).
  2. After line 2: fuel holds 9, distance holds 0.
  3. missing step
Which line is step 3?

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Names, values, and what happens when a value changes

What you need to know

  • A variable is a named piece of data stored in a computer's memory.
  • Programs use variables to store a value, and the program can access that value and change it.
  • The value of a variable is the data it holds at a particular time.
  • Have a goYour game starts with health holding 100, and near the end health holds 40. So what is 'the value of health'?

    It depends when you ask: 100 at the start, 40 near the end.

    A variable's value is the data it holds at a particular time, so it can be different at different times.

  • A variable has a name and a value. The program uses the name to get at the value.
  • Have a goMo reads set pages to 300 and announces, with total confidence, that 'pages is the value and 300 is the name'. Help Mo out: which is which?

    pages is the name. 300 is the value.

    The name is what the program uses to get at the variable. The value is the data it holds at that time.

  • Update a variable and the new value replaces the value it held before.
  • The old value is overwritten, and the computer does not remember it.
  • Have a goA program sets bonus to 20 and later sets bonus to 5. Can the program still get the 20 back?

    No. The 20 is gone.

    The update overwrote the 20, and the computer does not remember it.

  • To trace a variable is to track the value it holds at each point in a program.
  • A trace table does this job, with one column for each variable.

The big picture

A variable is a named piece of data stored in a computer's memory. A program can read it and change it. When a variable is updated, the new value replaces the old one, which is overwritten. Tracing means tracking what each variable holds at every point in the program.

Key points

1A variable is a named piece of data stored in a computer's memory, used to store a value in a program.
2The value is the data a variable holds at a particular time, and the program can access it and change it.
3Update a variable and the new value replaces the old one. The old value is overwritten and the computer does not remember it.
4Tracing a variable means tracking its value at each point in the program. A trace table has one column for each variable.

Worked example

Problem

A program runs three lines: set seats to 30, then set seats to 28, then set seats to 25. What does seats hold after each line, and what happens to the earlier values?

⚠ Watch out

Reading an update as 'add this on'. set coins to 9 doesn't make 4 + 9. It replaces the 4 with 9. And don't mistake your trace table for the variable: the table keeps old rows because it's your notebook, but the variable only holds its latest value.

🧠

Memory hook

A variable is a labelled box. One value inside. Put a new one in, and the old one is gone for good.

✓

Check yourself

Cover the screen: set x to 2, set y to 5, set x to 9. Fill in a trace table with columns x and y. Which value has the computer forgotten?

Flashcards

(9)
What is a variable?
A named piece of data stored in a computer's memory, used to store a value in a program.
What can a program do with a variable?
Access its value and change it.
What is the value of a variable?
The data it holds at a particular time.
In set speed to 12, which part is the name and which is the value?
speed is the name. 12 is the value.
A variable holds 6 and the program updates it to 9. What happens to the 6?
It is overwritten. The new value replaces it, and the computer does not remember it.
What does it mean to trace a variable?
To track the value it holds at each point during a program.
How many columns does a trace table have?
One for each variable.
A trace table keeps old values in its rows. Does the variable keep them too?
No. The table is a record of what the variable held at each point. The variable itself holds only its current value.
In a trace table, how can you spot that a variable was updated?
Its cell changes from one row to the next.

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More KS3 Computer Science topics

See the full KS3 Computer Science curriculum →

How this lesson was checked. This KS3 Computer Sciencelesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 7 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.

Helpful guides for parents and students