SelectCase

=Select Case= toc SELECT CASE is a construction for evaluating and acting on sets of conditions. The syntax for Select Case is:

SELECT CASE expr //- Begin a select block using expr (see CASE)// CASE expr1[,expr2...] //- Execute the code that follows if one or more expressions match the SELECT expression (See SELECT CASE)// END SELECT //- Specify the end of a SELECT block//

SELECT CASE expr CASE valueExpr1 'basic code goes here CASE valueExpr2 'basic code goes here CASE valueExpr3 'basic code goes here CASE ELSE 'basic code goes here END SELECT


 * SELECT CASE expr** - defines the beginning of the construct and **expr** computes a value that will be evaluated. The **expr** can be a numeric variable or a string variable, or an expression such as **a+b**.


 * CASE valueExpr** - following the SELECT CASE statement, are individual CASE statements, specifying the conditions to evaluate for the selected variable. Code after the CASE statement is executed if **valueExpr** for that particular case is equal to the value of **expr** specified by the enclosing SELECT CASE statement. There is no limit to the number of conditions that can be used for evaluation.


 * CASE ELSE** - defines a block of code to be executed if the selected value does not fulfil any other CASE statements.


 * END SELECT** - signals the end of the SELECT CASE construct.

Numeric Case
The following example uses a numeric evaluation.

code format="vbnet" num = 3

select case num case 1 print "one"

case 2 print "two"

case 3 print "three"

case else print "other number"

end select code

The example above prints: code three code

String Case
SELECT CASE can also evaluate string expressions in a similar way to numeric expressions.

String example: code format="vbnet" var$="blue"

select case var$

case "red" print "red"

case "green" print "green"

case else print "color unknown"

end select code

The example above prints: code color unknown code

Multiple Cases
MULTIPLE CASES - may be evaluated when separated by commas.

code case a, b, c code

Numeric Example: code format="vbnet" num = 3

select case num

case 3, 5, 10 print "3, 5, 10"

case 3, 12, 14, 18 print "3, 12, 14, 18"

case else print "Not evaluated."

end select code

The example above prints: code 3, 5, 10 code

String Example: code format="vbnet" var$="green"

select case var$

case "red" print "red"

case "green", "yellow" print "green or yellow"

case else print "color unknown"

end select code

The example above prints: code green or yellow code

Order of Evaluation
Once one of the CASEs has been met, no other case statements are evaluated. In the following example, since the value meets the condition of the first CASE statement, the second CASE statement isn't considered, even though the value meets that condition also.

code format="vbnet" num = 3

select case num

case 3, 5, 10 print "3, 5, 10"

case 3, 12, 14, 18 print "3, 12, 14, 18"

case else print "Not evaluated."

end select code

The example above prints: code 3, 5, 10 code