Chapter 2: Basic programming concepts

BASin

Chapter 2: Basic programming concepts

Summary

Type in these two lines of a computer program to print out the sum of two numbers:

20 PRINT a
10 LET a=10

Because these lines began with numbers, they were not obeyed immediately but stored away, as program lines. You will also have noticed here that the line numbers govern the order of the lines within the program: this is most important when the program is run, but it is also reflected in the order of the lines in the listing that you can see on the screen now.

So far you have only entered one number, so type

15 LET b=15

and in it goes. It would have been impossible to insert this line between the first two if they had been numbered 1 and 2 instead of 10 and 20 (line numbers must be whole numbers between 1 and 9999), so that is why, when first typing in a program, it is good practice to leave gaps between the line numbers. If you do get stuck, though, BASin provides a line renumbering option on the Tools menu.

Now you need to change line 20 to

20 PRINT a+b

You could type out the replacement in full, but it is easier to move onto the existing line and edit it in place. The quickest way to move to a line is the Go to Line Number command on the Search menu.

Move to line 20, and keep pressing the key until the cursor moves to the end of the line. Then type

+b (without Enter)

The line should now read

20 PRINT a+b

Press Enter and it will replace the old line 20.

BASin has a safety feature that was not present on the original ZX Spectrum. When you enter a new line with the same number as an existing line, the cursor turns green and jumps to the line number to warn you. Pressing Enter a second time will replace the old line with the new one. This behaviour can be turned off in BASin's options.

Run the edited program using RUN and Enter and the sum will be displayed.

Run the program again and then type

PRINT a, b

The variables are still there, even though the program has finished.

If you enter a line by mistake, say

12 LET b=8

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

12 (with Enter of course)

Lastly, type

LIST 15

You will now see in the display window

15 LET b=15
20 PRINT a+b

Line 10 is not present in the listing, but it is still in your program - which you can prove by scrolling through the program in the editor. The only effects of LIST 15 are to produce a listing that starts at line 15, and to put the program cursor at line 15. If you have a very long program, then LIST will probably be a more useful way of moving the program cursor than and .

This illustrates another use of line numbers: they act as names for the program lines so that you can refer to them, rather like the way in which variables have names.

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

Another useful command is NEW. This erases any old programs and variables in the computer. Now carefully type in this program, which changes Fahrenheit temperatures to Centigrade.

10 REM temperature conversion
20 PRINT "deg F", "deg C"
30 PRINT
40 INPUT "Enter deg F", F
50 PRINT F,(F-32)*5/9
60 GO TO 40

Although GO TO has a space in it, it is really all one keyword. The space is optional.

Now run it. You will see the headings printed on the screen by line 20, but what happened to line 10? Apparently the computer has completely ignored it. Well, it has. REM in line 10 stands for remark, or reminder, and is there solely to remind you of what the program does. A REM command consists of REM followed by anything you like, and the computer will ignore it right up to the end of the line.

By now, the computer has got to the INPUT command on line 40 and is waiting for you to type in a value for the variable F - you can tell this because there is a flashing cursor at the bottom of the display window. Enter a number; remember Enter. Now the computer has displayed the result and is waiting for another number. This is because of line 60, GO TO 40, which means exactly what it says. Instead of running out of program and stopping, the computer jumps back to line 40 and starts again. So, enter another temperature. After a few more of these you might be wondering if the machine will ever get bored with this; it won't. Next time it asks for another number, type STOP. The computer comes back with a report H STOP in INPUT, 40:1, which tells you why it stopped, and where (in the first command of line 40).

If you want to continue the program type

CONTINUE

and the computer will ask you for another number.

When CONTINUE is used the computer remembers the line number in the last report that it sent you, as long as it was not 0 OK, and jumps back to that line: in our case, this involves jumping to line 40, the INPUT command.

Replace line 60 by GO TO 31 - it will make no perceptible difference to the running of the program. If the line number in a GO TO command refers to a non-existent line, then the jump is to the next line after the given number. The same goes for RUN; in fact RUN on its own actually means RUN 0.

Now type in numbers until the display starts getting full. When it is full, the computer will move the whole of the top half of the display up one line to make room, losing the heading off the top. This is called scrolling.

When you are tired of this, stop the program using STOP (or Esc) and return to the listing in the editor.

Look at the PRINT statement on line 50. The punctuation in this - the comma (,) - is very important, and you should remember that it follows much more definite rules than the punctuation in English.

Commas are used to make the printing start either at the left hand margin, or in the middle of the display, depending on which comes next. Thus in line 50, the comma causes the centigrade temperature to be printed in the middle of the line. With a semicolon, on the other hand, the next number or string is printed immediately after the preceding one. You can see this in line 50, if the comma is replaced by a semicolon.

