BASIC Programming/Beginning BASIC/Control Structures/GOTO

From Wikibooks, open books for an open world
Jump to navigation Jump to search

The GOTO statement in BASIC is used to jump to a specific location or label within the source code; it may take either a line number (which appears at the beginning of a line) or a label (which is a word that ends in a colon).

This statement was originally essential for programming in Basic; the older interpreters only allowed IF statements to run on one line and did not have more advanced handling of FOR or WHILE loops. Under modern versions of Basic, you will generally see GOTO used only to return to the top of a main loop. In all other cases, usage of GOTO has been deprecated in favor of other statements.

GOSUB[edit | edit source]

The Gosub statement is a variation of the Goto statement. When used, it jumps to a specific location within the program, and allows the next RETURN statement to head back to the point just after the GOSUB call.

This statement must be paired with a matching return statement. Under modern versions of Basic, GOSUB uses a stack of locations that may be filled up, resulting in a stack overflow.