Select…Case
Statement
Runs one of many blocks of statements, because of a test expression.
Syntax
[ @Strict ] _
Select [ Case ] test_expression [ tolerance ]
Case expression_list
[ statements ]
…
[ Case Else
[ else_statements ] ]
End [ Select ]
Parts
@Strict
- Optional modifier specifies that you must supply tests for all possible values of
test_expression
. test_expression
can be an enumeration type (declared withEnum
) that also has the modifier@Strict
. If it does, you must use all the constants with the equality construct, and withoutCase Else
. Other data types put no such limits on the syntax.Case
- Usually optional keyword after
Select
. It becomes mandatory if you use the directive@Option Select Case
. - mandatory keyword that starts each of the subsequent blocks.
test_expression
- Mandatory expression of an elementary data type.
tolerance
- Optional floating-point constant (after the keyword
Tol
) that gives 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 run iftest_expression
agrees with one of the expressions inexpression_list
. else_statements
- Optional one or more statements after
Case Else
that run 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 the expressions after Case
, the related block of statements that follows it runs.
The block continues until the next Case
, Case Else
, or End Select
.
Then the statement after End Select
runs.
If more than one expression can agree, only the first one that agrees runs.
The line Case Else
is related to the block else_statements
.
This block runs only when test_expression
agreed with none of the sets of expressions that came before it.
Although usually not mandatory, we recommend that you supply Case Else
if unusual values of test_expression
are possible.
If none of the blocks that start with Case
ran, the statement after End
runs.
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 Strict
and@Option Select Strict String
@Option End Block
and@Option End
See @Option Directive for more information.
Examples
Program FizzBuzz
Require ViviFire.IO
For num = 1 To 100
Select Case num Mod 15
Case 0
PrintLine "fizzbuzz"
Case 3, 6, 9, 12
PrintLine "fizz"
Case 5, 10
PrintLine "buzz"
Case Else
PrintLine num
End Select
End For
1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz ...