FreeBasic While/Wend Keywords
last modified June 16, 2025
The FreeBasic While
and Wend
keywords create a loop
that executes as long as a condition remains true. This is a fundamental
control structure for repetitive tasks in programming.
Basic Definition
In FreeBasic, While
starts a loop that continues executing the
code block between While
and Wend
while the condition
is true. The condition is checked before each iteration.
The Wend
keyword marks the end of the loop block. It's important
to ensure the loop condition eventually becomes false to prevent infinite loops.
Simple While Loop
This example demonstrates a basic While loop that counts from 1 to 5.
Dim counter As Integer = 1 While counter <= 5 Print "Count: "; counter counter += 1 Wend Print "Loop finished"
The loop starts with counter at 1. Each iteration prints the current value and increments counter. When counter reaches 6, the condition becomes false and the loop exits. The message after Wend confirms loop completion.
While Loop with User Input
This example uses While to repeatedly prompt for input until valid.
Dim age As Integer = 0 While age <= 0 Input "Enter your age (positive number): ", age Wend Print "Thank you! Your age is: "; age
The loop continues until the user enters a positive number. The condition checks if age is less than or equal to zero. Invalid inputs keep the loop running, while valid input exits it.
Nested While Loops
While loops can be nested to handle more complex repetition patterns.
Dim i As Integer = 1 Dim j As Integer While i <= 3 j = 1 While j <= 3 Print "i:"; i; " j:"; j j += 1 Wend i += 1 Wend
This creates a 3x3 grid of coordinates. The outer loop runs 3 times, and for each outer iteration, the inner loop runs 3 times. Each Wend matches its corresponding While, maintaining proper nesting structure.
While Loop with Early Exit
The Exit While
statement allows premature loop termination.
Dim num As Integer = 1 Dim sum As Integer = 0 While True sum += num num += 1 If sum > 20 Then Exit While End If Print "Current sum:"; sum Wend Print "Final sum:"; sum
This loop would run forever due to the True condition, but the Exit While statement stops it when sum exceeds 20. This pattern is useful when the exit condition is complex or occurs mid-loop.
While Loop with Boolean Flag
A Boolean variable can control loop execution for more readable code.
Dim keepRunning As Boolean = True Dim attempts As Integer = 0 While keepRunning attempts += 1 Print "Attempt:"; attempts If attempts >= 5 Then keepRunning = False End If Wend Print "Finished after"; attempts; "attempts"
The Boolean flag keepRunning
controls the loop. After 5 attempts,
we set it to False to exit. This approach makes the loop's purpose clearer
than embedding the condition directly in the While statement.
While Loop with Array Processing
While loops are effective for processing arrays when the size is unknown.
Dim numbers(10) As Integer Dim i As Integer = 0 ' Fill array with random numbers While i < 10 numbers(i) = Int(Rnd * 100) + 1 i += 1 Wend i = 0 Dim total As Integer = 0 ' Calculate sum While i < 10 total += numbers(i) i += 1 Wend Print "Array sum:"; total
The first While loop fills an array with random numbers. The second While loop calculates their sum. Both loops use the same counter pattern but perform different operations on the array elements.
While Loop with String Processing
This example processes a string character by character using a While loop.
Dim text As String = "FreeBasic" Dim index As Integer = 1 Dim uppercaseCount As Integer = 0 While index <= Len(text) Dim c As String = Mid(text, index, 1) If c >= "A" And c <= "Z" Then uppercaseCount += 1 End If index += 1 Wend Print "Uppercase letters in '"; text; "':"; uppercaseCount
The loop examines each character in the string, counting uppercase letters.
The condition uses Len(text)
to determine when to stop. String
indexing starts at 1 in FreeBasic, so we initialize index to 1.
Best Practices
- Initialization: Always initialize loop control variables before the While.
- Termination: Ensure the loop condition will eventually become false.
- Bracketing: Use consistent indentation between While and Wend.
- Complex conditions: For complex conditions, use Boolean variables.
- Comments: Add comments for non-obvious loop conditions or exits.
This tutorial covered the FreeBasic While/Wend
keywords with practical
examples showing their usage in different scenarios.
Author
List all FreeBasic Tutorials.