ZX SPECTRUM Introduction
5. Simple programming
Up to now we have been telling the computer what to do, directly from the keyboard. Although it is possible to combine commands together, only limited applications are feasible using this method.
The great thing about computers is that they are programmable. This means that we can give them a series of instructions to make them do things in a sequence.
Every computer has its own language which allows us to communicate with it. Some languages are very simple - so that the computer can understand them easily. Unfortunately languages that are simple for the computer to understand are difficult to humans. In some ways the reverse is also true - languages simple enough for us to understand are relatively difficult to the computer - and even have to be translated or interpreted.
The ZX Spectrum uses a high level language called BASIC. BASIC stands for Beginners All-purpose Symbolic Instruction Code and the language was designed at Dartmouth college in New Hampshire, USA, in 1964. It is very widely used on personal computers but although it is broadly similar on all of them there are subtle differences. That is why this manual is written specifically for the ZX Spectrum. But ZX Spectrum BASIC is not too far from a non-existent) consensus BASIC and so you should not have too much trouble adapting any BASIC program to work on the ZX Spectrum. Unlike other BASICs ZX Spectrum BASIC does not allow the command LET to be omitted when values are being assigned to variables.
There is a limit to how many instructions can be stored in the computer. The ZX Spectrum indicates this limit by emitting a buzz. When programming in BASIC it is necessary to let the computer know the order in which the instructions are to be executed, Hence each line of the sequence of instructions has a number at its beginning. It is normal to start at 16 and to increase this by 10 for each new line. This allows other lines to be inserted it they have been omitted or the program to be modified.
Let's look at a simple program. Consider the series of commands at the end of the last chapter. If we wanted to repeat the series of commands it would be necessary to enter them each time. A program overcomes that necessity.
Type in the following with ENTER after each line.
10 LET b$="What is your age? ”
20 LET a$="Your age is "
30 INPUT (b$);age
40 PRINT a$;age
Note that it is not necessary to enter any spaces, except inside quotes.
Nothing will actually happen until we tell the computer to start working on the program. That is done by using RUN (the keyword on R).
Enter this command and see what happens.
You may also have noticed a right facing arrow when each line has been entered. This indicates the last line entered. If you want to see the program again key ENTER again, (or LIST) You can use RUN to execute the program as many times as you like. When you no longer need this program, you can remove it by using the NEW command. This wipes out the program stored in memory, and gives you a 'clean slate’ ready to put in a new one.
Key NEW then LIST and see what happens.
When you type in a command preceded by a number then this tells the computer that it is not simply a command, but a program line. The computer does not execute it but stores it away for later.
The ZX Spectrum helpfully writes on the screen (or lists) all the program lines that you have entered with a > against the last line that you entered.
The computer will not execute any of these lines immediately but just stores them away, inside itself.
To get the computer to execute these lines you must use the command RUN.
If you press ENTER on its own you will get the listing back.
Let’s consider another simple program. This one will be a bit more mathematical and print out the squares of all the numbers between 1 and 10 (the square of a number is just that number multiplied by itself).
To generate numbers from 1 to 10 introduces another concept in programming in BASIC. This is the method by which we get the computer to count. Earlier we have seen that numbers can be stored in the computer's memory by attaching a 'name' to them - or technically, assigning a value to a variable. Let the variable x start with the value 1 and increase in steps of 1 to 16. This is done by using the command FOR … TO... STEP.
So to enter this program key NEW to get rid of the previous one and type the following:
10 FOR x=1 TO 10 STEP 1
(Normally the STEP 1 part can be omitted if counting is going up in steps of one).
The next tine must now tell the computer what to do with x at whatever value it is, so key:
20 PRINT x, x*x
Finally we need a line to tell the computer to go to the next value of x, therefore key
30 NEXT x
On reaching this instruction the computer goes back to line 10 and repeats the sequence. When x exceeds 10 the computer goes to the next line in the program i.e. line 40.
The program should now appear on the screen as follows:
10 FOR x=1 TO 10 STEP 1
20 PRINT x, x*x
30 NEXT x
For completeness we should really have another line telling the computer that the program has ended when x= 10 so key
If the program is now RUN two columns should appear the first with values of x, the second with values of x*x or x squared. It is possible to label these columns by adding another line, like this
5 PRINT "x","x*x"
Notice that although this has been entered after all the other lines because it has a lower line number the computer automatically puts it in the correct place.
Try writing programs using other mathematical functions. If you have any doubt about how to use them refer to the appropriate pages in the BASIC programming manual.