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 with Enum) that also has the modifier @Strict. If it does, you must use all the constants with the equality construct, and without Case 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 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 operates the same as Is =.

statements
Optional one or more statements after Case that run if test_expression agrees with one of the expressions in expression_list.
else_statements
Optional one or more statements after Case Else that run only if none of the expressions in the sets of expression_list agree with test_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:

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
Output:
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz
...

See also