in BASIC ROM:
|List of all BASIC keywords|
Typ: Command General Programming-Syntax: ON <index> GOSUB|GOTO <line>,<line>,...
In Commodore BASIC V2, the command ON is part of a structure which jumps to a specific line in the given list of BASIC <line> numbers, either as an unconditional GOTO or as a subroutine call through GOSUB. If <index> (which must be either a floating point or integer expression) equals 1, the first <line> on the list is taken, if <index> equates to 2, the second line number is taken.
If the numerical <index> expression evaluates to a non-integer result, ON will round it down to nearest lower integer, as if the INT function had been used on the result; e.g. an <index> of 2.2 resolves to 2, and so the second line number on the list is taken.
If the <index> expression, after eventual rounding, exceeds the count of line numbers given, ON does not GOTO or GOSUB anywhere, and program execution continues with the subsequent command.
Attempting to use a string expression as the <index> causes the ?TYPE MISMATCH ERROR in line error message to appear. A negative <index> yields an ?OVERFLOW ERROR IN line. As with "normal" GOTO and GOSUB statements, referring to a non-existent line number causes an ?UNDEF'D STATEMENT ERROR in line as soon as ON tries to call it. Other calculation failures may raise other error messages, e.g. ?DIVISION BY ZERO ERROR or ?OVERFLOW ERROR.
Important: Two-letter keywords like ON "blocks" the use of "on" as the name of a BASIC variable; you cannot create e.g. a string variable named ON$; any reference to it in the BASIC code will cause a ?SYNTAX ERROR IN line.
Starting with BASIC 3.5 the keyword ON can be also used in disk oriented BASIC commands to specify the device number (in addition to or instead a comma which separates parameters).
ON ... GOTO example
10 INPUT "Write an integer number, please "; A 20 PRINT: ON A GOTO 1000,2000,3000 999 END 1000 PRINT "1. jump" 1001 A=A+1: GOTO 20 2000 PRINT "2. jump" 2001 A=A+1: GOTO 20 3000 PRINT "3. jump" 3001 A=A+1: GOTO 20
ON ... GOSUB example
10 INPUT "Write a integer number, please"; A 20 ON A GOSUB 1000,2000,3000: PRINT 999 END 1000 PRINT "1. Subroutine" 1001 A=A+1: RETURN 2000 PRINT "2. Subroutine" 2001 A=A+1: RETURN 3000 PRINT "3. Subroutine" 3001 A=A+1: RETURN
ON ... GOSUB with calculated input
20 ON A/4 GOSUB 1000,2000,3000
The number variables can be used directly for easy calculations.
An equivalent to IF ... GOTO
10 IF A > 0 GOTO 99
10 ON -(A > 0) GOTO 99
but with the difference that the IF version ignores everything after the GOTO until the line ends. In contrast, the ON version continues right after a given colon (":") in case the GOTO is no taken.
Usage in disk oriented commands
DIRECTORY ON U9