-
Notifications
You must be signed in to change notification settings - Fork 0
Language Reference, Flow Control GOTO Statement
The GOTO statement is a fundamental control flow mechanism in FBASIC that allows the program's execution to jump unconditionally to a specific, named point within the code, known as a Label. It is a permanent jump to a new program location. Having the same syntax, the GOSUB statement performs a temporary jump to a separate section of code, from which you will later return (via the RETURN command). Every gosub command must be matched by a corresponding return command (see the Reference Manual for the Syntax).
The primary purpose of GOTO is to transfer program control to a different line of execution. It is commonly used for:
-
Creating Loops: Used with an
IFstatement to create explicitWHILEorDO...UNTILloop structures (as seen in thettt1.basandBiorhythm.basexamples). -
Skipping Code Blocks: Used to bypass sections of code, often within error handling or conditional logic.
-
Exiting Structures: Used to immediately jump out of a loop or subroutine.
The GOTO statement requires a defined Label name as its operand.
REM Define a Label
LabelName:
REM Commands at the Label start here
REM Jump to the Label from anywhere in the program
GOTO LabelName
| Part | Description |
|---|---|
| GOTO | The required statement keyword. |
| LabelName | The required name of the target Label. |
A Label is a marker in the code that serves as the destination for a GOTO or GOSUB statement.
-
Definition: A Label is defined by a unique name followed immediately by a colon (
:). -
Placement: Labels must be placed on their own line or immediately before a command.
-
Naming: Label names must follow the standard naming rules for identifiers (letters, numbers, no spaces) and are generally case-insensitive in execution, although consistent casing is recommended for readability.
-
Flow: When execution reaches a Label, it simply continues to the next statement unless it arrived via a
GOTOorGOSUB.
This example demonstrates how to use GOTO and a Label to create a simple counting loop structure, similar to the pattern found in Biorhythm.bas for iteration.
REM Example of GOTO for loop creation
let count = 1
startLoop:
print "Current count: " + str(count)
let count = count + 1
IF count < 5 THEN
GOTO startLoop
ENDIF
print "Loop finished."
HALT
GoTo abc
assert 0
def:
assert 1
Halt