On ... Gosub Command
Obsolete
This command is replaced by the Case command.
Purpose
To select one of a series of subroutines to execute, based on the numeric value of a variable or expression.
Syntax
on
variable
gosub
label [...label]
One label is required, and additional labels may be provided, separated by spaces. Each label must exist in the program.
What It Does
The numeric value of variable is used as an index to select for execution one of the subroutines whose label is listed. If the value of variable is 1, the first label is executed; if the value of variable is 2, the second is executed, and so forth. When the subroutine has been executed, execution returns to the command line next after the on ... gosub command.
If the value of variable is less than 1 or greater than the number of labels listed, no subroutine will be executed.
on time gosub early on_time late
In this example, when the value of time is 1, DataFlex executes subroutine early. When the value is 2, it executes on_time, and when it is 3, it does late. When time has any other value, execution passes to the line after on ... gosub.
Notes
-
You must provide a subroutine under
labelfor everylabelin everyon ... gosubcommand you use. The labelled routine must end with a return command. -
If the value of
variableis less than 1 or greater than the total number of labels on theon ... gosubcommand line, no subroutine will be executed. If you want this to constitute an error in your program, declare the error as follows:on music gosub rock country jazz if music gt 3 error 198 "INVALID MUSIC TYPE SELECTED"If the value of
musicis greater than 3, DataFlex declares Error 198. -
You may precede
on ... gosubwith indicators and if tests. -
The
on ... gotocommand is similar to theon ... gosubcommand with the exception that theon ... gotocommand does not return to the line after theon ... gotocommand. -
If
variableis a number that includes a fractional (decimal) value, the value will be truncated to an integer value before theon ... gosubselection is made.