Acorn Computers BBC Micro Model B w/8271 — Acorn BBC Micro Model B w/8271 User Guide Manual PDF

276 ON Purpose To alter the order in which BASIC executes a program by jumping to one of a selection of lines depending on the value of a particular variable. The word ON is used with three other keywords GOTO , GOSUB and ERROR . For example: ON value GOTO 800,920,100 ELSE 7300 ON result GOSUB 8000,8300,120,7600 ON ERROR GOTO 9000 ON ERROR GOSUB 2001 First: ON X GOTO 1100,1210,1450,1600,1950 If the value X is equal to 1 then the program will go to line 1100. If X=2 then the program will go to line 1210. If X=3 then line 1450 and so on. What is it used for? Suppose that you are counting coins put into a machine and you want to offer different things if one, two or three coins are put in.

The program which follows illustrates, in outline, how ON GOTO will help. 450 REM the variable COINS gives the number 460 REM of coins inserted 500 ON COINS GOTO 550,600,650 550 PRINT "One coin buys a biscuit" 560 REM gives him a biscuit somehow 590 GOTO 1000 600 PRINT "Two coins can buy tea or coffee" 610 GOTO 1000 650 PRINT "Three coins can buy a piece of cake" 660 REM something else in here as well 690 GOTO 1000 1000 REM all the routines end up here Secondly: ON X GOSUB 2200,2300,2400,2500 ON can also be used with GOSUB instead of GOTO . See GOSUB for an explanation of subroutines. ON X GOSUB provides a neat way of using different subroutines in different situations.