Select...Case Statement

Executes one of several 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 in comparisons. 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 matches 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 match test_expression.
End
Completes the statement. You can also use End Select.

Instructions

If test_expression matches 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 match is possible, only the first of Case that matches gets executed.

The line Case Else introduces the else_statements, which get executed only when there are no matches between test_expression and all of the expression_list. Although not a requirement, it is considered a good practice to include one to handle any unexpected values of test_expression. If there are no matching Case blocks and no Case Else, 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

TODO

See also