ZX 81 - BASIC Programming

Sinclair ZX Spectrum
Chapter 8.1 - Computer programming
And now at last you shall write a computer program. Turn the computer off & on, just to make sure that it is clear. Now type

10 LET BUTTER=75      (& NEWLINE)

& the screen will look like this:


This is different from what happened with EGGS in chapter 6; if you type

PRINT BUTTER

you will see (from the report 2) that the variable BUTTER has not been set up. (Press NEWLINE again & the screen should go back to looking like the picture.)

Because the LET statement had a number, 10, in front of it, the computer did not execute it straight away, but saved it for later. 10 is its line number, & is used to refer to it rather in the same way that names are used to refer to variables. A set of these stored statements is called a program. Now type

20 PRINT BUTTER

& the screen should look like this:


This is a listing of your program. To have the program carried out (or executed or run), type

RUN      (don't forget the NEWLINE)

& the answer 75 will appear in the top left-hand corner of the screen. At the bottom left-hand corner you will see the report 0/20. 0, as you know, means 'OK, no problems', & 20 is the number of the line where it finished. Press NEWLINE, & the listing will come back.

Note that the statements were executed in the order of their line numbers.

Now suppose you suddenly remember that you also need to record the price of yeast. Type

15 LET YEAST=40

& in it goes. This would have been much harder if the first two lines had been numbered 1 & 2 instead of 10 & 20 (line numbers must be whole numbers between 1 & 9999), so that is why, when first typing in a program, it is good practice to leave gaps in the line numbers.

Now you need to change line 20 to

20 PRINT BUTTER, YEAST

You could type out the replacement in full, but there is a way to use what is there already. You see that little by line 15? This is the program cursor, & the line it points to is the current line. Press the key (shifted 6), & it will move down to line 20. ( moves it up again.) Now press the EDIT key (shifted 1), & a copy of line 20 will be displayed at the bottom of the screen. Press the key 7 times so that the cursor moves to the end of the line, & then type

,YEAST (without NEWLINE)

The line at the bottom should now read

20 PRINT BUTTER, YEAST

Press NEWLINE & it will replace the old line 20. The screen will now look like this:


RUN this program & both prices will be displayed.

(Here is a useful trick involving EDIT, to use when you want to clear the bottom part of the screen altogether. Press EDIT, & the current line will be brought down from the program, replacing what you wanted deleting. If you now press NEWLINE, the line will be put back in the program, making no difference to it, & the bottom part of the screen will be cleared leaving just the cursor.)

Now type - in a fit of absent-mindedness -

12 LET YEAST=40

This will go up into the program & you will realise your mistake. To delete this unnecessary line, type

12      (with NEWLINE, of course)

You will notice with surprise that the program cursor has gone. You should imagine it as being hidden in between lines 10 & 15, so if you press it will move up to line 10, while if you press it will move down to line 15.

Last, type

LIST 15

You will now see on the screen

15LET YEAST=40

20 PRINT BUTTER, YEAST

Line 10 has vanished from the screen, but it is still in your program - which you can prove by pressing NEWLINE again.

The only effect of LIST 15 are to produce a listing that starts at line 15, & to put the program cursor at line 15.

LIST

on its own makes the listing start at the beginning of the program.

Summary

Programs

Editing programs using , & EDIT.

Statements: RUN, LIST

Sinclair ZX Spectrum

  Previous Page Back Next Page