BASIC Language Reference Manual > Command and Function Reference >

GOTO Statement


Transfers execution of the program to a specific line number or label.


GOTO line number or label

Remarks and Examples

The GOTO statement transfers program execution to the line number or label specified after the GOTO keyword.

For example, GOTO 300 will cause the program execution to "jump" to line 300.

If the label DrawGraphics: is defined in the program, GOTO DrawGraphics causes the program execution to jump to that label and begin executing the statements following the label. (For more information on labels, please see the article Line Numbers and Labels in ReadyBASIC.)

Example using line numbers:

10 PRINT "Hello"
20 GOTO 50
30 PRINT "This text will not be printed"
40 END
50 PRINT "Goodbye"



Example using labels:

    PRINT "Hello"
    GOTO SayGoodbye
    PRINT "This text will not be printed"

    PRINT "Goodbye"



Comparison with GOSUB statement

The GOSUB statement keeps a record of the initiating location, and when a RETURN statement is encountered, the program jumps back to the initiating location and continues program execution with the next statement. No such "return" behavior is possible with the GOTO statement -- it is a one-way jump (formally called a branch in computer science).

"GOTO" and "GO TO"

Some BASIC dialects allow the use of the alternative form "GO TO" instead of "GOTO". ReadyBASIC does not support the "GO TO" form.

Implicit GOTO statements in IF..THEN statements

An IF..THEN statement can take the form IF condition THEN line number or label. For example:

IF NAME$ = "Gerda" THEN 500

If the condition is evaluated as true, execution will be transferred to line number 500. The "GOTO" keyword is implicit.

(Note that the form IF condition THEN GOTO line number or label is not supported, as ReadyBASIC's IF statement does not support statements directly following the THEN statement.)

ReadyBASIC supports IF..THEN..END IF blocks and the ELSE statement, which can eliminate the need for GOTO statements traditionally used handling "else" cases. For example, the following code...

100 IF PASSWORD$ = "Secret" THEN 130
110 PRINT "Incorrect password" : REM This is effectively the "ELSE" case
120 GOTO 140
130 PRINT "Password is correct"
140 REM Program continues here

...could be rewritten as:

100 IF PASSWORD$ = "Secret" THEN
110 PRINT "Password is correct"
120 ELSE
130 PRINT "Incorrect password"
140 END IF
150 REM Program continues here

(The line numbers could also be eliminated.)

Replacements for GOTO and GOSUB

GOTO and GOSUB are old-fashioned control constructs found in traditional line-numbered BASIC programs. GOTO, in particular, is to be avoided, as it breaks the rules of structured programming and can lead to programs that are difficult to understand (see further discussion below).

Instead of GOTO and GOSUB, you may wish to consider the following modern constructs:

Why using GOTO is a bad idea

The simplicity and directness of the GOTO statement means it can be easily explained to beginning programmers, and for that reason it is often used to explain how line numbers work and to illustrate the most basic concepts of loops and decision-branching. It is easy to get started with GOTO statements before moving on to more abstract control constructs.

Unfortuntely, the GOTO statement violates the principles of structured programming. The use of GOTO statement can lead to very confusing "spaghetti code", in which program execution can jump into any point of a program, skipping over the boundaries of any type of program structure. And GOTOs involving line numbers or non-descriptive labels make programs difficult to read -- a procedure call such as CALL DrawWindow can describe and give clues as to what the section of code will do, but GOTO 450 gives few clues as to what will be performed.

The structured program theorem states that, at the lowest level, any program logic can be constructed using combinations of the following control statements: 1. sequences of statements, 2. selection (branching based on a true/false condition), and 3. iteration (loops). At a higher level, procedural programming introduces procedures and functions to divide and structure programs into sections of code that can be repeatedly executed and parameterized.

Modern programming languages provide structured constructs for selection (e.g. IF..THEN), loops (e.g. WHILE), procedures and functions, and exceptions (note that exceptions are not supported by most BASIC dialects, including ReadyBASIC). It has been proven that any logic that uses GOTO statements can also be represented in a properly structured form using these constructs. Although in a few cases, the GOTO variant may be slightly simpler, in the vast majority of cases, the structured representation of an algorithm or program is simpler and more understandable.

Although structured programming was debated in the late 1960s and the 1970s, today the programming community generally accepts that GOTO statements are to be avoided. Practicing programmers consider the use of GOTOs in modern programs to be unprofessional. Computer scientists dislike GOTOs as they make the correctness of programs impossible to prove (structured programs are generally provable, though the time and effort required for a proof is seldom practical).

Edsger Dijkstra's famous 1968 article, Go to statement considered harmful, published in the journal Communications of the Association for Computing Machinery, provides some interesting historical background on the subject.

Copyright 2006-2008, Kevin Matz, All Rights Reserved.