KS3 Computing/Variables

From Wikibooks, open books for an open world
Jump to navigation Jump to search

A variable is a named location in the computer's memory, used to store a piece data (or information).

What does a variable look like?

[edit | edit source]
Diagram showing how a variable is stored

A variable can be called anything you want - you decide on a name when you write a program.

For example, in a chat-bot program, I might invite the user to enter their name. Each time the program runs, I would store that name in a variable in my program called name.
I might also have one called age to store the user's age. I can then refer to name and age at any point in my program - it is like having a placeholder for data entered by the user.

The diagram on the right illustrates how the variables name and age are used to store the information entered by the user.

Ideas for the classroom

[edit | edit source]

A good analogy to use to explain this concept to students is the one of a variable being like a box - you can put anything inside the box, give it a name, and refer to it anytime in your program. You can change what is in the box any time also.

It is important to stress that 'name' and 'age' (the variables in the example above) will change every time the program runs, depending on what the user types in. Often students find this difficult to grasp, especially if, as the main user of their own programs, they always type in their own name and age!

Students also find it difficult to name their own variables at first - they get hung up on what to call their variables, but really they need to understand that it can be anything (meaningful!).

Doing lots of examples in the classroom is the best way for students to get comfortable with this concept. Often it is the word 'variable' that causes confusion, even if they actually grasp the concept well.