ZetCode

FreeBasic Select Case Keyword

last modified June 16, 2025

The FreeBasic Select Case statement provides a clean way to handle multiple conditional branches. It's often more readable than a series of If-ElseIf statements when testing a single variable.

Basic Definition

Select Case evaluates an expression once and compares it against multiple possible values. Each value is specified in a Case block. The Case Else block handles any unmatched values.

The structure begins with Select Case expression and ends with End Select. Between them are Case statements that define the conditions to match against the expression.

Basic Select Case Example

This example demonstrates the simplest form of Select Case statement.

basic_case.bas
Dim grade As String = "B"

Select Case grade
    Case "A"
        Print "Excellent!"
    Case "B"
        Print "Good job!"
    Case "C"
        Print "You passed"
    Case Else
        Print "Needs improvement"
End Select

Here we evaluate a letter grade and print a corresponding message. The code matches the "B" case and executes its block. Case Else would catch any grades not explicitly listed (D, F, etc.).

Numeric Range Matching

Select Case can match against numeric ranges using the To keyword.

numeric_range.bas
Dim temperature As Integer = 22

Select Case temperature
    Case Is < 0
        Print "Freezing"
    Case 0 To 10
        Print "Very cold"
    Case 11 To 20
        Print "Cool"
    Case 21 To 30
        Print "Comfortable"
    Case Else
        Print "Too hot"
End Select

This example checks temperature ranges. The Is keyword allows comparison operators. Ranges are specified with To. The 22° temperature falls in the "Comfortable" range.

Multiple Values in a Case

A single Case statement can match against multiple values separated by commas.

multiple_values.bas
Dim day As String = "Mon"

Select Case day
    Case "Mon", "Tue", "Wed", "Thu", "Fri"
        Print "Weekday"
    Case "Sat", "Sun"
        Print "Weekend"
    Case Else
        Print "Invalid day"
End Select

This code checks if a day is a weekday or weekend day. The first Case matches any weekday abbreviation. Multiple values make the code concise without needing separate Case statements for each value.

Type Matching with Select Case

Select Case can work with different data types, including strings and numbers.

type_matching.bas
Dim value As Variant = 3.14

Select Case VarType(value)
    Case VT_INTEGER, VT_LONG
        Print "Integer number"
    Case VT_SINGLE, VT_DOUBLE
        Print "Floating-point number"
    Case VT_STRING
        Print "String"
    Case Else
        Print "Unknown type"
End Select

This example uses VarType to check a variable's type. The Case statements match against FreeBasic's type constants. The code prints "Floating-point number" for the 3.14 value.

Select Case with Enumerations

Enumerations work well with Select Case for readable code.

enum_case.bas
Enum Colors
    RED
    GREEN
    BLUE
End Enum

Dim favorite As Colors = GREEN

Select Case favorite
    Case RED
        Print "You chose red"
    Case GREEN
        Print "You chose green"
    Case BLUE
        Print "You chose blue"
End Select

Here we define an enumeration of colors and use it in a Select Case. The code matches the GREEN case. Enums make the code more maintainable than using magic numbers directly in Case statements.

Nested Select Case

Select Case statements can be nested inside each other for complex logic.

nested_case.bas
Dim category As String = "fruit"
Dim item As String = "apple"

Select Case category
    Case "fruit"
        Select Case item
            Case "apple"
                Print "Apple selected"
            Case "banana"
                Print "Banana selected"
            Case Else
                Print "Unknown fruit"
        End Select
    Case "vegetable"
        Print "Vegetable selected"
    Case Else
        Print "Unknown category"
End Select

This example shows nested Select Case statements. The outer case matches "fruit", then the inner case checks specific fruits. Nested cases help organize complex decision trees while keeping code readable.

Select Case with Expressions

The expression in Select Case can be more complex than just a variable.

expression_case.bas
Dim x As Integer = 5
Dim y As Integer = 10

Select Case (x + y)
    Case 1 To 5
        Print "Sum is small"
    Case 6 To 15
        Print "Sum is medium"
    Case Is > 15
        Print "Sum is large"
End Select

Here we evaluate the sum of two variables in the Select Case expression. The sum 15 falls in the "medium" range. This demonstrates that any valid expression can be used, not just simple variables.

Best Practices

This tutorial covered the FreeBasic Select Case statement 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.