Using Variables in Programs
This KS3 computing lesson explores Variables and Data Types in Programming. Students learn how variables store changing values, use meaningful variable names and work with common data types such as integers, strings and Boolean values. It provides a clear introduction to the topic and can be used for classroom teaching, homework or revision.
Using Variables in Programs
This KS3 computing lesson explores Variables and Data Types in Programming. Students learn how variables store changing values, use meaningful variable names and work with common data types such as integers, strings and Boolean values. It provides a clear introduction to the topic and can be used for classroom teaching, homework or revision.
Variables in Programs, KS3 Computing, Computer Science, Programming, Data Types, Integers, Strings, Booleans, Variable Assignment, Constants, Code Tracing, Python, MCQs, Open Questions, Polls, Emoji Polls, Exit Tickets
Learning goal
I can explain what a variable is in programming and use variables to store, name and change values in a simple program.
Deze slide heeft geen instructies
What are some words you think are similar to "variable"?
Use this to surface prior ideas such as storing, name, score, value, change, text, number, or memory. Briefly pick out any misconceptions before teaching.
What is a variable?
A variable is a named storage place in a program
It holds a value that the program can use later
The value can stay the same or change while the program runs
Keep the definition simple and consistent: a variable has a name and stores a value. Avoid low-level memory explanations at this stage.
Why variables are useful
A game can store a player's name
A quiz can keep the current score
A program can save an answer from the user
A calculation can store a result and use it again
Link each example to something students know: games, quizzes, calculators, or login screens. Stress that without variables, the program would not remember these values.
A simple example
`score = 0`
The variable `score` starts with the value `0`. Each time the player earns points, the program can change `score`.
You can read the code aloud as 'score becomes zero'. If useful, mention that different programming languages use slightly different symbols, but the idea is the same.
Name and value
A variable has two important parts:
name - how the program refers to it
value - the data stored inside it
In `playerName = "Amira"`, `playerName` is the name and `"Amira"` is the value.
Students may confuse the variable with the value. Reinforce that the name stays the label; the value is what is currently stored.
Different kinds of values
Variables can store different types of data
Common simple types are text, numbers, and Boolean values
The type affects what the program can do with the value
You do not need to introduce formal type systems in depth. The key idea is that not all values are the same kind of data.
What is a variable in a program?
A named storage place
A type of computer
A coding mistake
A game level
Checks the core definition from slides 2 to 5. Distractors are simple but clearly wrong for A2 learners.
Complete the idea
A variable has a name and a value. The name is the label used by the program. The value is the data stored inside it, such as score or playerName.
Reinforces the difference between the variable name and the value, echoing the wording on slides 4 and 5.
Text, numbers, Boolean
What they mean
Text stores words or symbols
Number stores amounts used in maths
Boolean stores `true` or `false`
Text - `playerName = "Leo"`
Number - `score = 12`
Boolean - `gameOver = false`
Examples
Point out that text often appears inside quotation marks in many languages. Boolean values are useful for yes/no states such as game over, logged in, or answer correct.
Using the right kind of value
Use text for names and messages
Use numbers for scores, ages, totals, and calculations
Use Boolean values for choices or conditions that are only true or false
A quick contrast helps here: a score should be a number because the program may add to it; a player's name should be text because it is displayed, not added.
Which variable should store true or false?
gameOver
playerName
highScore
ageTotal
Tests whether students can match Boolean use to a sensible example from slides 7 and 8.
Choose the right value types
Use text for names and messages. Use numbers for scores and calculations. Use Boolean values for choices that are only true or false.
This mirrors the exact contrasts taught in slides 7 and 8.
Choosing sensible names
A good variable name tells you what the value means
Clear names make code easier to read
`score`, `playerName`, and `livesLeft` are better than `x`, `a`, or `thing`
Explain that good names help both the writer and anyone else reading the program later. This is about clarity, not just correctness.
Rules for good variable names
Make the name match the job of the variable
Keep it short but clear
Avoid spaces in most programming languages
Be consistent, such as `playerName` and `playerScore`
You can mention that naming rules vary slightly between languages, but spaces are usually not allowed. Keep the focus on meaningful, readable names.
Poor names and better names
`x = "Mia"` → not clear what `x` means
`name = "Mia"` → clearer
`playerName = "Mia"` → even clearer in a game program
Show that the 'best' name depends on context. In a game, `playerName` is clearer than the more general `name`.
Which is the best variable name in a game?
thing
playerName
a
x
Checks the idea of clear, sensible names from slides 9 to 11.
Why is playerName better than x?
Expected elements: the name should match the job, be clear, and fit the context. Good examples could include score, livesLeft, playerName, or gameOver. Poor examples include x, a, or thing.
Assigning a value
When a program assigns a value, it stores something in a variable.
`lives = 3`
Now the variable `lives` stores the number `3`.
Use the word 'assign' consistently. Students often say 'equals' in a maths sense; here it means giving a value to a variable.
Updating a value
A variable can change.
`score = 10`
`score = score + 5`
The new value of `score` becomes `15`.
This is a key misconception point: the second line does not mean 10 equals 15. It means 'take the old value of score, add 5, then store the new result back in score'.
How change happens in a program
Input or event → program updates variable → new value is stored
Example:
player answers correctly
program adds 1 to `score`
`score` now holds the new total
This slide shows the process rather than syntax. Connect it to quiz apps, games, and simple calculators.
Changing a value after input
`playerName = input`
If the user types `Sam`, the variable `playerName` stores `"Sam"`. The program can then use that name later.
Keep this language-neutral. The exact code for input differs by language, so treat this as pseudocode to show the idea of storing user input.
After score = 10, what is score after score = score + 5?
5
10
15
105
Targets the common misconception on slide 13. The program uses the old value, adds 5, then stores the new result.
How values change
A program can assign a value to a variable, such as lives = 3. Later it can update the value after an input or event. Then the new value is stored in the variable.
Uses glossary words already taught in slides 12 to 15.
Reading a value back
A program can read a variable when it needs the stored value
It might display it, compare it, or use it in a calculation
Example: `print(playerName)` shows the saved name
Emphasise that storing a value is only useful because the program can use it again later.
Using a stored value
Examples of reading variables back:
`print("Hello " + playerName)`
`score = score + 1`
`if gameOver == true`
Students do not need to master syntax here. The aim is to see three uses: display text, update a number, and check a Boolean value.
Putting it all together
A simple program might:
store the player's name in `playerName`
start `score` at `0`
change `score` when points are earned
read both values back to show a message like `Well done, Sam! Score: 5`
This slide ties the whole lesson together: name, store, change, and use values in one familiar example.
What does print(playerName) do?
Stores a new name
Adds to the score
Shows the saved name
Checks true or false
Checks reading a value back from slides 16 to 18.
Putting it together
A simple program can store the player's name in playerName. It can start score at 0 and change it when points are earned. Then it can read both values back to show a message on the screen.
Summarises the final example on slide 18 using the same familiar context.
How much more do you feel you understand about variables thanks to this lesson?
Deze slide heeft geen instructies
Glossary
variable - a named storage place in a program
value - the data stored in a variable
assign - to store a value in a variable
update - to change the value stored in a variable
text - data made of letters, words, or symbols
number - data used for counting or calculation
Boolean - data that can only be `true` or `false`
Deze slide heeft geen instructies