GOTO

From C64-Wiki
Jump to navigationJump to search
BASIC keyword
Keyword: GOTO
Abbreviation: G Shift+O
Type: Command
Token code: 137/$89
Handling routine
in BASIC ROM:
43168–43217
$A8A0–A8D1
List of all BASIC keywords


Remark: This article describes the BASIC command GOTO in BASIC V2 at the Commodore 64.

Typ: Command
General Programming-Syntax: GOTO [<line>]

The BASIC command GOTO makes the BASIC interpreter branch to the indicated line and the execution of the BASIC program is continued at that line. In Commodore BASIC, legal line numbers are from 0 to 63999. Exceeding the upper limit leads to an ?SYNTAX ERROR. If the targeted line number doesn't exist, the BASIC error ?UNDEF'D STATEMENT ERROR occurs. A special case is the usage without a parameter which implicitly refers to line number 0.

The line number is expected to be a numeric constant consisting of digits only (possibly intermixed with spaces) until the first non-digit character. All the following characters until the end of the line are ignored and can be regarded as a comment.

GOTO can be used to construct infinite loops. The code sample 55 GOTO 55 won't terminate, but can be aborted manually by pushing RUN/STOP .

The IF-THEN construction allows the alternative usage of GOTO instead of THEN (see first example, line 31).

See also ON-GOTO construction for multiple GOTO targets selected by a numeric expression.

Technical aspects[edit]

The BASIC command GOTO can be used to start or continue a program at a specified line number like the BASIC command RUN <line>. In contrast to RUN, which clears variables and closes open files before starting the program, GOTO continues keeping the current state. While CONT just continues at the last break-point, GOTO has the advantage of allowing the program to be started at any point, for instance, just beyond code that causes an error.

The performance of the GOTO gets progressively worse as the program size grows. For loop constructs, this problem can be avoided by using FOR-NEXT construct wherever possible. Where this is not possible, understanding the line-lookup process can improve performance.

GOTO has an alternate encoding if written as GO TO. It takes 2 tokens instead of one, which is not recommended because consumes more program space and is (slightly) slower to process. This variation cannot be used in the alternate version of IF <expression> GOTO <line>. In this case an explicit THEN is mandatory leading to IF <expression> THEN GO TO <line> otherwise a ?SYNTAX ERROR is raised.

Examples[edit]

10 C=0: PRINT CHR$(147);: INPUT "Input a number, please "; A
20 PRINT "Input a second number, please "; 
30 INPUT B: IF C=1 AND B=0 GOTO 30
31 IF C=1 AND B<>0 GOTO 74
40 C=1: INPUT "Input an arithmetic operator (+-/*) "; A$
50 IF A$<>"+" OR A$<>"-" OR A$<>"*" OR A$<>"/" GOTO 30
60 IF B=0 AND A$="/" THEN PRINT "Division by 0 is not allowed, correction:";: GO TO 30
70 IF A$="+" THEN C=A+B: B$="ADDITION"
71 IF A$="-" THEN C=A-B: B$="SUBTRACTION"
72 IF A$="*" THEN C=A*B: B$="MULTIPLICATION"
73 IF A$="/" THEN C=A/B: B$="DIVISION"
74 PRINT "Your choice: " B$
75 PRINT "The output is: " C
80 INPUT "Another calculation (Y=YES, End with other chars)"; A$
90 IF A$="Y" THEN GOTO 10

Demonstration of direct GOTO vs. RUN contrast:

10 INPUT A
20 PRINT A

A=7:GOTO20
7

READY.
A=7:RUN20
0

READY.

BASIC V2.0 (second release) Commands

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