Select Case (Select Case Statement)

The Select Case instruction is used to execute one of several statement blocks depending on the value of an expression.

Syntax

Select Case TestExpression

Case ExpressionList1

          [StatementBlock-1]

Case ExpressionList2

          [StatementBlock-2]

Case Else

          [StatementBlock-n]

Case Is comparative expression

          [StatementBlock-n]

Expression

EndSelect

Remarks

Multiple expressions or ranges can be used in each Case clause (i.e., Case 1 To 4, 7 To 9, 11, 13). Select Case statements can be nested.

Parameters

Select Case

The Select Case statement begins the Select Case decision control structure. It must appear before any other part of the Select Case structure.

TestExpression

The TestExpression is any numeric or string expression. If TestExpression matches the ExpressionList associated with a Case clause, the StatementBlock following that Case clause is executed up to the next Case clause, or for the final one, up to the EndSelect. Control then passes to the statement following EndSelect. If TestExpression matches more than one Case clause, only the statements following the first match are executed.

Case

The Case statement sets apart a group of program statements to be executed if an expression in the ExpressionList matches TestExpression.

ExpressionList

The ExpressionList consists of a comma-delimited list of one or more of the following forms.

Where Expression is any numeric expression and To is a keyword used to specify a range of values. If you use the To keyword to indicate a range of values, the smaller value must precede To.

Case Else

The Case Else statement is a keyword indicating the StatementBlock to be executed if no match is found between the TestExpression and an ExpressionList in any of the other Case selections. When there is no Case Else statement and no expression listed in the Case clauses matches TestExpression, program execution continues at the statement following EndSelect. The Case Else statement is optional, but it is a good idea to include the statement to handle unforeseen TestExpression values.

Case Is

The Case Is statement is a keyword that is used before a comparison operator (=, <>, <, <=, >, or >=). If the Is keyword is omitted (for example, Case < 10), it is implied.

Expression

A single expression can be entered to indicate a case of Case Is = Expression.

EndSelect

The EndSelect statement marks the end of the Select Case control structure. It must appear after all other statements in the Select Case control structure.