FreeBasic Do Until Keyword
last modified June 16, 2025
The FreeBasic Do Until keyword creates a loop that continues
executing until a specified condition becomes true. It's useful when you
need to repeat code while waiting for a particular state.
Basic Definition
In FreeBasic, Do Until is a loop control structure that checks
the condition after each iteration. The loop runs at least once before
evaluating the exit condition.
The syntax consists of Do Until condition to start the loop and
Loop to mark its end. Code between these statements executes
repeatedly until the condition evaluates to true.
Simple Do Until Loop
This basic example demonstrates the fundamental structure of a Do Until loop.
Dim counter As Integer = 1
Do Until counter > 5
Print "Iteration: "; counter
counter += 1
Loop
Print "Loop finished"
The loop continues until counter exceeds 5. Each iteration prints the current counter value and increments it. Notice the condition is checked after each complete iteration of the loop body.
User Input Validation
Do Until is excellent for validating user input until acceptable data is entered.
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
This loop repeatedly prompts for age until a value between 18 and 120 is provided. It demonstrates how Do Until can combine with conditional logic for robust input validation.
File Reading
Do Until loops work well for reading files until the end is reached.
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
This example reads a file line by line until EOF (End Of File) is true. The Do Until structure ensures we process all file content while handling the termination condition automatically.
Random Number Generation
Do Until can generate values until a specific random number appears.
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"
The loop simulates dice rolls until the target number appears. This shows how Do Until handles unpredictable termination conditions in simulations or games.
Password Verification
Do Until works well for implementing limited password attempts.
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
This security example combines Do Until with multiple conditions. The loop continues until either authentication succeeds or attempts are exhausted. Complex conditions are common in real-world Do Until scenarios.
Game Loop
Do Until serves as an excellent game loop controller.
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
This combat simulation uses Do Until to control the game flow. The loop continues until either character's health drops to zero. The example shows how Do Until can manage complex game state transitions.
Nested Do Until Loops
Do Until loops can be nested to handle multi-dimensional problems.
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
This nested loop example creates a multiplication table. The outer loop controls rows while the inner loop handles columns. Nested Do Until loops are powerful for grid-based operations and multi-pass algorithms.
Best Practices
- Clear Conditions: Make exit conditions obvious and well-documented.
- Avoid Infinite Loops: Ensure the condition can become true.
- Initialization: Initialize all variables used in the condition.
- Modification: Update condition variables within the loop body.
- Readability: Use meaningful variable names in conditions.
- Complex Conditions: Break complex conditions into variables.
- Error Handling: Include safeguards against unexpected states.
This tutorial covered the FreeBasic Do Until keyword with practical
examples showing its usage in different programming scenarios.
Author
List all FreeBasic Tutorials.