Contents / Part 1 / Previous chapter / Next chapter / Index


4 A simple program

In the previous sections we have been giving the computer commands which it obeys immediately. The problem with this technique is that you have to wait until the computer has completed one command before you can give it the next one. If the computer takes a long time to work out one of the problems you have set it, then you may have to waste an awful lot of time just sitting there waiting for it. For example if you want your computer to work out the number of 5p, 10p and 50p coins that you will need to pay the wages at the end of the week the computer will take a fair time to calculate all the wages before it can sort out the coins required.

The same problem arises when you take a car into a garage to be serviced. You could for example stand by the mechanic and say "Right, first of all I want the oil changed" and then you could wait for him to change the oil. When he had completed that you could then say "Right, now I want you to replace the bulb that has blown in one of the front headlights" and then you could wait for him to do that job. And thirdly you might say "The exhaust is making, a bit of noise, so I want you to put the car up on the ramp and check the exhaust".

Inevitably you would spend a great deal of time waiting, for the mechanic to complete the job that you had set him before you could give him the next job. There is a far more efficient way of doing things; when you go into the garage you give the mechanic a whole set of instructions, for example

Once you have given your set of instructions and checked that the garage understands what they have to do, you can then walk off and have a cup of coffee and then go back expecting the job to be done. Now the same thing applies with a computer. It is far better to give it a whole set of instructions and set it going on something while you wander off and have a cup of coffee. "Writing a computer program" is nothing more than giving a set of instructions.

If you give the computer a command like

PRINT "HOW ARE YOU"

then the computer will do that immediately. On the other hand, if you give the computer a statement

10 PRINT "HOW ARE YOU"

then the computer will regard that as instruction number 10 and it will not do it straight, away, but expect other instructions. to follow. Instruction number 10 is usually referred to as Line 10 etc. Again: if there is a line number then the statement is part of a program; if there is no line number then it is a command which the computer must obey immediately.

When you have given the computer a set of instructions and you then want it to carry them out, you type the word RUN on the keyboard. The computer will then carry out the instructions that you asked it to do one at a time and in line-number order. In other words, it will "execute the program" that you have typed in. Just to check that you have got the idea of what is going on, here is a small program that you can type in.

10 REPEAT

20 PRINT "GIVE ME A NUMBER";

30 INPUT B

40 PRINT "12 TIMES ";B;" IS ";12*B

50 UNTIL B=0

When you RUN the program line 20 will print the message

GIVE ME A NUMBER

on the screen.

Line 30 will print a question mark on the screen and wait for you to type in a number (followed by RETURN as usual). The number you type in will become the value of the variable "B".

Line 40 will first print the words 12 TIMES followed on the same line by the number you typed in, followed on the same line by the word IS followed by the result of the calculation.

The semi-colons tell the computer to print the next item on the same line as the previous one and right up against it.

Line 50 sends the computer back to line 10 unless B=0, when the program will stop.

Another way of stopping the program is to press the "panic button" which is marked ESCAPE on the keyboard. It is at the top left of the keyboard. If the computer seems to be ignoring you because it's too busy running a program you can nearly always get its attention by pressing the ESCAPE button. When you do that it will stop running your program and print a > prompt to show that it has stopped the program and that you have command again.

When the computer shows a > it is in Command Mode. You can change your program, give it command for immediate execution, or tell it to RUN the program (in its memory) again. It doesn't forget a program when you press ESCAPE.

If the computer is in command mode (in other words the last thing on the screen is >) then you can command it to print the program in its memory by typing

LIST

and pressing RETURN.

The computer will then give a list of the program on the screen for you to check. If you discover that you have made an error, for example that you have got something wrong in line 20, then it is easy to correct the error. There are two ways of correcting major errors:

1 Retype the whole line

2 Use the screen editor

Using the screen editor

There is a group of six keys on the right hand side of the keyboard which can be used to edit, or alter, program lines that are displayed on the screen. Four of the keys have arrows on them and are coloured a lighter brown than most of the other keys. These keys enable you to move a flashing cursor around the screen to a line that you wish to edit. As soon as you press one of these keys the computer enters a special "editing mode" where it displays two cursors. The large white block is called the WRITE CURSOR and it shows you where anything that you enter will appear. The other small, flashing cursor - the READ CURSOR - is the one that can be moved around by the arrow keys.

Try moving the read cursor, by using the arrow keys, until it is under a letter at the start of a word and then press the COPY key several times. As you will see the COPY key copies everything that the read cursor passes under intro the new input line. Half way through copying a line you can always use to move the read cursor to some new place on the screen before using COPY again to copy some other text to your new input line. The DELETE key can always been used to delete characters from the input line.

You can also type new characters in at any time instead of using the COPY key. When your new input line is complete just press RETURN in the usual way.

Try the following: clear the screen with the command CLS and then LIST the program. It should include the line

20 PRINT "GIVE ME A NUMBER";

If not, then type that line in so that you can edit it. Suppose that you wanted to insert the word BIG so that line 20 reads

