ZetCode

FreeBasic Control Flow

Last modified October 18, 2026

Control flow statements decide which statements run and how many times. This article covers the conditional statements If and Select Case, the loop forms For, While, and Do, and the statements that change a loop's course: Exit and Continue. The individual pages If Then Else, Select Case, and Do Loop cover the full syntax.

If, ElseIf and Else

The If statement runs a block of statements when a condition is true. Additional ElseIf clauses test further conditions in order, and an optional Else clause handles the case where none of them matched. The example turns a numeric score into a letter grade.

flow_ifelse.bas
Dim score As Integer = 85
Dim grade As String

If score >= 90 Then
    grade = "A"
ElseIf score >= 80 Then
    grade = "B"
ElseIf score >= 70 Then
    grade = "C"
Else
    grade = "F"
End If

Print "Score "; score; " gets grade "; grade

The conditions are tested in order. The score 85 fails score >= 90 and passes score >= 80, so the ElseIf branch sets the grade to B. Exactly one branch runs. The Else branch handles every value no earlier condition matched.

$ fbc -lang fb flow_ifelse.bas
$ ./flow_ifelse
Score  85 gets grade B

Select Case

A long If/ElseIf chain that compares one expression against many constants is often clearer as a Select Case statement. The expression after Select Case is evaluated once, and each Case label is then tested against it in turn.

flow_select.bas
Dim temperature As Integer = 22

Select Case temperature
    Case Is < 0
        Print "Freezing"
    Case 0 To 15
        Print "Cold"
    Case 16 To 25
        Print "Comfortable"
    Case Else
        Print "Hot"
End Select

Select Case compares one expression with a list of values. Case Is < 0 compares the expression with a relational operator. Case 0 To 15 matches a range. Each branch ends where the next Case starts, and FreeBasic never falls through to the next branch. The value 22 matches 16 To 25.

$ fbc -lang fb flow_select.bas
$ ./flow_select
Comfortable

For loops with Exit and Continue

A For loop runs for each value in its range. Continue For skips the rest of the current iteration and moves to the next value. Exit For leaves the loop entirely. The loop variable keeps its last value after the loop ends.

flow_exit_continue.bas
Dim i As Integer

For i = 1 To 10
    If i Mod 2 = 0 Then Continue For
    If i > 7 Then Exit For
    Print i;
Next
Print
Print "Stopped at "; i

Even numbers are skipped by Continue For. When i becomes 9, the test i > 7 is true and Exit For ends the loop. The variable i still holds 9 afterward, which the last line shows. The semicolon after Print i keeps the numbers on one line.

$ fbc -lang fb flow_exit_continue.bas
$ ./flow_exit_continue
 1 3 5 7
Stopped at  9

Do loops

A Do While loop tests its condition before each pass, so the body may run zero times. A Do ... Loop Until loop tests its condition after the body, so the body runs at least once.

flow_do.bas
Dim n As Integer = 1

Do While n < 50
    n *= 2
Loop
Print "First power of two at least 50: "; n

Dim count As Integer = 0
Do
    count += 1
Loop Until count = 3
Print "Loop Until ran "; count; " times"

The first loop doubles n until it is at least 50. The values are 2, 4, 8, 16, 32, and 64, and the loop stops at 64. The second loop increments count and stops when it equals 3. Because the test comes after the body, the loop runs exactly three times.

$ fbc -lang fb flow_do.bas
$ ./flow_do
First power of two at least 50:  64
Loop Until ran  3 times

Source

FreeBASIC documentation

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.