FreeBasic Do Loop Keywords
last modified June 16, 2025
The FreeBasic Do
and Loop
keywords create loops
that repeat a block of code. They offer flexible control structures with
different condition checking positions.
Basic Definition
In FreeBasic, Do
and Loop
work together to create
looping structures. The loop continues until a specified condition is met.
There are four main variations: Do While
, Do Until
,
Loop While
, and Loop Until
. Each checks the
condition at different points in the loop execution.
Basic Do Loop
This example shows the simplest infinite loop structure using Do/Loop.
Dim counter As Integer = 0 Do counter += 1 Print "Counter: "; counter If counter >= 5 Then Exit Do End If Loop Print "Loop ended"
This creates an infinite loop that increments a counter. The Exit Do
statement breaks the loop when counter reaches 5. Without Exit Do, this loop
would run indefinitely.
Do While Loop
The Do While
loop checks the condition before each iteration.
Dim number As Integer = 1 Do While number <= 10 Print "Number: "; number number += 2 Loop Print "Loop completed"
This loop prints odd numbers from 1 to 10. The condition is checked before each iteration. If false initially, the loop body never executes. The variable is modified inside the loop to eventually make the condition false.
Do Until Loop
The Do Until
loop runs until the condition becomes true.
Dim password As String Dim attempts As Integer = 0 Do Until password = "secret" Or attempts >= 3 Input "Enter password: ", password attempts += 1 Loop If password = "secret" Then Print "Access granted" Else Print "Too many attempts" End If
This loop prompts for a password until either the correct password is entered or 3 attempts are made. Unlike While, Until continues until the condition becomes true rather than while it remains true.
Loop While
The Loop While
checks the condition after each iteration.
Dim response As String Do Input "Do you want to continue (yes/no)? ", response response = LCase(response) Loop While response = "yes" Print "Goodbye"
This loop always executes at least once before checking the condition. It continues while the user enters "yes". The condition is evaluated after each complete iteration of the loop body.
Loop Until
The Loop Until
checks its condition after each iteration.
Dim randomNum As Integer Dim guess As Integer Dim tries As Integer = 0 Randomize Timer randomNum = Int(Rnd * 10) + 1 Do Input "Guess the number (1-10): ", guess tries += 1 If guess < randomNum Then Print "Too low" ElseIf guess > randomNum Then Print "Too high" End If Loop Until guess = randomNum Print "Correct! You guessed it in "; tries; " tries"
This number guessing game loop runs until the correct number is guessed. The condition is checked after each attempt. The loop always runs at least once before checking the exit condition.
Nested Do Loops
Do loops can be nested inside other Do loops for complex logic.
Dim i As Integer = 1 Do While i <= 3 Dim j As Integer = 1 Do While j <= 3 Print "i:"; i; " j:"; j j += 1 Loop i += 1 Loop Print "Nested loops completed"
This example shows two nested Do While loops. The outer loop runs 3 times, and for each outer iteration, the inner loop runs 3 times. This creates a total of 9 iterations (3×3) of the inner loop's body.
Do Loop With Continue
The Continue Do
statement skips the rest of the current iteration.
Dim n As Integer = 0 Do While n < 10 n += 1 If n Mod 2 = 0 Then Continue Do End If Print "Odd number: "; n Loop Print "Finished"
This loop prints only odd numbers between 1 and 10. When an even number
is encountered, Continue Do
skips the print statement and
jumps to the next iteration. The loop condition is still checked normally.
Best Practices
- Initialization: Always initialize loop control variables before the loop.
- Termination: Ensure the loop has a clear exit condition.
- Readability: Prefer While/Until conditions over Exit Do when possible.
- Performance: Move invariant calculations outside loops.
- Indentation: Consistently indent loop bodies for clarity.
This tutorial covered FreeBasic's Do
and Loop
keywords with practical examples showing different looping structures.
Author
List all FreeBasic Tutorials.