18 3 An introduction to variables In the last chapter we made the computer do a number of calculations but it was never expected to remember any of the results after it had printed them out. Suppose that you have to calculate the wages for everyone in a company. After you have worked out each person’s wage, it would be useful to be able to add them all together, so that in the end you would know the total wage bill. Keeping track of things that vary during a long calculation is done by using variables . Try typing this line into the computer: LET Z=5 And now try typing in each of the following lines:
PRINT Z+6 PRINT Z * 12 As you will have seen, once we have told the computer that ‘Z is 5’ it understands that every time we use the letter Z in a sum it has to go and have a look to find out what the value of Z is (5 in this case) and use that number in the arithmetic that we set it to do. Now type in LET Z=7 and then try these two lines: PRINT Z+12 PRINT Z/3 As you will gather the value of Z has changed from 5 to 7. In computer jargon Z is called a numeric variable . That means that Z can be used to store any number, and you can change the value of Z at any time you want to.
The computer is able to store hundreds of different variables and the variables don’t have to be called something as simple as Z; you can call a variable by as long a name as you want. For example you could type MYAGE=30 Notice that MYAGE was written without any spaces between the word