ON

From C64-Wiki

Revision as of 21:06, 3 January 2010 by Jodigi (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
BASIC keyword
Keyword:ON
Abbreviation:
Type:Command
Token code:145/$91
Handling routine
in BASIC ROM:
43339–43370
$A94B–A96A
List of all BASIC keywords


Remark: This article describes the BASIC-keyword ON in BASIC V2 at the Commodore 64.

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.

[edit] Examples

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.

BASIC V2.0-Commands of Commodore 64

ABS | AND | ASC | ATN | CHR$ | CLOSE | CLR | CMD | CONT | COS | DATA | DEF | DIM | END | EXP | FN | FOR | FRE | GET | GET# | GOSUB | GOTO | IF | INPUT | INPUT# | INT | LEFT$ | LEN | LET | LIST | LOAD | LOG | MID$ | NEW | NEXT | NOT | ON | OPEN | OR | PEEK | POKE | POS | PRINT | PRINT# | READ | REM | RESTORE | RETURN | RIGHT$ | RND | RUN | SAVE | SGN | SIN | SPC( | SQR | STATUS/ST | STEP | STOP | STR$ | SYS | TAB( | TAN | THEN | TIME/TI | TIME$/TI$ | TO | USR | VAL | VERIFY | WAIT

 
Personal tools
Help and Feedback
In other languages