Chapter 9 - 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 instability 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, byt 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
 
 

Exercises

1. In the square root program, try replacing line 40 by GOTO 5, GOTO 10 or GOTO 15 - it should make no perceptible difference to the running of the program. If the line number in a GOTO statement refers to a non-existent line, then the jump is to the next line after. The same goes for RUN; in fact RUN on its own actually means RUN 0.
 
 

2. Run the string length program, & when it asks you for input type

        X$      (after removing the quotes)

    Of course, X$ is an undefined variable & you get report 2/110.

    If you now type

       LET X$="SOMETHING DEFINITE"

(which has its own report of 0/0) &

       CONT

you will find that you can use X$ as input data without any trouble.

    The point about this exercise is that CONT has the effect of GOTO 110. It disregards the report 0/0 because that had code 0, & takes its line number from the previous report, 2/110. This is intended to be useful. If a program stops over some error then you can do all sorts of things to fix it, & CONT will start work afterwards.
 
 

3. Try this program:

        10 INPUT A$

        20 PRINT A$;" = ";VAL A$

        30 GOTO 10

(c.f. chapter 7, exercise 1).

    Put in extra print statements so that the computer announces what it is going to do, & asks for the input string with extravagant politeness.
 
 

4. Write a program to input prices & print out the VAT due (at 15%). Again, put in PRINT statements so that it tells you what it's doing. Modify the program so that you can also input the VAT rate (to allow for zero rating or future budgets).
 
 

5. Write a program to print a running total of numbers you input. (Suggestion: have two variables TOTAL - set to 0 to begin with - & ITEM. Input ITEM, add it to TOTAL, print them both, & go round again.)
 
 

6. The automatic listings (the ones that are not the result of a LIST statement) may well have you puzzled. If you type in a program with 50 lines, all REM statements,

        1 REM

        2 REM

        3 REM

        :  :

        :  :

        49 REM

        50 REM

then you will be able to experiment.

    The first thing to remember is that the current line (with ) will always appear on the screen, & preferably near the middle.

    Type

       LIST    (& NEWLINE, of course)

& then press NEWLINE again. You should get lines 1 to 22 on the screen. Now type

        23 REM

& you should get lines 2 to 23 on the screen; type

        28 REM

& you get lines 27 to 48. (In both cases, by typing in a new line you have moved the program cursor so that a new listing has to be made.)

    Does this look a little arbitrary to you? It is actually trying to give you exactly what you want, although, humans being unpredictable creatures, it doesn't always guess right.

    The computer keeps a record not only of the current line, the one that has to appear on the screen, but also the top line on the screen. When it tries to make a listing, the first thing it does is compare the top line with the current line.

    If the top line comes after, then there is no point in starting there, so it uses the current line for a new top line & makes its listing.

    Otherwise, it first tries to make the listing starting at the top line. If the current line gets on the screen then all is well; if the current line is only just off the bottom of the screen then it moves the top line down one & tries again; & if the current line is way off the bottom of the screen then it changes the top line to be the line before the current line.

    Experiment with moving the current line about by typing

        line number REM

    LIST moves the cursor line but not the top line, so subsequent listings might be different. For instance, type

       LIST

to get the LIST listing, & then press NEWLINE again to make line 0 the top line. You should have lines 1 to 22 on the screen. Type

       LIST 22

which gives you lines 22 to 43; when you press NEWLINE again, you get back lines 1 to 22. This tends to be more useful for short programs than for long ones.
 
 

7. What would CONT, CLEAR & NEW do in a program? Can you think of any uses at all for this?


Previous: Chapter 8    Next: Chapter 10