BASIC ON Statement - fvdhoef/aquarius-plus GitHub Wiki

ON ... GOTO

TYPE: BASIC flow control statement

FORMAT: ON expression GOTO lineref_list

Action: Branches to one of several specified lines depending on the result of expression.

  • lineref_list is list of line numbers and/or labels separated by commas.
  • The value of determines which line number in the list will be used for branching.
    • expression is rounded down to an integer
    • If expression evaluates to 1, the first line number or label is branched to. If expression evaluates to 2, the second line number or label is branched to, and so on.
    • If the value of is zero or greater than the number of items in the list (but less than or equal to 255), BASIC continues with the next executable statement.
    • Illegal quantity results if the value of is negative or greater than 255.

Example:

100 ON L GOTO 150,_sub2,_sub3,390

Branches to line 150 if L is 1, to line _sub2 if L is 2, line _sub3 if L is 3, or line 390 if L is 390.


ON ... GOSUB

TYPE: BASIC flow control statement

FORMAT: ON expression GOSUB lineref_list

Action: As above but the BASIC program returns to the statement following the ON ... GOSUB when the first RETURN is executed.

  • Each line number or label should correspond to the first line of a subroutine.

plusBASIC enhancement

ON ERROR GOTO

TYPE: plusBASIC flow control statement

FORMAT: ON ERROR GOTO lineref

Action: Enables error trapping.

  • lineref is a line number or label specifying the first line of an error handling routine.
  • Once error trapping has been enabled all errors detected, including direct mode errors, will cause a jump to the specified error handling subroutine
  • Undefined line number results if a line number that does not exist is specified.
  • Undefined line label results if a line label that does not exist is specified.
  • If an error occurs during execution of an error handling subroutine, the BASIC error message is printed and execution terminates.

FORMAT: ON ERROR GOTO 0

Action Disables error trapping.

  • Subsequent errors will print an error message and halt execution.
  • An ON ERROR GOTO statement that appears in an error trapping subroutine causes MX BASIC to stop and print the error message for the error that caused the trap.
    • To prevent this, use RESUME or RESUME *line number* before the ON ERROR GOTO 0.
    • It is recommended that all error trapping subroutines execute an ON ERROR GOTO 0 if an error is encountered for which there is no recovery action.

Example:

10 ON ERROR GOTO 100
20 NEXT
30 PRINT "I get skipped"
100 PRINT ERR(-1)
110 PRINT ERR(0)
120 PRINT ERR(1)

Sets line 100 as the error handler, forces an error (NEXT without FOR) in line 20, then jumps to 100 and prints 100 for the error handler line, then the error number, then the line the error occurred on 20.

⚠️ **GitHub.com Fallback** ⚠️