FreeBasic Loop Until Keyword
last modified June 21, 2025
The FreeBasic Loop Until statement creates a loop that continues
executing until a specified condition becomes true. Unlike While
loops, it always executes at least once before checking the condition.
Basic Definition
In FreeBasic, Loop Until is a post-test loop structure. The loop
body executes first, then the condition is evaluated. If false, the loop
repeats. The syntax is simple: Do ... Loop Until condition.
This loop is ideal when you need to execute code at least once before checking a termination condition. It's commonly used for input validation and menu systems where initial execution is required.
Simple Loop Until Example
This basic example demonstrates the fundamental structure of a
Loop Until.
Dim counter As Integer = 1
Do
Print "Iteration: "; counter
counter += 1
Loop Until counter > 5
The loop prints numbers from 1 to 5. It increments the counter each iteration and checks if it exceeds 5. Note that the condition is evaluated after the loop body executes, ensuring at least one iteration.
Input Validation with Loop Until
Loop Until can be used for validating user input, as it ensures at
least one prompt is displayed.
Dim age As Integer
Do
Input "Enter your age (18+): ", age
Loop Until age >= 18
Print "Valid age entered: "; age
This code repeatedly asks for age until a value of 18 or more is entered. The loop guarantees the prompt appears at least once, making it ideal for user input scenarios where initial interaction is required.
Loop Until with Boolean Flag
Using a Boolean flag with Loop Until provides clear loop control.
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
Here we use a Boolean flag to control the loop. The loop continues
until the random value equals 22. The flag isDone is set to
when the condition is met, allowing for clear and explicit loop
termination.
Nested Loop Until
Loop Until statements can be nested to create complex control
structures.
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
This example shows nested Loop Until structures. The inner loop
completes all its iterations for each outer loop iteration. Nested loops are
useful for working with multi-dimensional data or complex algorithms.
Loop Until with Array Processing
Loop Until can process arrays until a condition is met.
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
This code sums array elements until either all elements are processed or the sum
exceeds 75. The compound condition demonstrates how Loop Until
can handle multiple exit criteria. The loop stops when either condition
becomes true.
Loop Until with Random Numbers
Loop Until works well with random number generation scenarios.
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"
This example generates random numbers until the target value (50) is found. The loop continues until the random number matches the target, counting each attempt. This demonstrates Loop Until's usefulness in probabilistic scenarios.
Loop Until with Early Exit
The Exit Do statement can provide early termination within a
Loop Until.
#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
This password checker uses Loop Until with an early exit condition.
If three incorrect attempts occur, the loop exits immediately using Exit
Do. Otherwise, it continues until the correct password is entered.
Best Practices
- Readability: Keep loop conditions simple and clear.
- Initialization: Initialize variables before the loop.
- Termination: Ensure the loop can eventually terminate.
- Complexity: Avoid deeply nested Loop Until structures.
- Comments: Document non-obvious loop conditions.
This tutorial covered the FreeBasic Loop Until keyword with
practical examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.