FreeBasic Do Until 关键字
最后修改日期:2025 年 6 月 16 日
FreeBasic 的 Do Until 关键字创建一个循环,该循环将持续执行,直到指定的条件变为真。当您需要重复代码以等待特定状态时,它很有用。
基本定义
在 FreeBasic 中,Do Until 是一种循环控制结构,它在每次迭代后检查条件。循环至少执行一次,然后评估退出条件。
语法包括用于启动循环的 Do Until condition 和用于标记其结束的 Loop。这些语句之间的代码会重复执行,直到条件计算为真。
简单的 Do Until 循环
这个基本示例演示了 Do Until 循环的基本结构。
Dim counter As Integer = 1
Do Until counter > 5
Print "Iteration: "; counter
counter += 1
Loop
Print "Loop finished"
循环将继续,直到 counter 大于 5。每次迭代都会打印当前的 counter 值并递增。请注意,条件是在循环体每次完整迭代后检查的。
用户输入验证
Do Until 非常适合验证用户输入,直到输入可接受的数据为止。
Dim age As Integer
Do Until age >= 18 And age <= 120
Input "Enter your age (18-120): ", age
If age < 18 Then
Print "You're too young!"
ElseIf age > 120 Then
Print "Please enter a realistic age!"
End If
Loop
Print "Valid age entered: "; age
此循环会反复提示输入年龄,直到输入的值在 18 到 120 之间。它展示了 Do Until 如何与条件逻辑结合使用以实现健壮的输入验证。
文件读取
Do Until 循环非常适合读取文件,直到到达文件末尾。
Dim fileNum As Integer = FreeFile()
Open "data.txt" For Input As #fileNum
Dim line As String
Dim lineCount As Integer = 0
Do Until EOF(fileNum)
Line Input #fileNum, line
lineCount += 1
Print "Line "; lineCount; ": "; line
Loop
Close #fileNum
Print "Total lines read: "; lineCount
此示例逐行读取文件,直到 EOF(文件结束)为真。Do Until 结构确保我们处理所有文件内容,同时自动处理终止条件。
随机数生成
Do Until 可以生成值,直到出现特定的随机数。
Randomize Timer
Dim target As Integer = 7
Dim attempts As Integer = 0
Dim roll As Integer
Print "Rolling dice until we get "; target
Do Until roll = target
roll = Int(Rnd() * 6) + 1
attempts += 1
Print "Attempt "; attempts; ": "; roll
Loop
Print "Got "; target; " after "; attempts; " attempts"
该循环模拟掷骰子,直到出现目标数字。这表明 Do Until 如何在模拟或游戏中处理不可预测的终止条件。
密码验证
Do Until 非常适合实现有限次数的密码尝试。
Dim correctPassword As String = "secret123"
Dim userPassword As String
Dim attempts As Integer = 0
Dim maxAttempts As Integer = 3
Dim authenticated As Boolean = False
Do Until authenticated Or attempts >= maxAttempts
Input "Enter password: ", userPassword
attempts += 1
If userPassword = correctPassword Then
authenticated = True
Print "Access granted!"
ElseIf attempts < maxAttempts Then
Print "Incorrect. Attempts left: "; maxAttempts - attempts
End If
Loop
If Not authenticated Then
Print "Maximum attempts reached. Access denied."
End If
此安全示例将 Do Until 与多个条件结合使用。循环将继续,直到身份验证成功或尝试次数耗尽。在实际的 Do Until 场景中,复杂的条件很常见。
游戏循环
Do Until 是一个出色的游戏循环控制器。
Dim playerHealth As Integer = 100
Dim enemyHealth As Integer = 75
Dim gameOver As Boolean = False
Print "Combat begins!"
Do Until gameOver
' Player attack
Dim playerDamage As Integer = Int(Rnd() * 20) + 5
enemyHealth -= playerDamage
Print "You hit for "; playerDamage; " damage"
' Enemy attack if still alive
If enemyHealth > 0 Then
Dim enemyDamage As Integer = Int(Rnd() * 15) + 3
playerHealth -= enemyDamage
Print "Enemy hits for "; enemyDamage; " damage"
End If
' Check win/lose conditions
If playerHealth <= 0 Then
gameOver = True
Print "You have been defeated!"
ElseIf enemyHealth <= 0 Then
gameOver = True
Print "Enemy defeated! You win!"
End If
Print "Player: "; playerHealth; " HP | Enemy: "; _
IIf(enemyHealth > 0, enemyHealth, 0); " HP"
Print "--------------------------------"
Sleep 1500
Loop
此战斗模拟使用 Do Until 来控制游戏流程。循环将继续,直到任一角色的生命值降至零。该示例显示了 Do Until 如何管理复杂的游戏状态转换。
嵌套的 Do Until 循环
Do Until 循环可以嵌套以处理多维问题。
Dim rows As Integer = 3
Dim cols As Integer = 4
Dim row As Integer = 1
Dim col As Integer
Do Until row > rows
col = 1
Print "Row "; row; ": ";
Do Until col > cols
Print row * col; " ";
col += 1
Loop
Print
row += 1
Loop
此嵌套循环示例创建一个乘法表。外层循环控制行,内层循环控制列。嵌套的 Do Until 循环对于基于网格的操作和多遍算法非常强大。
最佳实践
- 清晰的条件:使退出条件清晰明了并妥善记录。
- 避免无限循环:确保条件最终可以变为真。
- 初始化:初始化条件中使用的所有变量。
- 修改:在循环体内更新条件变量。
- 可读性:在条件中使用有意义的变量名。
- 复杂条件:将复杂条件分解为变量。
- 错误处理:包含针对意外状态的保护措施。
本教程介绍了 FreeBasic 的 Do Until 关键字,并通过实际示例展示了其在不同编程场景中的用法。