Description

Transfers program control to one of several statement labels. Branching is determined by computing an index expression.

Syntax

On index GoTo label [ [ , label ] ...]

Parameters

The On...GoTo statement has the following parameters.

ParameterDescription
indexWhen the On...GoTo statement is encountered, the current value of index is determined, and the program transfers control to the proper label.

If index evaluates to 1 (one), control will branch to the first statement label; if the expression evaluates to 2 (two), the program will branch to the second statement label; and so on. When index is first evaluated, the result is truncated to an integer value. If the resulting value is less than 1 (one) or if the value exceeds the number of statement labels, an error message is generated.

labelThe labels that are designated in the list may either precede or follow the On...GoTo statement, but they must be valid labels that are defined in the program. If the specified label does not occur in the program, a compiler error message will result. The On...GoTo statement must be written on one line with the statement labels separated by commas as in the following example:
On T GoTo Debit, Credit, Update

Note: On...GoTo is an efficient form of branching based on an index. It compiles as a jump table (as compared to the Case construct which offers comparative functionality, but is compiled as a series of nested If statements).

See also

GoSubGoToOn...GoSub

Example

/* The value of the variable Action will determine where the program will be transferred. 
A value of 1 (one) will transfer control to label ReadRow, a value of 2 to SaveRow, and so on. */
On Action GoTo ReadRow, SaveRow, DeleteRow
  • No labels