20 PRINT "GIVE ME A BIG NUMBER";

then all you have to do is to press the up-arrow cursor key until the small flashing line is positioned under the 2 of 20. Then press the COPY key to copy the first part of line 20 to a fresh line at the bottom. When the cursor reaches the space after the A where you want to insert the word BIG, just type it in with a space in front - it will appear on the bottom line. Then COPY the rest of the line 20. the space after the A becoming the space after BIG. At the end press RETURN.

Now try changing the program already in the computer once again by doing the following things:

  1. List the program by using the LIST command.
  2. Practice using the cursor control and COPY keys to alter line 20 so that it reads:
    20 PRINT "NOW GIVE ME A BIG NUMBER";
  3. Now add these new lines. Don't forget to press RETURN after each one.
     5 CLS
    25 REPEAT
    35 IF B<1000 THEN PRINT "I SAID A BIG NUMBER"
    37 UNTIL B>1000

    Note: it doesn't matter in what order you type in new lines. The computer will automatically put them into numerical order. You will see that this is true by typing

    LIST RETURN

    these extra lines tell the computer to reject any number .smaller than 1000 and to go on going back to line 30 to ask for a new number until that number is greater than 1000. The symbol < means 'is smaller than', and > means 'is greater than'. IF and THEN are self explanatory.

  4. Now RUN the program.

    >RUN

    NOW GIVE ME A BIG NUMBER? 16

    I SAID A BIG NUMBER

    ?20

    I SAID A BIG NUMBER

    ?2000

    12 TIMES 2000 IS 24000

    NOW GIVE ME A BIG NUMBER?

This program will go on running until you press ESCAPE. If you look you will see that if you give the value 0 for the number, the program never reaches line 50, so it can never end unless you press the panic button!

Removing part of a program

Quite often you will want to delete a whole line or group of lines in your program. This is easy to do but don't forget that if you type in a new line 20 (for example), it will automatically remove the old line 20 and replace it with your new one. If you just want to delete a line completely then type in just the line number and press RETURN thus:

20 RETURN

To delete a whole set of line numbers, for example, lines 50 to 70 inclusive, you can type

DELETE 50, 70

You cannot get these lines back once they are deleted - unless you copy them off the screen, so use this with care.

After you have deleted several lines - or if you have typed in lots of new lines - you often find that you have a very odd set of line numbers. The command

RENUMBER

will make the computer go through your whole program renumbering all the lines so that they are given line numbers in a tidy sequence. Here is an awful example of programming style - but it will illustrate the renumber command. Don't bother to type it in - just look at it.

>LIST

1 REM ** GOTO GOTO GOTO

2 REM WITH ACKNOWLEDGEMENTS TO

3 REM "COMPUTERS IN SCHOOLS"

4 REM THE JOURNAL OF MUSE

15 GOTO 100

16 GOTO 95

40 N=N+1

44 END

57 IF N=18 THEN PRINT "GOTO OR NOT TO GOTO"

60 IF N>35 THEN GOTO 110

78 GOTO 40

95 PRINT "**THE GOTO SHOW**": GOTO 40

100 N=0: GOTO 16

105 PRINT "GOT TO GOTO GOTO NOW"

110 GOTO 44

115 PRINT "GOTO OR NOT TO GOTO"; GOTO 60

>RENUMBER

>LIST

10 REM ** GOTO GOTO GOTO

20 REM WITH ACKNOWLEDGEMENTS TO

30 REM "COMPUTERS IN SCHOOLS"

40 REM THE JOURNAL OF MUSE

50 GOTO 130

60 GOTO 120

70 N=N+1

80 END

90 IF N=18 THEN PRINT "GOTO OR NOT TO GOTO"

100 IF N>35 THEN GOTO 150

110 GOTO 70

120 PRINT "**THE GOTO SHOW**": GOTO 70

130 N=0: GOTO 60

140 PRINT "GOT TO GOTO GOTO NOW"

150 GOTO 80

160 PRINT "GOTO OR NOT TO GOTO"; GOTO 100

>RUN

**THE GOTO SHOW**

As you will see, the RENUMBER command has not only renumbered all the line numbers but it has accurately renumbered the references to line numbers which occur within the program itself - namely after the statements containing the keyword GOTO. (This gives the computer the instruction to go to a particular line number and carry out the instruction it finds there.)

Removing a program

If you want to write a new program you will want to remove the old program from the computer's memory. This can be done by using the command NEW, or by pressing the BREAK key. In either case, if you regret having lost your program, type OLD and press RETURN and, providing you haven't begun to type in a new program, the old one should reappear.

You can always check what's in the memory by typing LIST.

Try experimenting with these various commands on the program you have typed in.

Exit: BBC Microcomputer User Guide; Kasoft Typesetting; Archer


The BBC Microcomputer User Guide was written by John Coll and edited by David Allen for the British Broadcasting Corporation.

Optical character recognition and original formatting effort by Mark Usher.

HTML version maintained by: Kade "Archer" Hansson; e-mail: archer@dialix.com.au

Last updated: Monday 12th February 2001