Select...Case Statement

Executes one of many blocks of statements, because of a test expression.

Syntax

Select [ Case ] test_expression [ Tol tolerance ]
    Case expression_list
        [ statements ]
    …
    [ Case Else
        [ else_statements ] ]
End [ Select ]

Parts

test_expression
Mandatory expression of an elementary data type.
tolerance
Optional floating-point constant specifies the tolerance for rounding errors when values are compared. See Tol Clause for more information.
expression_list

Mandatory after Case, one or more expressions for tests with test_expression with a comma between each. It can be one of the constructs that follow:

Range construct

expression_1 To expression_2

Comparison construct

Is comparison_op expression

Equality construct

expression

The range construct lets you compare a range of values. The value of expression_1 must be less than or equal to expression_2.

The comparison construct lets you use all the comparison operators: <, <=, >, >=, =, or <>.

The equality construct is the same as if you used Is =.

statements
Optional one or more statements after Case that execute if test_expression agrees with one of expression_list.
else_statements
Optional one or more statements after Case Else that execute only if none of expression_list in all of Case agree with test_expression.
End
Completes the statement. You can also use End Select.

Instructions

If test_expression agrees with one of Case expression_list, then control moves to the block of statements that follow it and continues until the next Case, Case Else, or End Select. Control then moves to the statement after End Select. If more than one expression can agree, then only the first one that agrees gets executed.

The line Case Else introduces the else_statements, which get executed only when test_expression agrees with none in expression_list. Although not mandatory, it is thought a good practice to have one if unusual values of test_expression are possible. If none of the blocks that start with Case executed, then control moves to the statement after End Select.

Changes in syntax with @Option

There are three areas where you can change the syntax of the statement:

See @Option Directive for more information.

Examples

' FizzBuzz
For n = 1 To 100
    Select Case n Mod 15
    Case 0
        PrintLine "fizzbuzz"
    Case 3, 6, 9, 12
        PrintLine "fizz"
    Case 5, 10
        PrintLine "buzz"
    Case Else
        PrintLine n
    End Select
End For

See also