ON Index [GOTO | GOSUB] Label0 {, Label1, Label2, ...}
  ON Index = Value0 {, Value1, Value2, ...} [GOTO | GOSUB] Label0 {, Label1, Label2, ...}

Function
Jump to the address specified by Index  (if in range).

Quick Facts
  All SX Models
 Limit of Label entries  256

Explanation
The ON... instruction is useful when you want to write something like this:

  IF idx = 0 THEN Case_0                        ' idx = 0: jump to label "Case_0"
  IF idx = 1 THEN Case_1                        ' idx = 1: jump to label "Case_1"
  IF idx = 2 THEN Case_2                        ' idx = 2: jump to label "Case_2"

You can use ON... with GOTO to organize this into a single statement:

  ON idx GOTO Case_0, Case_1, Case_2

This works exactly the same as the previous IF...THEN example. If the value isn't in range (in this case if value is greater than 2), ON...GOTO does nothing and the program continues with the next instruction after ON...GOTO. ON...GOSUB is identical, however, the program should be designed such that a RETURN is placed at the end of the code at Label.

A variant of the ON... instruction essentially combines LOOKDOWN into the syntax. For example:

Main:
  cmd = RX_BYTE
  LOOKDOWN cmd, "L", R", "S", cmd
  ON cmd GOSUB Robot_Left, Robot_Right, Robot_Stop
  GOTO Main  

... can be combined to:

Main:
  cmd = RX_BYTE
  ON cmd = "L", R", "S" GOSUB Robot_Left, Robot_Right, Robot_Stop
  GOTO Main  

Related instructions: IF ... THEN and BRANCH