ZetCode

FreeBasic Goto 关键字

最后修改日期:2025 年 6 月 16 日

FreeBasic 的 Goto 关键字提供了一个无条件跳转到同一过程中标有标签的语句。虽然功能强大,但应谨慎使用以保持代码的可读性。

基本定义

在 FreeBasic 中,Goto 将程序执行转移到同一函数或子程序中的指定标签。目标标签必须在与 Goto 语句相同的范围内定义。

Goto 可以简化某些控制流程,但通常会使代码更难理解。现代编程倾向于在可能的情况下使用结构化控制语句,如循环和条件语句。

简单的 Goto 示例

这个基础示例演示了 Goto 的基本用法。

goto_simple.bas
Print "Before Goto"

Goto skip_section

Print "This won't be printed"

skip_section:
Print "After label"

程序打印“Before Goto”,然后跳转到“skip_section”标签。中间的打印语句被完全跳过。FreeBasic 中的标签以冒号结尾。

Goto 在错误处理中的应用

Goto 在过程中集中进行错误处理时很有用。

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"

此示例使用 Goto 处理无效输入。如果条件失败,执行将跳转到错误消息。`program_end` 标签为该过程提供了一个干净的退出点。

循环中的 Goto

Goto 可用于跳出嵌套循环,这可能比标志变量更清晰。

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

这个嵌套循环打印坐标对,直到两个计数器都达到 3。Goto 提供了同时从两个循环中直接退出的方法,而使用标准的循环控制则需要额外的逻辑。

Goto 用于状态机

Goto 可以通过在不同部分之间跳转来实现简单的状态机。

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"

这个状态机使用 Goto 循环状态直到完成。每个状态在跳转回选择器之前都会更新下一个状态。这种模式对于简单的过程状态机很有用。

带条件逻辑的 Goto

Goto 可以根据条件创建替代的控制流程。

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"

此示例演示了基于条件的程序流程分支。Goto 语句会跳转到偶数或奇数的不同标签。`number_end` 标签提供了一个共同的退出点。

Goto 用于菜单系统

可以使用 Goto 语句实现简单的文本菜单。

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"

这个菜单系统使用 Goto 来实现循环和处理选择。处理完每个选项后,它会返回菜单。这种结构很简单,但选项过多时会难以维护。

Goto 在旧代码转换中的应用

Goto 在转换依赖行号的旧 BASIC 代码时很有帮助。

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"

此示例模仿了使用行号进行流程控制的旧 BASIC 程序。每一“行”都变成一个标签,Goto 语句替换了原始的跳转。这有助于现代化改造遗留代码。

最佳实践

本教程通过实际示例介绍了 FreeBasic 的 Goto 关键字,展示了其在现代编程中的用途和局限性。

作者

我的名字是 Jan Bodnar,我是一名热情的程序员,拥有丰富的编程经验。我自 2007 年以来一直撰写编程文章。至今,我已撰写了 1,400 多篇文章和 8 本电子书。我在教授编程方面拥有十多年的经验。

列出所有 FreeBasic 教程