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-existant line number causes an ?UNDEF'D STATEMENT ERROR in line as soon as ON tries to call it.
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.
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" 2001 A=A+1: GOTO 20
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" 2001 A=A+1: RETURN
20 ON A/4 GOSUB 1000,2000,3000
The number variables can be used directly for easy calculations.