basic.on.gosub Statement/BASIC Program

basic.on.gosub

Command basic.on.gosub Statement/BASIC Program
Applicable release versions: AP, R83
Category BASIC Program (486)
Description The "on .. gosub" statement transfers control to a local subroutine designated by a specific statement label according to the positional value of the "index.expression". The syntax also allows being specified as "on ... go sub ..." (it allows a space between "go" and "sub").

The "index.expression" produces a number that corresponds to the position of the corresponding statement label within a list of statement labels separated by commas (,).

The "index.expression" is truncated to an integer and the program executes the subroutine identified by the label number in the statement that corresponds to the truncated expression.

For example, if the expression evaluates to 1, the first subroutine is executed. If the "index.expression" evaluates to a number less than one, or to a number greater than the number of statement labels, no action is taken. Execution continues at the first executable statement following the "on ... gosub".

The local subroutine must be terminated with a "return" statement. At the end of the subroutine, the program returns to the statement following the "on...gosub" statement.

For ease of reading, the "on..gosub" statement.label list can be spread over multiple lines if each continued line is terminated with a comma (,).
Syntax on index.expression gosub statement.label{, statement.label...}
-or-
on index.expression gosub statement.label,...,
statement.label,
statement.label
Options
Example
loop
  print '1) doit  2) printit  3) deleteit'
  input response
until num(response) do repeat
if response >=1 and response <= 3 then
    on response gosub 8000,9000,9500
end

If "response" is 1, this branches to subroutine 8000. If it is 2, it 
branches to subroutine 9000. If it is 3, it branches to subroutine 9500.

print 'a) doit  b) printit  c) deleteit'
input response,1
on index('abc',response,1) gosub doit,printit,deleteit

In this example, the result of the "index" function is used as the 
"index.expression" to determine which subroutine is called.  If the 
response is "a", then subroutine "doit" is called, and so 
on.

on response gosub 10,20,30,40,50,60
,70,80,90,100,110,120,130

When the value of the "index.expression" has a large number of 
possible branches, readability is improved by using the multiple line syntax.
Purpose
Related basic.statements
basic.on.goto
basic.statement.labels
basic.return