teach-ict.com logo

THE education site for computer science and ICT

3. Variables

Before we set out an example, you need to know about a vital part of programming. Namely the meaning of 'variable'.

A variable is to computing as a brick is to building. It is absolutely basic to computing.

A variable is a named value that can be changed as the program runs.

For instance define a 'variable' called A and set it to a starting value such as 10 i.e. A = 10

Now anywhere in the program that needs to refer to that value can use the label 'A'.

In addition, if any change to that value is needed, the program can refer to A

For example

Let A = 10

then another part of the program changes it by doing something to it, like this

A = A + 1 

which adds 1 to the variable called A, so A = 11 at tthis point in the computer program

or

A = 11

which re-defines the value of A

or

A = B + 1

which copies the value of another variable called B and adds 1 to it.

A variable is actually a location in memory within the computer. The program changes the data held in that location by referring to the variable name.

Variables can be almost any name but it makes sense to use sensible description such as Sales_Price and not be too long.

It is also a good idea to avoid spaces as it can make spotting a problem very difficult.

 

Challenge see if you can find out one extra fact on this topic that we haven't already told you

Click on this link: Designing algorithm