ZetCode

FreeBasic Goto Keyword

last modified June 16, 2025

The FreeBasic Goto keyword provides an unconditional jump to a labeled statement in the same procedure. While powerful, it should be used judiciously to maintain code readability.

Basic Definition

In FreeBasic, Goto transfers program execution to a specified label within the same function or subroutine. The target label must be defined in the same scope as the Goto statement.

Goto can simplify certain control flows but often makes code harder to understand. Modern programming prefers structured control statements like loops and conditionals when possible.

Simple Goto Example

This basic example demonstrates the fundamental usage of Goto.

goto_simple.bas
Print "Before Goto"

Goto skip_section

Print "This won't be printed"

skip_section:
Print "After label"

The program prints "Before Goto" then jumps to the label "skip_section". The middle print statement is skipped entirely. Labels in FreeBasic end with a colon.

Goto in Error Handling

Goto can be useful for centralized error handling in a procedure.

goto_error.bas
Dim value As Integer

Input "Enter a positive number: ", value

If value <= 0 Then
    Goto invalid_input
End If

Print "You entered: "; value
Goto program_end

invalid_input:
Print "Error: Input must be positive"

program_end:
Print "Program complete"

This example uses Goto to handle invalid input. If the condition fails, execution jumps to the error message. The program_end label provides a clean exit point for the procedure.

Goto in a Loop

Goto can be used to break out of nested loops, which can be clearer than flag variables.

goto_loop.bas
Dim i As Integer, j As Integer

For i = 1 To 5
    For j = 1 To 5
        Print i; ","; j
        If i = 3 And j = 3 Then
            Goto loop_exit
        End If
    Next j
Next i

loop_exit:
Print "Exited loops at i="; i; " j="; j

This nested loop prints coordinate pairs until both counters reach 3. The Goto provides a direct exit from both loops simultaneously, which would require additional logic with standard loop controls.

Goto for State Machines

Goto can implement simple state machines by jumping between sections.

goto_state.bas
Dim state As Integer = 1

state_start:
Select Case state
    Case 1:
        Print "State 1: Initializing"
        state = 2
        Goto state_start
    Case 2:
        Print "State 2: Processing"
        state = 3
        Goto state_start
    Case 3:
        Print "State 3: Finalizing"
        Goto state_end
End Select

state_end:
Print "State machine complete"

This state machine uses Goto to loop through states until completion. Each state updates the next state before jumping back to the selector. This pattern can be useful for simple procedural state machines.

Goto with Conditional Logic

Goto can create alternative control flows based on conditions.

goto_conditional.bas
Dim number As Integer = 7

If number Mod 2 = 0 Then
    Goto even_number
Else
    Goto odd_number
End If

even_number:
Print number; " is even"
Goto number_end

odd_number:
Print number; " is odd"

number_end:
Print "Number check complete"

This example demonstrates branching program flow based on a condition. The Goto statements jump to different labels for even or odd numbers. The number_end label provides a common exit point.

Goto for Menu Systems

Simple text menus can be implemented using Goto statements.

goto_menu.bas
Dim choice As String

menu_start:
Print "1. Option One"
Print "2. Option Two"
Print "3. Exit"
Input "Choose: ", choice

Select Case choice
    Case "1":
        Goto option_one
    Case "2":
        Goto option_two
    Case "3":
        Goto menu_exit
    Case Else:
        Goto menu_start
End Select

option_one:
Print "You chose option one"
Goto menu_start

option_two:
Print "You chose option two"
Goto menu_start

menu_exit:
Print "Goodbye"

This menu system uses Goto to implement a loop and handle selections. After processing each option, it returns to the menu. The structure is simple but becomes hard to maintain with many options.

Goto in Legacy Code Conversion

Goto can help when converting older BASIC code that relies on line numbers.

goto_legacy.bas
' Simulating old-style BASIC with line numbers
Goto line_20

line_10:
Print "This is line 10"
Goto line_30

line_20:
Print "This is line 20"
Goto line_10

line_30:
Print "This is line 30"

This example mimics old BASIC programs that used line numbers for flow control. Each "line" becomes a label, and Goto statements replace the original jumps. This helps when modernizing legacy code.

Best Practices

This tutorial covered the FreeBasic Goto keyword with practical examples showing both its uses and limitations in modern programming.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.