On GoTo
Syntax
On Index Variable GoTo Label1 {,...Labeln }
Overview
Cause the program to jump to different locations based on a variable index. On a PICmicro™ device with only one page of memory. Exactly the same functionality as Branch.
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. A maximum of 255 labels may be placed after the GoTo, 256 if using an 18F device.
Example
Device = 18F26K40 ' Tell the compiler what device to compile for
Declare Xtal = 16 ' Tell the compiler the device will be operating at 16MHz
Declare Hserial_Baud = 9600 ' Choose the Baud rate for HRsoutLn
Dim bIndex as Byte
bIndex = 2 ' Assign Index a value of 2
Start: ' Jump to label 2 (Label_2) because Index = 2
On bIndex GoTo Label_0, Label_1, Label_2
Label_0:
bIndex = 2 ' Index now equals 2
HRsoutLn "Label 0" ' Display the Label name on the serial terminal
DelayMs 500 ' Wait 500ms
GoTo Start ' Jump back to Start
Label_1:
bIndex = 0 ' Index now equals 0
HRsoutLn "Label 1" ' Display the Label name on the serial terminal
DelayMs 500 ' Wait 500ms
GoTo Start ' Jump back to Start
Label_2:
bIndex = 1 ' Index now equals 1
HRsoutLn "Label 2" ' Display the Label name on the serial terminal
DelayMs 500 ' Wait 500ms
GoTo Start ' Jump back to Start
The above example we first assign the bIndex variable a value of 2, then we define our labels. Since the first position is considered 0 and the variable bIndex 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.
The On GoTo command is primarily for use with PICmicro™ devices that have one page of memory (0-2047). If larger PICmicros are used and you suspect that the branch label will be over a page boundary, use the On GoToL command instead.

