ZetCode

FreeBasic Loop Until 关键字

最后修改于 2025 年 6 月 21 日

FreeBasic 的 Loop Until 语句创建一个循环,该循环会一直执行,直到指定的条件变为真。与 While 循环不同,它在检查条件之前至少会执行一次。

基本定义

在 FreeBasic 中,Loop Until 是一种后测试循环结构。循环体首先执行,然后评估条件。如果条件为假,则循环重复。语法很简单:Do ... Loop Until condition

当您需要在检查终止条件之前至少执行一次代码时,此循环非常理想。它通常用于输入验证和菜单系统,因为这些系统需要初始执行。

简单的 Loop Until 示例

这个基本示例演示了 Loop Until 的基本结构。

loop_until_basic.bas
Dim counter As Integer = 1

Do
    Print "Iteration: "; counter
    counter += 1
Loop Until counter > 5

该循环打印从 1 到 5 的数字。它在每次迭代中递增计数器,并检查它是否超过 5。请注意,条件是在循环体执行后评估的,确保至少执行一次迭代。

使用 Loop Until 进行输入验证

Loop Until 可用于验证用户输入,因为它确保至少显示一次提示。

loop_until_input.bas
Dim age As Integer

Do
    Input "Enter your age (18+): ", age
Loop Until age >= 18

Print "Valid age entered: "; age

此代码会反复要求输入年龄,直到输入的值为 18 岁或以上。该循环保证提示至少出现一次,使其成为需要初始交互的用户输入场景的理想选择。

带布尔标志的 Loop Until

使用 Boolean 标志与 Loop Until 结合可以提供清晰的循环控制。

loop_until_flag.bas
Randomize Timer  ' Seed the random number generator

Dim isDone As Boolean = False
Dim randomValue As Integer = 0

Do
    randomValue = Int(Rnd * 30) + 1
    Print "Random value: "; randomValue
    
    If randomValue = 22 Then
        isDone = True
    End If
Loop Until isDone

这里我们使用一个 Boolean 标志来控制循环。循环将一直持续到随机值等于 22。当满足条件时,标志 isDone 被设置为 ,从而实现清晰明确的循环终止。

嵌套的 Loop Until

Loop Until 语句可以嵌套以创建复杂的控制结构。

loop_until_nested.bas
Dim outer As Integer = 1
Dim inner As Integer

Do
    inner = 1
    Print "Outer loop: "; outer
    
    Do
        Print "  Inner loop: "; inner
        inner += 1
    Loop Until inner > 3
    
    outer += 1
Loop Until outer > 4

此示例显示了嵌套的 Loop Until 结构。对于每一次外部循环迭代,内部循环都会完成其所有迭代。嵌套循环对于处理多维数据或复杂算法非常有用。

带数组处理的 Loop Until

Loop Until 可以处理数组,直到满足某个条件为止。

loop_until_array.bas
Dim numbers(1 To 5) As Integer = {10, 20, 30, 40, 50}
Dim index As Integer = 1
Dim sum As Integer = 0

Do
    sum += numbers(index)
    index += 1
Loop Until index > 5 Or sum > 75

Print "Sum: "; sum
Print "Processed elements: "; index - 1

此代码将数组元素相加,直到所有元素都被处理或总和超过 75。复合条件演示了 Loop Until 如何处理多个退出标准。当任一条件变为真时,循环停止。

带随机数的 Loop Until

Loop Until 在随机数生成场景中效果很好。

loop_until_random.bas
Randomize Timer
Dim target As Integer = 50
Dim attempts As Integer = 0
Dim guess As Integer

Do
    guess = Int(Rnd * 100) + 1
    attempts += 1
    Print "Attempt "; attempts; ": "; guess
Loop Until guess = target

Print "Found "; target; " in "; attempts; " attempts"

此示例生成随机数,直到找到目标值(50)。循环将一直持续到随机数匹配目标为止,并计算每次尝试。这展示了 Loop Until 在概率场景中的实用性。

带提前退出的 Loop Until

Exit Do 语句可以在 Loop Until 中提供提前终止。

loop_until_exit.bas
#include "vbcompat.bi"

Dim today As String = Format(Now, "dddd")
Dim guess As String
Dim attempts As Integer = 0
Dim correct As Boolean = False

Do
    Input "Guess today's date: ", guess

    If guess = today Then
        correct = True
        Exit Do
    End If

    attempts += 1
    If attempts >= 3 Then
        Exit Do
    End If
Loop

If correct Then
    Print "Correct! Today is "; today
Else
    Print "Out of attempts! The correct date was "; today
End If

此密码检查器使用带提前退出条件的 Loop Until。如果发生三次错误尝试,循环将使用 Exit Do 立即退出。否则,它将一直持续到输入正确的密码为止。

最佳实践

本教程通过实用示例介绍了 FreeBasic 的 Loop Until 关键字,展示了其在不同场景下的用法。

作者

我叫 Jan Bodnar,是一名充满热情的程序员,拥有丰富的编程经验。我自 2007 年以来一直在撰写编程文章。到目前为止,我已撰写了 1,400 多篇文章和 8 本电子书。我在编程教学方面拥有十多年的经验。

列出所有 FreeBasic 教程