GOTO Statement

The GOTO statement transfers control to the line with the specified statement label, from which the program sequentially proceeds until it reaches the end of the program or another GOTO statement.

Format

GO[TO] label

ON expr GO[TO] label1 label2 ...

Description

The GOTO statement has two forms of execution: the direct branch to a specified label, and the computed branch to one of several labels.

When the first form is used, program control is directly transferred to the line starting with the specified label.

The ON...GOTO syntax variation, or computed GOTO, may be used to branch to different labels according to the value of expr. expr is evaluated and truncated into an integer n, and the program branches to the statements starting at the nth label on the ON...GOTO line. If expr evaluates to a number less than 1 or greater than the number of statement labels listed, no action is taken.

The colon is optional in the statement label reference. Furthermore, the statement label may be anywhere in the program; that is, it can either precede or follow the referencing GOTO statement, as long as it is before the end of the program. If the specified statement label is not found, an error message is printed at compile time.

Example

This example shows how a GOTO statement may be used to conditionally jump to another point in the program.

IF CARD(1,1) + CARD(1,2) = 21 THEN

   PRINT "BLACKJACK!  YOU WIN!"

   GOTO GAMEEND

   END ELSE ...

      .

      .

      .

GAMEEND:

   PRINT "WANT TO PLAY AGAIN?" :

      .

      .

      .

See Also

Statement and Function Reference