Nau mai, haere mai ki te whārangi "Variable"! This page will teach you about variables, fundamental building blocks in programming used to store and manage changing pieces of information within a computer program.
Define what a "variable" is in programming.
Explain the purpose of using variables in a computer program.
Identify different types of data that variables can store (data types).
Understand how the value stored in a variable can change during a program's execution.
To get the most out of learning about Variables, it's essential to have a clear understanding of:
Computer Program: Do you know that programs are sets of instructions designed to perform tasks?
Algorithms: Do you understand that algorithms are step-by-step instructions that manipulate information?
Input & Output: Do you know how data enters (input) and leaves (output) a program?
Quick Check: Imagine you're keeping score in a game. The score changes as players earn points. How do you keep track of that changing number in your head or on paper? That's similar to what a variable does in a computer program!
A variable in programming is like a named container or a labelled box in a computer's memory (hardware). This container is used to store a piece of data (information), and, crucially, the data stored inside it can change while the computer program is running.
Programmers give variables meaningful names (like user_score, player_name, temperature) so they can easily refer to the information they are storing.
Think of it like this:
You have a box labeled "My Favourite Colour". You put a piece of paper with "Blue" inside. "My Favourite Colour" is the variable name, and "Blue" is its current value.
Later, you change your mind and replace "Blue" with "Green". The variable name ("My Favourite Colour") stays the same, but its value has changed.
Data Types: Variables can store different kinds of data. This is called the data type. Common data types include:
Numbers: Whole numbers (integers like 5, -100) or numbers with decimals (floating-point numbers like 3.14, 0.5).
Example variable: player_score = 150
Text (Strings): Sequences of characters, like words, sentences, or names. Often put inside quotation marks.
Example variable: user_name = "Alice"
Boolean (True/False): Represents one of two states: True or False. Used for logical conditions.
Example variable: game_over = True
Variables are used throughout a computer program to hold input from the user, store results of calculations, keep track of changing states, and prepare output.
Key Actions with Variables:
Declaring/Creating a Variable: Giving a variable a name. (In some languages, you specify its data type at this point).
Assigning a Value: Storing a piece of data into the variable. This is often done using an equals sign (=).
Using a Variable: Accessing the value currently stored in the variable for calculations, displaying it, or making decisions.
Example: A Simple Program Using a Variable (Pseudocode)
Let's imagine a program that greets a user by their name:
1. CREATE user_name_variable // Declare a variable named 'user_name_variable'
2. PRINT "What is your name?"
3. GET input FROM keyboard and STORE it IN user_name_variable // Assign user's input to the variable
4. PRINT "Hello, " + user_name_variable + "!" // Use the variable's value for output
What happens step-by-step:
Step 1: The program sets aside a labelled box called user_name_variable in its memory. It's empty for now.
Step 2: The program displays "What is your name?" on the screen.
Step 3: The user types their name (e.g., "Māia"). The program takes this input and puts "Māia" into the user_name_variable box. Now, the variable user_name_variable holds the value "Māia".
Step 4: The program looks inside the user_name_variable box, sees "Māia", and combines it with "Hello, " and "!" to print "Hello, Māia!" on the screen as output.
If you ran this program again and someone else typed "Tane", the user_name_variable would then store "Tane", and the program would print "Hello, Tane!". The variable's value changed.
Programs also use variables with Comparison Operators and Logical Operators inside Selection statements (e.g., IF score > 100 THEN print "You win!") and to count or track progress in Iteration (loops).
Activity 1: Tracking Game Score
Task: Imagine you are playing a simple game where you start with 100 points. You gain 10 points for picking up a coin and lose 5 points for hitting an obstacle.
Activity:
Identify a variable you would need to store the player's score. What would you name it? What data type would it be?
Show how the value of this variable would change if:
You start the game.
You pick up 2 coins.
You hit 1 obstacle.
You pick up 1 more coin.
Explain how a computer program would use this variable to display your score.
Evidence: Create a Google Doc or Google Slide outlining your variable's name, type, its changing values through the scenario, and how a program would use it.
Activity 2: Data Type Match-Up
Task: Look at the following pieces of data.
Your age
Your favourite color
Whether it's currently raining (yes/no)
The price of a new video game
Your school's name
Activity: For each piece of data, suggest a suitable variable name and its appropriate data type (Number, Text, or Boolean).
Evidence: In a Google Doc, create a table with columns for "Data Item," "Suggested Variable Name," and "Data Type."
Check your understanding of Variables.
Multiple Choice: In programming, what is a variable primarily used for?
a) To make a program look visually appealing.
b) To perform calculations directly.
c) To store information that can change during a program's execution.
d) To define the sequence of a program.
Short Answer: What is the most appropriate data type (Number, Text, or Boolean) for a variable named user_login_successful that stores whether a user logged in correctly? Explain why.
Scenario: A simple weather app uses a variable named current_temperature.
If the app's temperature display changes from "18°C" to "22°C," what does this tell you about the current_temperature variable?
What type of data (data type) would typically be stored in the current_temperature variable?
A variable is a named container in memory that stores data which can change during a program's execution.
Variables have different data types (e.g., Number, Text/String, Boolean).
They are used to store input, calculation results, and data for output.
Variables are essential for programs to manage information dynamically.
Now that you understand how programs store changing information, you're ready to explore how they use this information to make decisions and perform complex tasks:
Comparison Operator: Learn how programs compare the values stored in variables.
Logical Operator: Understand how to combine different conditions involving variables to create more complex decisions.
Selection: Discover how programs make choices and take different paths based on the values of variables.
Iteration: Explore how programs use variables to count, track progress, or control how many times a task is repeated.
Formal Languages: See how programming languages define rules for naming, declaring, and using variables.
Artificial Intelligence (AI): Understand how AI systems use vast numbers of variables to store and manipulate data for learning and decision-making.
Continue your journey by clicking on the links to these exciting topics!