Q(uick)BASIC Statement: GOTO

Quick View

GOTO

A control flow statement that branches unconditionally to the specified line

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • GOTO line
Description/Parameter(s)
line The label or number of the line to execute next.
  • DO...LOOP, SELECT CASE, IF...THEN...ELSE, SUB, and FUNCTION provide better ways to control the flow of your program.
  • GOTO is also used as a keyword in the ON ERROR statement. See Example
Syntax
  • GOTO {linelabel | linenumber}
Description/Parameter(s)

The GOTO statement provides a way to branch unconditionally to another line (linelabel or linenumber). A GOTO statement can branch only to another statement at the same level of a program. You cannot use GOTO to enter or exit a SUB, FUNCTION, or multiline DEF FN function. You can, however, use GOTO to control program flow within any of these program structures.

It is good programming practice to use structured control statements (DO...LOOP, FOR, IF..THEN...ELSE, SELECT CASE) instead of GOTO statements because a program with many GOTO statements is difficult to read and debug.

Example

This example calculates the area of a circle after you supply the radius.

CLS ' Clear screen PRINT "Input 0 to end." Start: INPUT R IF R = 0 THEN END ELSE A = 3.14 * R ^ 2 PRINT "Area ="; A END IF GOTO Start

Sample Output:

Input 0 to end. ? 5 Area = 78.5 ? 0

See also:

Syntax
  • GOTO {linelabel | linenumber}
Description/Parameter(s)

Usage Notes

  • The GOTO statement provides a way to branch unconditionally to another line (linelabel or linenumber). A GOTO statement can branch only to another statement at the same level of a program. You cannot use GOTO to enter or exit a SUB, FUNCTION, or multiline DEF FNfunction. You can, however, use GOTO to control program flow within any of these program structures.
  • It is good programming practice to use structured control statements (DO...LOOP, FOR...NEXT, IF..THEN...ELSE, SELECT CASE) instead of GOTO statements, because a program with many GOTO statements can be difficult to read and debug.
Example

This example calculates the area of a circle after you supply the radius. It uses the GOTO statement to repeat the procedure.

CLS 'Clear screen. PRINT "Input 0 to end." Start: INPUT "Radius"; R IF R = 0 THEN END ELSE A = 3.14 * R ^ 2 PRINT "Area ="; A END IF GOTO Start

Sample Output:

Input 0 to end. Radius? 5 Area = 78.5 Radius? 0