FreeBasic Do While Keyword
last modified June 16, 2025
The FreeBasic Do While
keyword creates a loop that executes
a block of code repeatedly while a condition remains true. It checks the
condition before each iteration.
Basic Definition
In FreeBasic, Do While
is a loop construct that continues
executing as long as its condition evaluates to true. The condition is
checked at the start of each iteration.
The syntax consists of Do While
followed by a condition,
the loop body, and Loop
to mark the end. It's useful when
you need to repeat code an unknown number of times.
Simple Counter Loop
This example demonstrates a basic counter using Do While.
Dim counter As Integer = 1 Do While counter <= 5 Print "Iteration: "; counter counter += 1 Loop Print "Loop finished"
The loop runs while counter is less than or equal to 5. Each iteration prints the current value and increments counter. When counter reaches 6, the condition becomes false and the loop exits.
User Input Validation
Do While is excellent for validating user input until it meets criteria.
Dim age As Integer Do While age < 1 Or age > 120 Input "Enter your age (1-120): ", age Loop Print "Valid age entered: "; age
This loop continues prompting for age until a value between 1 and 120 is entered. The condition checks for invalid values, keeping the loop active until valid input is received.
Reading Until Sentinel Value
Do While can process input until a specific sentinel value appears.
Dim number As Integer Dim sum As Integer = 0 Print "Enter numbers (0 to stop):" Do While True Input "", number If number = 0 Then Exit Do sum += number Loop Print "Sum of numbers: "; sum
This example uses an infinite loop with Exit Do
to break out
when zero is entered. It accumulates numbers until the sentinel value (0)
terminates input.
File Reading Loop
Do While is commonly used to read 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 While Not EOF(fileNum) Line Input #fileNum, line lineCount += 1 Print "Line "; lineCount; ": "; line Loop Close #fileNum Print "Total lines read: "; lineCount
The loop continues while EOF (End Of File) is false. Each iteration reads one line and increments the counter. This pattern is standard for processing text files line by line.
Menu System
Do While can power interactive menu systems that run until quit.
Dim choice As String Do While choice <> "4" Print "1. Option One" Print "2. Option Two" Print "3. Option Three" Print "4. Exit" Input "Select: ", choice Select Case choice Case "1": Print "Option One selected" Case "2": Print "Option Two selected" Case "3": Print "Option Three selected" Case "4": Print "Exiting..." Case Else: Print "Invalid choice" End Select Loop
This menu system displays options until the user selects 4 to exit. The Do While condition checks for the exit command, making the menu loop intuitive and easy to maintain.
Game Loop
Do While is perfect for game loops that run while the game is active.
Dim gameOver As Boolean = False Dim score As Integer = 0 Dim lives As Integer = 3 Do While Not gameOver ' Game logic would go here score += 10 Print "Score: "; score; " Lives: "; lives If score >= 100 Then gameOver = True Print "You won!" ElseIf Rnd() < 0.1 Then lives -= 1 If lives <= 0 Then gameOver = True Print "Game Over!" End If End If Loop
This simplified game loop runs while gameOver is false. It updates score and checks win/lose conditions. The Boolean flag controls the loop's execution based on game state.
Nested Do While Loops
Do While loops can be nested to handle complex repetitive tasks.
Dim outer As Integer = 1 Do While outer <= 3 Dim inner As Integer = 1 Print "Outer loop: "; outer Do While inner <= outer Print " Inner loop: "; inner inner += 1 Loop outer += 1 Loop
This example shows nested Do While loops. The outer loop runs 3 times, while the inner loop's iterations increase with each outer loop pass. Nested loops are useful for multi-dimensional processing.
Best Practices
- Initialization: Initialize loop variables before the Do While.
- Termination: Ensure the condition will eventually become false.
- Readability: Use meaningful variable names in conditions.
- Complex Conditions: Break complex conditions into variables.
- Exit Do: Use sparingly, only for clear exceptional cases.
This tutorial covered the FreeBasic Do While
keyword with practical
examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.