Select Statement

Executes one of several groups of statements, depending on the value of an expression.

Syntax

Select testexpression

[Case expressionlist1

[statements-n]]

[Case Else

[elsestatements]]

EndSelect

The Select statement syntax has the following parts:

 

Name

Description

testexpression

An integer or string expression.

expressionlist-n

Required if a Case appears. The expressionlist-n must of the same type as the testexpression and must be a constant. Expressionlist-n be one or more of the following forms:

expression,

expression To expression,

The To keyword specifies a range of values. If you use the To keyword, the smaller value must appear before To.

statements-n

One or more statements executed if testexpression matches any part of expressionlist-n.

elsestatements

One or more statements executed if testexpression does not match any of the Case clauses.

Comments

If testexpression matches any Case expressionlist expression, the statements following that Case clause are executed up to the next Case clause, or, for the last clause, up to EndSelect. Control then passes to the statement following EndSelect. If testexpression matches an expressionlist expression in more than one Case clause, only the statements following the first match are executed.

The Case Else clause is used to indicate the elsestatements to be executed if no match is found between the testexpression and an expressionlist in any of the other Case selections. Although not required, it is a good idea to have a Case Else statement in the Select block to handle unforeseen testexpression values. If no Case expressionlist matches testexpression and there is no Case Else statement, execution continues at the statement following EndSelect.

You can use multiple expressions or ranges in each Case clause. For example, the following line is valid:

Case 1 To 4, 7 To 9, 11, 13

You also can specify ranges and multiple expressions for character strings. In the following example, Case matches strings that are exactly equal to "everything", then strings that fall between "nuts" and "soup" in alphabetic order:

Case "everything", "nuts" To "soup"

Select statements can be nested. Each nested Select statement must have a matching EndSelect statement.

The Select statement is an alternative to If...Then...Else for selectively executing one block of statements from among multiple blocks of statements. A Select statement provides capability similar to the If...Then...Else statement, but it makes code more readable when there are several choices.

A Select structure works with a single testexpression that is evaluated once, at the top of the structure. ATEasy then compares the result of this expression with the values for each Case in the structure. If there is a match, it executes the block of statements associated with that Case.

Each expressionlist is a list of one or more values. If there is more than one value in a single list, the values are separated by commas. Each statementblock statements-n contains zero or more statements. If more than one Case matches testexpression, only the statement block associated with the first matching Case will execute. If none of the values in the expression lists matches testexpression, ATEasy executes statements in the Case Else clause (which is optional).

Notice that the Select structure evaluates an expression once at the top of the structure. In contrast, the If...Then...Else structure can evaluate a different expression for each ElseIf statement. You can replace an If...Then...Else structure with a Select structure only if the If statement and each ElseIf statement evaluates the same expression.

Example

This example uses the Select statement to evaluate the value of a variable. The second Case clause contains the value of the variable being evaluated, and therefore only the statement associated with it is executed.

nSwt = 8 ! Initialize variable.

Select nSwt ! Evaluate nSwt.

Case 1 To 5 ! nSwt between 1 and 5, inclusive.

Print "Between 1 and 5"

! The following is the only Case clause

! that evaluates to True.

Case 6, 7, 8 ! nSwt between 6 and 8.

Print "Between 6 and 8"

Case 9 To 10 ! nSwt is 9 or 10.

Print "Greater than 8"

Case Else ! Other values.

Print "Not between 1 and 10"

EndSelect

See Also

#Ifdef, Expressions, If