ZetCode

FreeBasic Conditional Statements

last modified June 16, 2025

The FreeBasic If/Then/Else/ElseIf statements allow programs to make decisions based on conditions. These keywords control program flow by executing different code blocks depending on Boolean expressions.

Basic Definition

The If statement evaluates a condition and executes code if the condition is true. Then marks the start of the code block to execute when the condition is true.

Else provides an alternative code block that executes when the condition is false. ElseIf allows checking multiple conditions in sequence within a single If statement.

Simple If/Then Statement

This example demonstrates the basic If/Then structure.

simple_if.bas
Dim age As Integer = 18

If age >= 18 Then
    Print "You are an adult"
End If

The code checks if the age variable is 18 or more. If true, it prints the message. The condition age >= 18 evaluates to a Boolean value. The End If marks the end of the conditional block.

If/Then/Else Statement

This example shows how to use Else to provide an alternative path.

if_else.bas
Dim temperature As Integer = 25

If temperature > 30 Then
    Print "It's hot outside"
Else
    Print "It's not too hot"
End If

The condition checks if temperature exceeds 30. If true, it prints the first message. Otherwise, the Else block executes. Only one of the two messages will be displayed depending on the condition.

If/Then/ElseIf/Else Statement

This example demonstrates multiple conditions with ElseIf.

elseif.bas
Dim score As Integer = 85

If score >= 90 Then
    Print "Grade: A"
ElseIf score >= 80 Then
    Print "Grade: B"
ElseIf score >= 70 Then
    Print "Grade: C"
Else
    Print "Grade: D or F"
End If

The code checks multiple grade thresholds in sequence. When a condition is met, its block executes and the rest are skipped. The Else clause handles all remaining cases not covered by previous conditions.

Nested If Statements

This example shows If statements inside other If statements.

nested_if.bas
Dim age As Integer = 25
Dim hasLicense As Boolean = True

If age >= 18 Then
    If hasLicense Then
        Print "You can drive"
    Else
        Print "You need a license"
    End If
Else
    Print "You're too young to drive"
End If

The outer If checks the age, while the inner If checks the license status. Nested conditionals allow for more complex decision trees. Each If must have its own End If statement to mark its block's end.

Single-Line If Statement

FreeBasic allows simple If statements on a single line.

single_line_if.bas
Dim number As Integer = 7

If number Mod 2 = 0 Then Print "Even" Else Print "Odd"

This compact form is useful for very simple conditions. The entire If/Then/Else structure fits on one line. Note that only one statement can follow Then and Else in this format. Complex logic should use multi-line format.

Logical Operators in Conditions

This example combines multiple conditions with logical operators.

logical_operators.bas
Dim age As Integer = 22
Dim hasTicket As Boolean = True
Dim hasID As Boolean = True

If age >= 21 And hasTicket And hasID Then
    Print "You can enter the club"
Else
    Print "Entry requirements not met"
End If

The condition uses the And operator to require all three sub-conditions to be true. FreeBasic supports And, Or, Not, and Xor for combining Boolean expressions. Parentheses can clarify complex conditions.

Selecting Between Multiple Options

This example shows a more complex decision structure.

multiple_options.bas
Dim day As Integer = 3 ' 1=Monday, 7=Sunday

If day = 1 Or day = 2 Or day = 3 Then
    Print "Early week"
ElseIf day = 4 Or day = 5 Then
    Print "Mid week"
ElseIf day = 6 Then
    Print "Weekend starts"
ElseIf day = 7 Then
    Print "Sunday"
Else
    Print "Invalid day number"
End If

The code categorizes days of the week using multiple ElseIf clauses. Each condition is checked in order until one matches. The Else handles any invalid day numbers. This structure is cleaner than multiple separate If statements.

Best Practices

This tutorial covered FreeBasic conditional statements with practical examples showing If/Then/Else/ElseIf 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.