On GoTo
Syntax
On Index Variable GoTo Label1 {,...Labeln }
Overview
Cause the program to jump to different locations based on a variable index.
Parameters
Index Variable is a constant, variable, or expression, that specifies the label to jump to. Label1...Labeln are valid labels that specify where to branch to.
Example
Device = 24HJ128GP502 ' Select the device to compile for
Declare Xtal = 16
Dim Index as Byte
Cls ' Clear the LCD
Index = 2 ' Assign Index a value of 2
Start: ' Jump to label 2 (Label_2) because Index = 2
On Index GoTo Label_0, Label_1, Label_2
Label_0:
Index = 2 ' Index now equals 2
Print At 1,1,"Label 0" ' Display the Label name on the LCD
DelayMs 500 ' Wait 500ms
GoTo Start ' Jump back to Start
Label_1:
Index = 0 ' Index now equals 0
Print At 1,1,"Label 1" ' Display the Label name on the LCD
DelayMs 500 ' Wait 500ms
GoTo Start ' Jump back to Start
Label_2:
Index = 1 ' Index now equals 1
Print At 1,1,"Label 2" ' Display the Label name on the LCD
DelayMs 500 ' Wait 500ms
GoTo Start ' Jump back to Start
The above example we first assign the index variable a value of 2, then we define our labels. Since the first position is considered 0 and the variable Index equals 2 the On GoTo command will cause the program to jump to the third label in the list, which is Label_2.
Notes.
On GoTo is useful when you want to organise a structure such as: -
If Var1 = 0 Then GoTo Label_0 ' Var1 = 0: go to label "Label_0"
If Var1 = 1 Then GoTo Label_1 ' Var1 = 1: go to label "Label_1"
If Var1 = 2 Then GoTo Label_2 ' Var1 = 2: go to label "Label_2"
You can use On GoTo to organise this into a single statement: -
On Var1 GoTo Label_0, Label_1, Label_2
This works exactly the same as the above If...Then example. If the value is not in range (in this case if Var1 is greater than 2), On GoTo does nothing. The program continues with the next instruction.

