Contents / Part 2 / Previous chapter / Next chapter / Index
In an earlier section the idea of 'variables' was introduced. Variables are a fundamental concept in computing, and it is not possible to go far without understanding them.
As we have seen, it is possible try say
and the computer knows that it must label a 'box' in its memory with the name X and that the current value of X is l2. With a variable it is possible to alter the value of what is in the 'box' but not the name of the 'box' itself. The statement
simply changes the value of X from 12 to 14. Similarly we can say
which looks rather odd - like an equation which does not balance. In fact all that this is redoing is saying to the computer - whatever the value inside your box 'X', make it increase by 1 from now on.
So far we have considered only 'numeric' variables - that is, variables which contain numbers and on which arithmetic can be carried out. But the computer has letters and .symbols of various kinds on its keyboard - what about them?
Although we can talk of the 'number' 22, we can also consider 22 as a pair of characters - in the same way as A, B, C, ?, $ are characters. In computing it is important to be able to distinguish between numbers and characters. Arithmetic can be carried out on numbers but not on characters. To give you an example to show that this is not such an esoteric idea, consider 22. We can divide 22 by 2 and get 11 if 22 is taken to be a 'number'. But if we talked about a train leaving 'Platform 22', the 22 here would be a pair of characters. You cannot, with a great deal of meaning divide 'Platform 22' by 2 and get 'Platform 11'.
Next it's important to have a look at the other major kind of variable used in computing - one which can hold characters, not numbers. This is called a 'string' variable.
String variables are used to store "strings of character" e.g. words. They can be recognised easily because they always end with a dollar sign. Here are a few examples of string variables containing various strings of characters. Note that these strings must be enclosed by quotation marks.
DAY$= "SUNDAY 3RD JANUARY"
In the first example X$ is called a string variable and HELLO is called a string. Once X$ has been set to contain HELLO we can use statements like
in just the same way as we said on page 24.
String variables can be used to hold any number of characters between zero (empty) and 255 (full)
X$="" will empty X$
X$="A" will set X$ to contain 1 character
Of course you cannot use ordinary arithmetic on string variables. For example
PRINT NAME$ / 10
does not make sense. You can't divide Susan's name into 10 parts. Whilst you can add, subtract, multiply and divide using
numeric variables the only similar operation that can be carried out on a string variables is that of "addition". Thus
10 A$ = "TODAY IS"
20 B$ = "SUNDAY"
30 C$ = A$+B$
40 PRINT C$
TODAY IS SUNDAY
The importance of understanding string variables cannot be over-emphasized. Later sections develop the idea.
Each memory location in the computer can be used to store any number between, and including, 0 and 255, and yet some way has to he found to store letters and also very large numbers. A number of codes are used in the computer in much the same way that different groups of people have used different codes to count. Thus the number 1982 can also he written as
MCMLXXXII in Roman numerals
or 1982 in decimal Arabic numerals
or 7BE in hexadecimal Arabic
or 11110111110 in binary.
The need to transmit and store letters has produced another set of codes. The letter "J" is coded in various ways thus
.--- in Morse
1001010 in ASCII binary
4A in ASCII hexadecimal
74 in ASCII decimal.
The ASCII (American Standard Code for Information Interchange), is by far the most common code used by computers to represent characters. A complete code table is given on page 490.
When you tell the computer
A$ = "HELLO"
it stores the ASCII codes for the letters in the word HELLO in successive memory locations. The fact that they are stored as
ASCII codes is really irrelevant - as far as the user is concerned, it just works. However, there are times when the user needs to know about the ASCII codes and two functions are provided to convert between "characters" and ASCII codes.
The function ASC converts a character into its ASCII code. Thus
would print 74.
The reverse function, of converting an ASCII code into a character, is performed by CHR$.
Thus PRINT CHR$(74) would print the letter J. In fact, one quite often needs to use PRINT CHR$, so there is a further shortened version of that statement. It is VDU. So VDU 74 would also print the letter J.
Those doing more complicated programming will need to know the exact way that the computer stores strings and numerics in memory. Full information is given at the end of section 39.
The numeric variables you have met so far are technically known as "real variables". They can be used to store any number between
200 000 000 000 000 000 000 000 000 000 000 000 000 (2 x 1038) and 0.000 000 000 000 000 000 000 000 000 000 000 000 002 (2 x 10-39), and can include a decimal point. Of course a similar range of negative numbers can be stored too. The problem with real numbers is that they are only stored to 9 figure accuracy, nonetheless this is quite accurate enough for most purposes. Another type of numeric variable is an 'integer' variable.
Integer variable names are distinguished by having a percent sign as the last character of the variable name. They can only store whole numbers between - 2,147,483,648 and 2, 147,483, 647.
On the other hand integer variables are held with complete accuracy - so accounting problems can be dealt with to the nearest penny in ?2M. Arithmetic calculations with integer variables are significantly faster than with real variables. (See section 32 further suggestions for speeding up programs).
The two integer operators MOD and DIV are described on page 130
The variables A% to Z% are special in that they are permanently allocated space in memory. Typing RUN or NEW does not destroy them. As a result the variables A% to Z% can be set in one program and then used in another program later on without losing their values. Of course the values will be lost if the machine is switched off but otherwise they will remain, even if BREAK is pressed. The WELCOME cassette uses the variable M%, to inform each of the individual programs whether or not the user's cassette recorder has got automatic motor control.
The variables A% to Z% are called the Resident Integer Variables.
Three main types of variables are supported in this version of BASIC: they are Integer, real and string.
|maximum size||2,147,483,647||1.7x1038||255 characters|
|accuracy||1 digit||9 sig figs||-|
|stored in||32 bits||40 bits||ASCII values|
All variable names can contain as many characters as required and all characters are used to identify the variable. Variable names may contain capital letters, lower case letters and numbers and the underline character. Variable names must start with a letter and must not start with a BASIC keyword.