ON GOSUB

ON GOSUB

Top  Previous  Next

 

The ON GOSUB statement enters one of a list of internal subroutines depending on the value of an expression.

 

 

Format

 

ON expr GOSUB label1{:}, label2{:}, label3{:}

 

where

 

expris an expression which can be resolved to a numeric value

 

label1...are statement labels. The trailing colons are optional and have no effect on the behaviour of the statement.

 

The ON GOSUB statement may be written over multiple lines by inserting a newline after the comma separating two labels.

 

Execution of the program continues at label1 if the value of expr (converted to an integer) is 1, label2 if it is 2 and so on. By default, a value less than one will use label1 and a value greater than the number of labels in the list will use the last label. The PICK.JUMP.RANGE option of the $MODE directive can be used to invoke the Pick style behaviour where an out of range value continues execution at the statement following the ON GOSUB.

 

See the GOSUB statement for more details on internal subroutines.

 

 

Example

 

ON X GOSUB SUBR1,

          SUBR2,

          SUBR3

 

This program fragment enters one of three internal subroutines depending on the value of variable X. If X could not be guaranteed to hold a valid value (1 to 3), error checking statements should be included to ease debugging of program errors.