on gosub - branch to subroutine based on expression


on NumExpr gosub LineNum[, LineNum] . . . 
on NumExpr gosub LineLab[, LineLab] . . . 


The on gosub statement transfers control to one of several subroutines based on the value of a numerical index, supplied by the NumExpr argument.  NumExpr must be a numeric value and if it is not an integer, it is rounded to integer before use as the index.  The LineNum and LineLab arguments are assumed to be the starting line numbers of one or more subroutines.  LineNum and LineLab arguments may be mixed in the same statement.  The value of the index selects a LineNum or LineLab argument to which to transfer control based on direct correspondence between the index value and the position of the arguments in the list.  An index value of 1 selects the first argument, 2 the second, and so on.  Once a return statement is executed by the subroutine selected, execution resumes at the statement following the on gosub statement. 

If the value of NumExpr is zero or greater than the number of items in the list, no transfer occurs and BASIC continues with the next executable statement. 


In the on gosub statement in line 100, a branch to line 100 occurs if x equals 1, to line 200 if x equals 2, to line 300 if x equals 3, and to line 400 if x equals 4.  If x is equal to 0 (zero) or is greater than 4, then the program just goes on to the next statement. 
	    a = 1
	    on a gosub 100, 200, 300, 400
	100 rem start of subroutine for a=1
	900 return


gosub, goto, on goto


If NumExpr is negative, an "Illegal function call" error occurs. 

from The Basmark QuickBASIC Programmerís Manual by Lawrence Leinweber