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.
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.
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.
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.
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.
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.
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.
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
- Readability: Use indentation consistently for nested conditionals.
- Order: Place most likely conditions first in ElseIf chains.
- Simplicity: Avoid deeply nested If statements when possible.
- Parentheses: Use them to clarify complex Boolean expressions.
- Comments: Add comments for complex or non-obvious conditions.
This tutorial covered FreeBasic conditional statements with practical examples showing If/Then/Else/ElseIf usage in different scenarios.
Author
List all FreeBasic Tutorials.