ZX 81 - BASIC Programming

Sinclair ZX Spectrum
Chapter 9.2 - More computer programming

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?

Sinclair ZX Spectrum

  Previous Page Back Next Page