ON GOTO statement

Purpose

Send program flow to one of several possible destinations based on the value of a numeric expression.

Syntax

ON n GOTO {label | line_number} [, {label | line_number}] ...

Remarks

n is a numeric expression ranging from 1 to 255, and label or line_number identifies a statement in the program to branch to.  The nth label is branched to; for example, if n equals 4, the fourth label in the list receives control.  If n is less than one or greater than the number of labels in the list, program execution continues with the statement that immediately follows the ON GOTO statement.

ON GOTO behaves exactly like ON GOSUB, except that it performs a GOTO rather than a GOSUB.  This means that the program retains no memory of where the branch originated.  ON GOTO can only branch to labels or line numbers that have the same scope as the ON GOTO statement.

The SELECT and IF blocks also perform multiple branching, and are more flexible than ON GOTO.  See the GOTO entry for a discussion of ways to avoid using GOTOs in your programs.

Note that ON GOTO (and ON GOSUB) have been internally optimized to produce greater run-time performance than was possible with previous versions of PowerBASIC.

See also

GOTO, IF block, ON GOSUB, SELECT

Example

SUB MainEx

  FOR I& = 1 TO 3

    ON I& GOTO OneHandler, TwoHandler, ThreeHandler

    Back:

  NEXT I&

  EXIT SUB

 

OneHandler:

  Message$ = "Handler number" + STR$(I&)

  GOTO Back

 

TwoHandler:

  Message$ = "Handler number" + STR$(I&)

  GOTO Back

 

ThreeHandler:

  Message$ = "Handler number" + STR$(I&)

  GOTO Back

END SUB

Result

Handler number 1

Handler number 2

Handler number 3