BASIC stamp manual v2.2

ON – BASIC Stamp Command Reference
Page 290 BASIC Stamp Syntax and Reference Manual 2.2 www.parallax.com
You can use ON to organize each of these two examples into single
statements:
ON value GOTO Case_0, Case_1, Case_2 ' "GOTO" jump table
- or -
ON value GOSUB Case_0, Case_1, Case_2 ' "GOSUB" jump table
This works like the previous IF...THEN example. If the value isn’t in range
(in this case if value is greater than 2), ON does nothing and the program
continues with the next instruction after ON.
See the GOTO and GOSUB command descriptions for more information.
Demo Program (ON-GOTO.bs2)
' ON-GOTO.bs2
' This program shows how the value of idx controls the destination of the
' ON...GOTO instruction.
' {$STAMP BS2}
' {$PBASIC 2.5}
idx VAR Byte
Main:
DEBUG "idx: ", DEC idx, " "
ON idx GOTO Case_0, Case_1, Case_2 ' if idx = 0..2 goto label
DEBUG "ON..GOTO target error.", CR ' message if idx is out of range
Update:
idx = idx + 1 // 4 ' force idx to be 0..3
PAUSE 1000
GOTO Main
Case_0:
DEBUG "Running Case_0 routine", CR
GOTO Update
Case_1:
DEBUG "Running Case_1 routine", CR
GOTO Update
Case_2:
DEBUG "Running Case_2 routine", CR
GOTO Update
All
2
NOTE: This example program can be
used with all BS2 models by changing
the $STAMP directive accordingly.