Skip to content

Language Reference, Flow Control GOTO Statement

Andreas AFENTAKIS edited this page Oct 12, 2025 · 3 revisions

FBASIC Flow Control: GOTO and Labels

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). 


Purpose

The primary purpose of GOTO is to transfer program control to a different line of execution. It is commonly used for:

  1. Creating Loops: Used with an IF statement to create explicit WHILE or DO...UNTIL loop structures (as seen in the ttt1.bas and Biorhythm.bas examples).

  2. Skipping Code Blocks: Used to bypass sections of code, often within error handling or conditional logic.

  3. Exiting Structures: Used to immediately jump out of a loop or subroutine.

Syntax

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.

Labels

A Label is a marker in the code that serves as the destination for a GOTO or GOSUB statement.

Behavior and Rules

  1. Definition: A Label is defined by a unique name followed immediately by a colon (:).

  2. Placement: Labels must be placed on their own line or immediately before a command.

  3. 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.

  4. Flow: When execution reaches a Label, it simply continues to the next statement unless it arrived via a GOTO or GOSUB.

Example - 1, Usage: Creating a Loop

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

Example - 2, The test unit code**

GoTo abc
assert 0
def:
assert 1
Halt

Clone this wiki locally