Another punctuation mark you can use like this in PRINT commands is the apostrophe ('). This makes whatever is printed next appear at the beginning of the next line on the display but this happens anyway at the end of each PRINT command, so you will not need the apostrophe very much. This is why the PRINT command in line 50 always starts its printing on a new line, and it is also why the PRINT command in line 30 produces a blank line.

If you want to inhibit this, so that after one PRINT command the next one carries on on the same line, you can put a comma or semicolon at the end of the first. To see how this works, replace line 50 in turn by each of

50 PRINT F,
50 PRINT F;

and

50 PRINT F

and run each version - for good measure you could also try

50 PRINT F'

The one with the comma spreads everything out in two columns, that with the semicolon crams everything together, that without either allows a line for each number and so does that with the apostrophe - the apostrophe gives a new line of its own, but inhibits the automatic one.

Remember the difference between commas and semicolons in PRINT commands; also, do not confuse them with the colons (:) that are used to separate commands in a single line.

Now type in these extra lines:

100 REM this polite program remembers your name
110 INPUT n$
120 PRINT "Hello ";n$;"!"
130 GO TO 110

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

RUN 100

Because this program inputs a string instead of a number, it prints out two string quotes - this is a reminder to you, and it usually saves you some typing as well. Try it once with any alias you care to make up for yourself.

Next time round, you will get two string quotes again. but you don't have to use them if you don't want to. Try this, for example. Rub them out (with and Backspace twice), and type

n$

Since there are no string quotes, the computer knows that it has to do some calculation: the calculation in this case is to find the value of the string variable called n$, which is whatever name you happen to have typed in last time round. Of course, the INPUT statement acts like LET n$=n$, so the value of n$ is unchanged.

The next time round, for comparison, type

n$

again, this time without rubbing out the string quotes. Now, just to confuse you, the variable n$ has the value "n$".

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 that RUN 100 we had earlier on. That just jumps to line 100, so couldn't we have said GO TO 100 instead? In this case, it so happens that the answer is yes; but there is a difference. RUN 100 first of all clears all the variables and the display, and after that works just like GO TO 100. GO TO 100 doesn't clear anything. There may well be occasions where you want to run a program without clearing any variables; here GO TO would be necessary and RUN could be disastrous, so it is better not 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, and it starts off at the first line in the program. GO TO must always have a line number.

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

200 GO TO 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 Esc (equivalent to the Break key on a real Spectrum). The program will stop, saying

L BREAK into program

At the end of every statement, the program looks to see if this key is pressed; and if it is, then it stops. The Esc key can also be used when you are in the middle of printing, or using various other bits of machinery that you can attach to the computer - just in case the computer is waiting for them to do something but they're not doing it.

In these cases there is a different report,

D BREAK - CONT repeats

CONTINUE in this case (and in fact in most other cases too) repeats the statement where the program was stopped; but after the report L BREAK into program, CONTINUE carries straight on with the next statement after allowing for any jumps to be made.

Run the name program again and when it asks you for input type

n$ (after removing the quotes)

n$ is an undefined variable and you get an error report

2 Variable not found

If you now type

LET n$="something definite"

(which has its own report of 0 OK, 0:1) and

CONTINUE

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

In this case CONTINUE does a jump to the INPUT command in line 110. It disregards the report from the LET statement because that said 'OK', and jumps to the command referred to in the previous report, the first command in line 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, and CONTINUE will still work afterwards.

As we said before, the report L BREAK into program is special, because after it CONTINUE does not repeat the command where the program stopped.

Type

LIST (and Enter of course)

and when it asks scroll? (because it has filled up the display) press n for 'No'. The computer will give the report D BREAK - CONT repeats as though you had typed Esc. You might at some stage find out what happens if you press y instead of n; n, Space and Esc count as No, while everything else counts as Yes.

Now type

23 REM

and the edit cursor jumps to a blank line just after the newly inserted line 23; type

28 REM

and the edit cursor jumps to the new line 28. (In both cases, by typing a new line, you have caused the editor to move to that line in the listing.)

Experiment with moving the current line about by typing

line number REM

Using the program full of REMs above, type

LIST

and then n when it asks you scroll?. Now type

CONTINUE

CONTINUE is a bit quirky here, because the bottom part of the display goes blank; but you can restore normality with Esc. The reason is that LIST was the first command in the line, so CONTINUE repeats this command. Unfortunately, the first command in the line is now CONTINUE itself so the computer just sits there doing CONTINUE over and over again until you stop it.

You can vary this by replacing LIST with

:
LIST

(bear in mind that BASin splits lines at the ":" character) for which CONTINUE gives 0 OK (because CONTINUE jumps to the second command in the line, which is taken to be its end) or

:
:
LIST

for which CONTINUE gives N Statement lost (because CONTINUE jumps to the third command in the line, which no longer exists).

You have now seen the statements PRINT, LET, INPUT, RUN, LIST, GO TO, CONTINUE, NEW and REM, and they can all be used either as direct commands or in program lines - this is true of almost all commands in ZX Spectrum BASIC. RUN, LIST, CONTINUE and NEW are not usually of much use in a program, but they can be used.

Exercises

  1. Put a LIST statement in a program, so that when you run it, it lists itself.
  2. Write a program to input prices and print out the tax due (at 15 per cent). Put in PRINT statements so that the computer announces what it is going to do, and asks for the input price with extravagant politeness. Modify the program so that you can also input the tax rate (to allow for zero ratings or future changes).
  3. Write a program to print a running total of numbers you input. (Suggestion: have two variables called total - set to 0 to begin with - and item. Input item, add it to total, print them both, and go round again.)
  4. What would CONTINUE and NEW do in a program? Can you think of any uses at all for this?

Chapter 3

Chapter 1