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 withtest_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 toexpression_2
.The comparison construct lets you use all the comparison operators:
<
,<=
,>
,>=
,=
, or<>
.The equality construct operates the same as
Is =
.statements
- Optional one or more statements after
Case
that execute iftest_expression
agrees with one of the expressions inexpression_list
. else_statements
- Optional one or more statements after
Case Else
that execute only if none of the expressions in the sets ofexpression_list
agree withtest_expression
. End
- Completes the statement.
You can also use
End Select
.
Instructions
If test_expression
agrees with one of Case
expression_list
, 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, only the first one that agrees gets executed.
The line Case Else
introduces the block else_statements
, which executes only when test_expression
agrees with none of 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, 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:
@Option Select Case
and@Option Select
@Option Select Else
and@Option Select Else String
@Option End Block
and@Option End
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