ZetCode

FreeBasic Exit Keyword

last modified June 16, 2025

The FreeBasic Exit keyword is used to prematurely terminate loops, functions, or subroutines. It provides control over program flow by allowing early exits from code blocks.

Basic Definition

In FreeBasic, Exit is a flow control statement that immediately terminates the current loop, function, or subroutine. It comes in several forms: Exit For, Exit Do, Exit While, and Exit Sub/Function.

The Exit statement is useful when you need to break out of a loop based on a condition, or return early from a function. It helps avoid deeply nested conditional structures and makes code more readable.

Exit For in a For Loop

This example demonstrates using Exit For to terminate a loop early.

exit_for.bas
For i As Integer = 1 To 10
    Print i
    If i = 5 Then
        Exit For
    End If
Next

The loop normally runs from 1 to 10, but we exit when i reaches 5. The Exit For statement immediately terminates the loop and continues execution after the Next statement. This is useful for search operations where you find what you need early.

Exit Do in a Do Loop

Here we use Exit Do to break out of an infinite loop.

exit_do.bas
Dim count As Integer = 0

Do
    count += 1
    Print "Count:"; count
    If count >= 7 Then
        Exit Do
    End If
Loop

Print "Final count:"; count

This creates an infinite loop that increments a counter. When count reaches 7, we exit the loop using Exit Do. Without the exit condition, this loop would run forever. Exit Do provides a controlled way to break out of otherwise infinite loops.

Exit While in a While Loop

This example shows Exit While terminating a While loop.

exit_while.bas
Dim num As Integer = 1

While num < 100
    Print num
    num *= 2
    If num > 20 Then
        Exit While
    End If
Wend

Print "Final number:"; num

The While loop continues while num is less than 100. However, we add an additional condition to exit when num exceeds 20. Exit While immediately stops the loop execution, even though the main condition is still true. This demonstrates nested exit conditions.

Exit Sub in a Subroutine

This example uses Exit Sub to return early from a subroutine.

exit_sub.bas
Sub ProcessValue(value As Integer)
    If value < 0 Then
        Print "Error: Negative value"
        Exit Sub
    End If
    
    Print "Processing value:"; value
    ' More processing code here
End Sub

ProcessValue(10)
ProcessValue(-5)
ProcessValue(20)

The subroutine checks for invalid input (negative numbers). If found, it prints an error and exits immediately using Exit Sub. This prevents the rest of the subroutine from executing with invalid data. Early exits help validate inputs and handle errors cleanly.

Exit Function

In FreeBASIC, you can exit a function early using the Return statement, which immediately ends the function and returns a value to the caller. This is functionally equivalent to using Exit Function followed by an assignment to the function name, but Return is more concise and preferred in modern code.

exit_function.bas
Function FindFirstEven(numbers() As Integer) As Integer
    For i As Integer = LBound(numbers) To UBound(numbers)
        If numbers(i) Mod 2 = 0 Then
            Return numbers(i) ' Exit early with result
        End If
    Next
    Return -1 ' No even number found
End Function

Dim nums(1 To 5) As Integer = {3, 5, 7, 8, 9}
Print "First even:"; FindFirstEven(nums())

This function scans an array for the first even number. As soon as it finds one, it exits immediately using Return, passing the value back to the caller. If no even number is found, it returns -1 as a fallback. While FreeBASIC also supports Exit Function, it is typically used in combination with assigning a value to the function name. In contrast, Return is a direct and expressive way to return a result.

Nested Loops with Exit

This example shows how Exit works with nested loops.

exit_nested.bas
For i As Integer = 1 To 3
    Print "Outer loop:"; i
    For j As Integer = 1 To 5
        Print "  Inner loop:"; j
        If j = 3 Then
            Exit For ' Only exits the inner loop
        End If
    Next
Next

When Exit For is used in nested loops, it only exits the innermost loop containing it. The outer loop continues normally. This behavior is important to understand when working with complex loop structures. Each Exit statement affects only its immediate containing block.

Exit in Select Case

This example demonstrates using Exit in a Select Case structure.

exit_select.bas
Sub HandleChoice(choice As Integer)
    Select Case choice
        Case 1
            Print "Option 1 selected"
            Exit Sub
        Case 2
            Print "Option 2 selected"
            Exit Sub
        Case Else
            Print "Invalid option"
            Exit Sub
    End Select
    
    Print "This line never executes"
End Sub

HandleChoice(2)

Here we use Exit Sub within Select Case structure to return immediately after handling each case. This prevents fall-through to other cases and makes the code's behavior more explicit. The final Print statement never executes because all paths exit the subroutine early.

Best Practices

This tutorial covered the FreeBasic Exit keyword with practical examples showing its usage in different control structures.

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.