ON ... GOTO
ON … GOTO
Section titled “ON … GOTO”Syntax
Section titled “Syntax”ON <expression> GOTO <label0>, <label1>, <label2>, ..., <labelN>ON <expression> GO TO <label0>, <label1>, <label2>, ..., <labelN>
Transfers control to one of a series of specified line numbers or labels based on the value of an expression.
Parameters
Section titled “Parameters”<expression>
: An integer expression that determines which label to jump to. It can be any numeric expression.<label0>, <label1>, <label2>, ..., <labelN>
: A comma-separated list of line numbers or labels. These are the destinations for control transfer based on the value of<expression>
.
The value of expression is converted to a uByte (Unsigned Byte), so, for example, a value of 256 will be converted to 0. If the expression value is greater than the number of labels, this instruction will be ignored and the execution will continue normally.
This sentence uses a jump table for faster execution.
Example with ON … GOTO
Section titled “Example with ON … GOTO” 5 REM Random value between [0..3] both included10 LET X = INT(RND * 4)20 ON X GOTO 50, 100, 15030 PRINT "Invalid choice: "; X40 GOTO 1050 PRINT "You chose option 0"60 END100 PRINT "You chose option 1"110 END150 PRINT "You chose option 2"160 END
In this example,
- if
X
is 0, the execution will jump to line 50 - if
X
is 1, it will jump to line 100; - if
X
is 2, it will jump to line 150. - If
X
is outside this range, theON ... GOTO
sentence will be ignored.
The program will print “Invalid choice” and ask for input again.
Remarks
Section titled “Remarks”- The
<expression>
is evaluated, and if it results in a value outside the range 0 to N (where N is the number of labels), no action is taken. ON ... GOTO
is a structured programming alternative to multipleIF...THEN...GOTO
statements.
Compatibility
Section titled “Compatibility”- This statement is not compatible Sinclair BASIC.