Syntax
Jumps to a label. A backward jump repeats code, a forward jump skips code and can leave loops.
GOTO <label>
IF <condition> THEN GOTO <label> [ELSE GOTO <label>]
<label>:Also written : label
Parts
| <label> | A name followed by :, alone at the start of its line. Letters, digits and _; case does not matter. |
|---|---|
| <condition> | The test of a one-line IF that guards the jump. |
Good to know
- Allowed: a backward jump to a label at the same block level, a forward jump to a label later in the same block or in an enclosing block, and a jump from a subroutine back to a label of the main program, which restarts from there.
- Refused: a jump into a block (IF, FOR, WHILE…) from outside, or backward from inside a block to a label outside it: "GOTO … jumps across block boundaries".
- Not caught by the compiler, and broken when the program runs: two forward jumps to two different labels in the same block (including
IF c THEN GOTO a ELSE GOTO b), a restart jump written on one line in a subroutine, and a GOTO in the code afterLOOP. - A label cannot be named like a catalogue command:
Second:is refused. Two labels with the same name are a compile error: "Duplicate BASIC label". - A
:right after the first word of a line always declares a label.start: x = 1is the labelstartwithx = 1as its body. - Line numbers and
ON … GOTOare not supported. Prefer loops,EXITandCONTINUE: GOTO is the construct with the most limits.
Example
FOR i = 1 TO 10 IF i * 3 > 7 THEN GOTO foundNEXTConsole("not found")found:Console("found " + Str(i)) ' found 3In the manual : Control flow