ZX 81 - BASIC Programming

Sinclair ZX Spectrum
Chapter 9.1 - More computer programming
Type

NEW

This will erase any old programs & variables in the ZX81. (This is rather like CLEAR, but CLEAR only erases the variables.) Now carefully type in this program:

10 REM THIS PROGRAM EXTRACTS SQUARE ROOTS

20 INPUT A

30 PRINT A,SQR A

40 GOTO 20

(You will need to type in most of the spaces yourself in line 10).

Now run it. Apparently the screen goes blank, & nothing happens, but look at the cursor in the bottom left-hand corner: where you might have expected a there is instead an - the machine has gone into input mode. This is the effect of the INPUT statement in line 20. The machine is waiting for you to type in a number (or even an expression), & it won't carry on until you have. After that, it will have the effect of

20 LET A=... whatever you typed

Just a minute though, what happened to line 10? It looks as though the computer has completely ignored it. Well, it has. REM in line 10 stands for remark, or reminder, & there solely to remind you of what the program does. A REM statement consists of REM followed by anything you like, & the computer will ignore it.

All right, so we're in input mode for line 20. Type some number, 4, say, & then NEWLINE. The 4 & its square root appear on the screen, & you might think that was that. But, no - it seems to want another number. This is because of line 40, GOTO 20, which means exactly what it says. Instead of running out of program & stopping, the computer jumps back to line 20 & starts again. So, type another number (2, say; at any rate you had better make it positive).

After a few more of these you might be wondering if the machine will ever get bored with this game; it won't. Next time in its insatiability it asks for another number, type STOP (shifted A) instead; it will take the hint. The computer reports back with report D/20 - look up D in the list of reports (appendix B). 20 is the line where it was waiting for some input when you stopped it.

Have you suddenly remembered some more numbers that you wanted the square root of? Then type

CONT

(short for CONTINUE) & the computer will clear the screen & ask you for another number.

For CONT, the computer remembers the line number in the last report that it sent you that had a code other than 0, & jumps to that line. Since the last report was D/20 (& D is not 0), in our case CONT is identical to GOTO 20.

Now type in numbers until the screen starts getting full. When it is full, the computer will stop with the report 5/30. 5 means 'screen full', & 30 is the number of the PRINT statement it was trying to execute when it discovered there was no room. Again,

CONT

will clear the screen & carry on - this time, CONT means GOTO 30.

Note that the screen is cleared not because this is a CONT statement, but because it is a command. All commands (except COPY, which appears in chapter 20) clear the screen first.

When you're tired of this, stop the program using STOP & get the listing by pressing NEWLINE.

Look at the PRINT statement on line 30. Each time the pair of numbers A & SQR A is printed, it is on a new line, & this is because the PRINT statement does not end with a comma or semicolon. Whenever this is the case, then the next PRINT statement starts printing on a new line. (Thus to put in a blank line, use a PRINT statement in which there is nothing to be printed - just PRINT on its own.)

However, a PRINT statement can end in a comma or semicolon, & then the next PRINT statement carries on printing as though the two had been one long statement.

For instance, with commas, replace the 30 by

30 PRINT A,

& run the program to see how successive PRINT statements can print on the same line but spread out in two columns.

With semicolons, on the other hand, with

30 PRINT A;

everything is jammed together.

Try also

30 PRINT A

Now type in these extra lines.

100 REM THIS PROGRAM MEASURES STRINGS
110 INPUT A$
120 PRINT A$,LEN A$
130 GOTO 110

This is a separate program from the last one, but you can keep them both in at the same time. To run the new one, type

RUN 100

This program inputs a string instead of a number, & prints it & its length. Type

CAT      (& NEWLINE, as usual)

Because the computer is expecting a string, it prints out two string quotes - this is a reminder to you, & it usually saves you some typing as well. But you don't have to restrict yourself to string constants (explicit string with opening & closing quotes & all their characters in between); the computer will evaluate any string expression, such as one with string variables. In this case you might have to rub out the quotes that the computer has displayed. Try this. Rub them out (with & RUBOUT twice), & type

A$

Since A$ still has the value "CAT", the answer is CAT 3 again.

Now input

A$

again, but this time without rubbing out the string quotes. Now A$ has the value "A$", & the answer is 2.

If you want to use STOP for string input, you must first move the cursor back to the beginning of the line, using .

Now look back at the RUN 100 we had earlier on. That jumps to line 100, so couldn't we have said GOTO 100 instead? In this case, it so happens that the answer is yes, but there is a difference. RUN 100 first of all clears the variables (like CLEAR in chapter 6), & after that works just like GOTO 100. GOTO 100 doesn't clear anything. There may well be occasions when you want to run a program without clearing any variables. Here GOTO is necessary & RUN could be disastrous, so it is best to get into the habit of automatically typing RUN to run a program.

Another difference is that you can type RUN without a line number, & it starts off at the first line in the program. GOTO must always have a line number.

Both these programs stopped because you typed STOP in the INPUT line; but sometimes - by mistake - you write a program that you can't top & won't stop itself. Type

200 GOTO 200

RUN 200

This looks all set to go on for ever unless you pull the plug out; but there is a less drastic remedy. Press the SPACE key, which has 'BREAK' written above it. The program will stop, saying D/200.

At the end of every program line, the computer looks to see if this key is pressed; & if it is, then it stops. The BREAK key can also be used when you are in the middle of using the tape recorder or the printer.

You have now seen the statements PRINT, LET, INPUT, RUN, LIST, GOTO, CONT, CLEAR, NEW & REM, & most of them can be used either as commands or as program lines - this is true of almost all statements in BASIC. The only real exception is INPUT, which cannot be used as a command (you get report 8 if you try; the reason is that the same area inside the computer is used for both commands & for input data, & for an INPUT command there would be a muddle). RUN, LIST, CONT, CLEAR & NEW are not usually much use in a program, but they can be used.

Summary

Statements: GOTO, CONT, INPUT, NEW, REM, PRINT

STOP in input data

BREAK

Sinclair ZX Spectrum

  Previous Page Back Next Page