Select Case Statement

Language Items List

Definition:

Executes one of several statement blocks depending on which of the values or conditions are satisfied by a particular expression.

Syntax:

Select Case testexpression
[Case expressionlist
[statementblock] ]
[Case Else
[statementblock-n] ]
End Select

Description:


Syntax Description


Select Case Starts the Select Case decision control structure. It must always be the first statement in the Select Case structure.

testexpression Any numeric or string expression that defines the expression that is being tested.

Case Designates a group of Phoenix statements to be executed if an expression in expressionlist satisfies testexpression. There can be one or more.

expressionlist A list of single or multiple expressions, separated by commas, in one or both of the following forms:

expression, expression To expression,

Is compare-operator expression

statementblock Consists of several Phoenix statements on one or more lines that are executed if an expression in expressionlist satisfies testexpression.

Case Else An optional clause that defines the statement block to be executed if none of the expressionlists in any of the other Case selections satisfy testexpression.


Note: It is recommended that you include a Case Else statement in your Select Case block. This provides a means for handling unexpected testexpression values.

End Select Ends the Select Case decision control structure. It must be the last statement in the Select Case control structure.

The argument expressionlist is described as follows:


Syntax Description


expression Any numeric or string expression. The expression type must be the same as that of the testexpression. (The type of the expression will be automatically changed to the same type as testexpression. For example, if testexpression is an integer, expressionlist can contain a double-precision expression, but it will be changed to an integer.)

To Used to specify a range of values. When using the To keyword to specify a range, the smaller value of the range must be placed before To.

compare- Any valid comparison operator.
operator

Details:

The Select Case statement is executed as follows. Execution begins at the Select Case clause. If testexpression matches the expressionlist associated with a Case clause, the statementblock following that Case clause is executed up to the next Case clause. If it is the final Case clause it is executed up to End Select. Program execution then branches to the statement following End Select. Even if testexpression matches more than one Case clause, only the statements following the first match are executed.

The Case Else clause is optional and allows you to define a block of statements that are executed if none of the conditions in any of the Case selections are satisfied. When no Case Else statement and no expression in the Case clauses match testexpression, control passes to the statement following End Select.

Case clauses can test for multiple expressions, ranges of values, or a relation to a value. For example, the following lines are valid:

Case 1 To 5, 15, > 100

(This is True for 1,2,3,4,5,15,100,101,102,etc...)

Case <> 100

(This is True for any number except 100.)

Case < -1, > 1

(This is True for any number except 0.)

Ranges and multiple expressions can also be included for character strings as in the following examples:

Case Red, Blue, Green

(This is True for these string expressions.)

Case Alpha To Omega

(This is True for string expressions >= Alpha and <= Omega lexically.)

You can nest Select Case statements, but be sure to include a matching End Select statement for each one.

See Also:

If...Then...Else Statement