ZetCode

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.

do_while_counter.bas
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.

do_while_input.bas
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.

do_while_sentinel.bas
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.

do_while_file.bas
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.

do_while_menu.bas
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.

do_while_game.bas
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.

do_while_nested.bas
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

This tutorial covered the FreeBasic Do While keyword with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.