GOTO

GOTO Label

Jump to a label declared elsewhere in the function.

GOTO and labels cannot be used to enter a control structure. This is forbidden, even if the variables are preset.

= 18
Goto LOOP3 ' This is forbidden
    
For X = 20 TO -2 STEP -2
    
LOOP3:
  Print "Loop2 "; X
    
Next

Computed GOTO

Since 3.19

GOTO Expression

Since Gambas 3.19, GOTO (or GOSUB) can take any expression as argument.

This expression must return an integer that represents the label you want to branch.

This integer is obtained by using labels as expression. In that case, a labels is transformed into an integer index representing it (only in the function where the label is defined).

Only labels that are outside of any control structure can be a target of a computed GOTO or GOSUB.

Example

Dim JumpArray As Integer[]

JumpArray = [LABEL_1, LABEL_2, LABEL_3]

Goto JumpArray[Rand(1, 3)]

LABEL_1:
  Print "One !"
LABEL_2:
  Print "Two !"
LABEL_3:
  Print "Three !"

See also