Introducing Variables
Learning how to create placeholders for data that changes during a program's execution.
About This Topic
This topic introduces variables as named containers for data that can change while a program is running. Students learn to define a variable (give it a name) and initialise it (give it a starting value). This is a core concept in the UK National Curriculum for Key Stage 2, where students are expected to use variables in their programs.
In Year 5, the focus is on understanding that a variable is a placeholder. Whether it is a score in a game, a timer, or a player's name, the variable allows the program to be dynamic. Mastering this concept is essential for moving beyond simple linear scripts to creating interactive games and simulations.
Students grasp this concept faster through hands-on modeling where physical containers represent the variables in a program.
Key Questions
- Justify why we must set a variable to a starting value at the beginning of a game.
- Explain how one variable can be used to control multiple different sprites.
- Critique the impact of a confusing or inaccurate variable name on a program.
Learning Objectives
- Identify the purpose of a variable as a named container for data that can change.
- Demonstrate how to initialise a variable with a starting value in a simple program.
- Explain how changing the value of a variable affects the output or behaviour of a program.
- Critique the choice of variable names in provided code snippets for clarity and accuracy.
Before You Start
Why: Students need to understand that programs follow a specific order of instructions before they can grasp how variables change within that sequence.
Why: Familiarity with simple conditional statements (if/then) helps students understand how variable values can trigger different program behaviours.
Key Vocabulary
| Variable | A named storage location in a computer program that holds a value. This value can change while the program is running. |
| Initialise | To set a variable to a specific starting value before it is used in a program. This ensures the program begins with predictable data. |
| Data Type | The kind of data a variable can hold, such as a number (integer or decimal) or text (string). |
| Assign | The action of giving a value to a variable, either when it is first created or later in the program. |
Watch Out for These Misconceptions
Common MisconceptionA variable can hold multiple different values at the exact same time.
What to Teach Instead
Explain that a variable is like a single seat on a bus; only one value can sit there at a time. When a new value arrives, the old one is replaced. Physical modeling with boxes and single items helps reinforce this 'overwrite' rule.
Common MisconceptionThe name of the variable affects what it can do.
What to Teach Instead
Clarify that naming a variable 'score' doesn't automatically make it count points; the programmer must write the logic. Students often think the computer 'understands' English words, so showing a program where a variable named 'bananas' stores the score can be a powerful lesson.
Active Learning Ideas
See all activitiesSimulation Game: The Variable Boxes
Place several empty boxes at the front of the room, each labeled with a name like 'Score' or 'Lives'. Students act as the 'program' and follow instructions to put different numbers of counters into the boxes or swap them, demonstrating how the value changes but the name stays the same.
Think-Pair-Share: Naming Conventions
Show students a list of 'bad' variable names (e.g., 'x', 'thing', 'stuff') and 'good' names (e.g., 'player_score', 'timer_seconds'). Pairs must discuss why the good names make the code easier to read and then rename a set of mystery variables based on their function.
Inquiry Circle: Game Audit
Groups look at screenshots of popular games (like Minecraft or Roblox) and identify every piece of information on the screen that would need to be stored in a variable. They present their list, explaining why each variable needs a starting value.
Real-World Connections
- Video game developers use variables to keep track of player scores, health points, and the number of lives remaining. For example, in a racing game, a 'lap count' variable increases with each completed lap.
- App designers use variables to store user preferences, such as theme colours or notification settings. A weather app might use a variable to store the current temperature, which updates automatically.
Assessment Ideas
Present students with a short, simple program snippet that uses a variable (e.g., a score counter). Ask them to identify the variable name, its initial value, and predict the value after a specific action occurs. For example: 'What is the value of 'score' after the player collects 3 coins?'
Pose the question: 'Imagine you are designing a game where a character needs to jump. What variable could you use to control the height of the jump? Why is it important to give this variable a starting value?' Listen for student explanations of variables as placeholders and the need for initialisation.
Give each student a slip of paper. Ask them to write down one example of a variable they might use in a game and explain in one sentence why they chose that particular name for it. Collect and review for understanding of variable purpose and naming conventions.
Frequently Asked Questions
Why do we have to 'initialise' a variable?
Can a variable store words as well as numbers?
How does student-centered learning help with programming concepts like variables?
What is the best way to name a variable?
More in Variables in Games
Changing Variables
Programming triggers that increase or decrease variable values based on user input or sprite collisions.
2 methodologies
Variables for Game Rules
Using variables like timers to create win and loss conditions in a digital game.
2 methodologies
Decisions in Daily Life
Mapping everyday decisions to the logical structures used by computers.
2 methodologies
IF/ELSE Statements
Implementing branching paths in code to allow for different outcomes based on conditions.
2 methodologies
Sensing and Conditions
Using input sensors like mouse position or color touching to trigger conditional code.
2 methodologies
Nested IF Statements
Creating more complex decision structures by placing conditional statements inside other conditional statements.
2 methodologies