FreeBasic Loop While Keyword
last modified June 16, 2025
The FreeBasic Loop While
construct creates a loop that executes
a block of code repeatedly while a condition remains true. It checks the
condition after each iteration, ensuring the loop runs at least once.
Basic Definition
In FreeBasic, Loop While
is a post-test loop structure. The
loop body executes first, then the condition is evaluated. If true, the
loop repeats.
This differs from While Wend
which checks the condition before
execution. Loop While
is useful when you need to guarantee at
least one iteration occurs.
Simple Loop While Example
This basic example demonstrates the fundamental structure of a Loop While.
Dim counter As Integer = 1 Do Print "Iteration: "; counter counter += 1 Loop While counter <= 5
The loop prints numbers from 1 to 5. The condition checks after each iteration. Even if counter started at 6, the loop would run once before checking the condition.
User Input Validation
Loop While is ideal for input validation where you need at least one prompt.
Dim age As Integer Do Input "Enter your age (1-120): ", age Loop While age < 1 Or age > 120 Print "Valid age entered: "; age
This code repeatedly prompts for age until a valid value is entered. The loop always runs at least once, ensuring the user sees the prompt before any condition check.
Password Verification
Loop While works well for password verification scenarios.
Dim password As String Dim attempts As Integer = 0 Const CORRECT_PASSWORD = "secret123" Do Input "Enter password: ", password attempts += 1 Loop While password <> CORRECT_PASSWORD And attempts < 3 If password = CORRECT_PASSWORD Then Print "Access granted!" Else Print "Too many attempts!" End If
This example limits password attempts to three. The loop continues until either the correct password is entered or attempts are exhausted. The condition combines two checks with a logical AND.
Menu System
Loop While can control a simple text-based menu system.
Dim choice As String Do Print "1. Play game" Print "2. View scores" Print "3. Exit" Input "Enter choice: ", choice Select Case choice Case "1" Print "Starting game..." Case "2" Print "Displaying scores..." Case "3" Print "Goodbye!" Case Else Print "Invalid choice" End Select Loop While choice <> "3"
The menu displays until the user selects option 3 to exit. The loop structure ensures the menu always shows at least once. The Select Case handles different menu options.
Random Number Generation
Loop While can generate values until a condition is met.
Randomize Timer Dim number As Integer Do number = Int(Rnd * 100) + 1 Print "Generated: "; number Loop While number < 90 Print "Found number >= 90: "; number
This generates random numbers between 1-100 until one >= 90 is found. The loop runs at least once, and the condition is checked after each random number generation.
File Reading
Loop While can process file contents until the end is reached.
Dim fileNum As Integer = FreeFile() Dim line As String Open "data.txt" For Input As #fileNum Do Line Input #fileNum, line Print "Line: "; line Loop While Not EOF(fileNum) Close #fileNum
This reads a file line by line until EOF (End Of File) is reached. The loop structure ensures each line is processed, with the condition checked after each read operation.
Nested Loop While
Loop While statements can be nested to handle complex scenarios.
Dim outer As Integer = 1 Dim inner As Integer Do inner = 1 Print "Outer loop: "; outer Do Print " Inner loop: "; inner inner += 1 Loop While inner <= 3 outer += 1 Loop While outer <= 2
This demonstrates nested Loop While structures. The outer loop runs twice, and for each iteration, the inner loop runs three times. Indentation helps visualize the nesting level.
Best Practices
- Initialization: Ensure variables are initialized before the loop.
- Termination: Guarantee the condition will eventually become false.
- Readability: Use clear conditions and proper indentation.
- Comments: Add comments for complex loop conditions.
- Performance: Avoid expensive operations in the condition check.
This tutorial covered the FreeBasic Loop While
keyword with
practical examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.