ON GOTO Statement
Transfers control to a statement label determined by the value of a given expression.
Syntax
ON expression GO{TO} statementLabel {,statementLabel...}
Syntax Elements
expression Any expression that evaluates to an integer.
statementLabel The label of the statement to which control is to be transferred.
Operation
The ON GOTO statement evaluates expression, which must evaluate to an integer. If expression evaluates to 1, control is transferred to the first statement label. If expression evaluates to 2, control is transferred to the second statement label, and so forth.
If expression evaluates to less than one, the following message is displayed and the branch is taken to the first statement label:
[B22] BRANCH INDEX OF x IS ILLEGAL;
BRANCH TAKEN TO FIRST STATEMENT-LABEL!
If expression exceeds the number of statement labels in the list, the following message is displayed and the branch is taken to the last statement label:
[B23] BRANCH INDEX OF x EXCEEDS NUMBER OF STATEMENT-LABELS;
BRANCH TAKEN TO LAST STATEMENT-LABEL!
If any of the statement labels in the list refers to a label that does not exist, the program will not compile and an error message is printed.
Note
-
The program segment specified by statement label does not have to occur after the ON GOTO statement. It can occur anywhere in the program.
-
The behaviour of ON GOTO can be changed with the ONGO compatibility option.
See Also
GOTO statement, ON GOSUB statement.
Examples
ON M+N GOTO 40, 61, 5, 7
Transfers control to statement 40, 61, 5 or 7, depending on whether the value of M+N is 1 (or less than 1), 2, 3 or 4 (or more than 4), respectively.
ON C GOTO 25, 25, 20
Transfers control to statement 25 if C<3, to statement 20 in all other cases.
IF A GE 1 AND A LE 3 THEN
ON A GOTO 110, 120, 130
END
The IF statement assures that A is in the range for the computed ON GOTO statement.