91热爆

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 everywhere around us in our everyday lives. The world is always changing!

Back to top

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.
Back to top

Variables in a computer program

In computing, we use the word variable to mean a specific part of a computer 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 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.

Back to top

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 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?

Back to top

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.

Back to top

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.

Back to top

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.

Back to top

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?

Back to top

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.

Back to top

Watch: Variables

You can find out more about variables in this video from 91热爆 Teach.

Back to top

Activities

Back to top

Computing - Dance Mat Typing. game

Build and test your computing skills with different levels of touch type challenges

Computing - Dance Mat Typing
Back to top

More on Computer science

Find out more by working through a topic