R.T.Russell Home Page

Appendix A - Line Numbers and the Dreaded GOTO




Most seasoned programmers will throw up their hands at the mention of line numbers and GOTO. They are not considered polite conversation and ladies should be asked to leave the room before being discussed. There are an equal number of programmers who swear by them, rather than at them, insisting that with proper use they are valid. The truth is probably somewhere between the two. Don't e-mail me on this as it is a point over which many kilobytes have been exchanged without convincing anyone to change sides. At some point you will come across them in other people's programs so it would be remiss not to mention them.

Line Numbers

Traditional BASICs, of which this is one (and that's something to be proud of), always had line numbers. Though their use has fallen somewhat by the wayside, in BBC BASIC they are still useful. If you make a mistake on a line, when the program is run Basic will present you with an error message specifying the line on which it found the mistake. BBC BASIC allows lines in the range 0 - 65535. It is traditional to start with 10 and go up in increments of 10.

Lines must be in numeric order and no duplicates are allowed. If you need to insert another line, make sure it falls between the two numbers before and after or BBC BASIC will complain. If you need to insert lots of lines, BBC BASIC allows you to enter the lines without numbers and will renumber them afterwards if you ask it nicely. To demonstrate this, enter the following revolutionary piece of code:

REM Line number demo
PRINT "Hello, world"
WAIT 100
PRINT "That's all folks!"
END
Now, from the toolbar select Renumber. A dialogue appears: accept the default settings which start at 10 and go up in 10s and press OK. The listing is updated. For a bigger program, this is a lot easier than renumbering by hand. Run the program to prove it works. It is also possible to remove line numbers. To do this, invoke the renumber box again and tick 'Remove unused line numbers'. 'Unused ' will be qualified in the next section on GOTO, but it is possible to get a program to leap around within itself during execution. The target of these jumps can be a line number, so if a line jumps to a line number, this number is classed as used and will not be removed.

GOTO

GOTO is a command that is used in conjunction with a line number. When the program executes, a GOTO statement will cause an immediate jump to the line number specified and execution will continue from there. For example, in our well worn circle program, we can make it loop endlessly by inserting line 50.

10 REM Area of a circle
20 INPUT "Enter the radius " Radius
30 Area=PI*Radius^2
40 PRINT "Area of the circle is " ;Area
50 GOTO 20
60 END
GOTO can be used as a quick and dirty way to skip a block of code when testing:
400 REM Stuff
410 GOTO 500
420 REM Help screens you've already tested
430 REM ...
440 REM ...
450 REM ...
460 REM ...
470 REM ...
480 REM ...
490 REM ...
500 REM Interesting stuff starts here
GOTOs can be made conditional:
10 REM Area of a circle
20 INPUT "Enter the radius " Radius
30 Area=PI*Radius^2
40 PRINT "Area of the circle is " ;Area
50 INPUT "Another go (Y/N)"; Reply$
60 IF Reply$="Y" OR Reply$="y" THEN GOTO 20
70 END
And in single line IFs (don't ever jump out of or into a multi-line block with GOTO) you can miss the THEN or GOTO out completely:
50 IF TestResult>50 THEN 1000
Or:
50 IF TestResult>50 GOTO 1000
But never:
50 IF TestResult>50 1000
Overindulgence can lead to what is commonly known as spaghetti code, which is why people hate it so much. Don't bother to type this in but see if you can follow it:
10 GOTO 60
20 PRINT "Help!"
30 PRINT " lines 20 and 50 ";
40 GOTO 80
50 PRINT "I'm stranded!"
60 PRINT "Why are";
70 GOTO 30
80 PRINT "never executed?"
90 END
Tip:  Not every line needs a number
 You don't have to have a line number on each line, only the ones that are a target for a GOTO or similar instruction. The following is perfectly legal:

   REM Area of a circle
20 INPUT "Enter the radius " Radius
   Area=PI*Radius^2
   PRINT "Area of the circle is " ;Area
   GOTO 20
   END
This techinque can be used when converting old programs until you have the structure just right and can eliminate the GOTOs completely. When you select 'Remove used line numbers' from the renumber dialog, this would be the result.


The very, very early (we're talking late 60s or early 70s) BASIC programmers had to use GOTO because there wasn't a choice. BBC BASIC has many options to help with conditional execution and repeating blocks of code. It's largely not needed, but, as previously stated, you will see code with it in. If you do use GOTO:

a) Keep jumps short, preferably on one page so you can see the destination.
b) Don't jump in and out of blocks of code like PROCs, loops or IFs (can't emphasize this enough), you completely mess BBC BASIC's internal stacks up when you do this.
c) Never admit to it in public, you'll cause a riot.

Left CONTENTS

APPENDIX B Right


Best viewed with Any Browser Valid HTML 4.0!
© Peter Nairn 2006