What is a variable?
The word vary means to change or differ.
Other more familiar words that are linked to vary include variety, varies, various and variable. They all mean something to do with change and difference.
We find variableA named piece of data (often a number or text) stored in a computer鈥檚 memory, which can be accessed and changed by a computer program. everywhere around us in our everyday lives. The world is always changing!
Variables in everyday life
Let's think about a supermarket checkout.
- When a new shopper gets to the checkout the screen display reads 拢0.00.
- Each item is put through the barcode scanner to find the price.
- The price will change or vary depending on the item.
- The price is added to the total cost displayed on the screen.
- The total cost displayed changes or varies every time a new item is scanned.
Variables in a computer program
In computing, we use the word variable to mean a specific part of a computerprogramA sequence of instructions written in a coding language that a computer can understand. that can store a piece of information that changes or varies when the program is run.
We can think of the variable like a box that holds the changing information. The box stays the same but the information inside it can change.
Let's think about the algorithmA precise set of ordered rules or instructions that can be followed by a human or a computer to achieve a task. for the computer program that is controlling the checkout till display.
We know there are two things that are changing.
- The price of each item.
- The total cost of the shopping.
So, we can use two variables called price and total in our algorithm.
More about variables
Variables are a very useful programming tool. Here's how variables work.
- A program can have more than one variable, but each one will have its own name.
- A variable can only work with one piece of changing information at a time.
- Variables can contain words, numbers and valueThe specific information stored in a variable at any one time. like TRUE or FALSE.
- Once information is collected in a variable, it can be used somewhere else in the program.
We can find out more by looking at the program code for a simple computer quiz game.
Welcome to the dinosaur maths quiz!
When you start the game by pressing the space bar, the dino quizbot asks for your name and shows you an answer box.
Then, once you type in your name, the quizbot says "hello" and uses your name. It's been programmed to be very friendly!
The player's name will change every time someone new starts the quiz.
Do you think we can use a variable in the program for the quiz?
Using text in a variable
A variable can use text as well as numbers.
In our quiz program, we can create and use a variable called PLAYER NAME to store the player's name once they have typed it in.
What will the code look like?
The first line of code in our program tells the game to wait for the dinosaur to be clicked, before starting the game.
The next line of code instructs the game to type "What's your name?" on the screen and wait for the player to type in an answer.
The next line sets the value of the PLAYER NAME variable to the answer from the question.
The last line of code tells the program to type the information stored in the PLAYER NAME variable, after the word "Hello" on the screen.
Using the PLAYER NAME variable like this, means the quiz can personalise the greeting every time a new player starts and the name changes.
Remembering your details online
On the internet, variables like this are an important part of small programs called cookies.
Cookies are used to remember your name and other personal details, when you log into online accounts or visit websites.
You may have seen messages onscreen asking if you want to allow these variable values to be saved.
Keeping the score
We can use a variable in our dinosaur maths quiz program, to keep track of the correct answers.
The program has a variable called score.
Each time a player gets an answer correct the program changes the value in the score variable by adding one.
Look at the code and see if you can work out what the other parts of the program are doing?
Winning the game
We can use variables with other programming tools like selection. Selection is when a program asks a question or makes a choice about what to do next.
We can use a variable to help the program decide what to do.
Here the program uses the value of the score variable to decide if the player has won the game. The player can only win if the score has reached 10 points.
Watch: Variables
You can find out more about variables in this video from 成人快手 Teach.
Activities
Computing - Dance Mat Typing. gameComputing - Dance Mat Typing
Build and test your computing skills with different levels of touch type challenges
More on Computer science
Find out more by working through a topic
- count19 of 24
- count20 of 24
- count21 of 24
- count22 of 24