MyLeoNes™

Variables: names for changing information — Computing, 11–13 years

A variable gives a piece of information a name, so a program can remember it and change it.

What a variable does

A variable is a named place for a value, such as a score, a name or the number of lives left. The name helps a program find the value later. The value can stay the same or be replaced while the program runs.

Why use variables

Programs often need to work with information that is not known in advance, such as a player’s answer or a changing score. Without variables, you would have to write a separate instruction for every possible value. Names let one set of instructions handle many different cases.

Worked example: a running score

Set score to 0 at the start. When a player catches a star, add 10, so score becomes 10; after a second star, add 10 again, so it becomes 20. The same variable records the current total without needing a new name after every catch.

Trap: confusing a name with its value

It is easy to think that score is the number itself, but score is only the label for the current number. If score changes from 10 to 20, the name has not changed. This confusion is reasonable because a labelled box and what is inside it are often spoken about together.

Where variables appear

A weather app stores the current temperature, a bank app stores a balance, and a game stores a score or position. Each value may change, but the program still needs to refer to it. Variables make that changing information manageable and readable.

Keep exploring

Other languages

Loading MyLeoNes™…