Contents / Structure within BASIC / Previous chapter / Next chapter / Index


20 ON GOTO, ON GOSUB

There is often a need, in a computer program, to proceed in one of a number of directions. For example your program might present a "menu" of 8 options for the user to choose from. When the user has made the choice your program will need to branch off in the appropriate direction. There are a number of ways of doing this. Here is one in part of a program

100 MODE 7

110 PROCINTRO

120 REPEAT

130 PROCMENU

140 IF M=1 THEN PROCOscar7

150 IF M=2 THEN PROCOscar8

160 IF M=3 THEN PROCUOSAT

170 IF M=4 THEN PROCorbit

180 IF M=5 THEN PROCtransmit

190 IF M=6 THEN PROCshowfigs

200 IF M=7 THEN PROCMercator

210 IF M=8 THEN PROCLocator

220 IF M=9 THEN PROCgetdatetime

230 UNTIL M=-1

240 END

Lines 140 to 220 provide exits to a number of procedures all of which will automatically return to the main program. Which procedure is selected depends on the value of M as selected by the user during the procedure PROCMENU.

The above method is easy to understand and is recommended but there are other methods which have their place. The statement ON...GOTO also provides a number of exits.

100 ON M GOTO 1000,1200,1250,1600

would provide an exit to line 1000 of the BASIC program if M =

1. If M=2 then control will pass to line 1200 and so on.

An alternative format is

100 ON M GOSUB 1000,1200,1350

In this case control is passed to the subroutines indicated and then return to the next line.

Both these techniques are widely used but are less clear than the use of procedures as indicated at the beginning of this section.

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