in BASIC ROM:
|List of all BASIC keywords|
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 .
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.
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: