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.
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.
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?
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
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?
What can a program do with a variable?
What is the value of a variable?
In set speed to 12, which part is the name and which is the value?
A variable holds 6 and the program updates it to 9. What happens to the 6?
What does it mean to trace a variable?
How many columns does a trace table have?
A trace table keeps old values in its rows. Does the variable keep them too?
In a trace table, how can you spot that a variable was updated?
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 postedMore KS3 Computer Science topics
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.