Conditional statement block
Syntax
Select Case expression
[ Case expressionlist]
[statements]
[ Case Else ]
[statements]
End Select
or
Select Case As Const integerexpression
[ Case constant | enumeration ]
[ statements ]
[ Case Else ]
[ statements ]
End Select
Description
Select Case executes specific code depending on the value of an expression. The expression is compared against each Case, in order, until a matching expression is found. The code inside the matching Case branch is executed, and the program skips down to the end of the Select Case block. Case Else matches any Case not already matched, so if there is a Case Else, at least one Case is guaranteed to be executed. If no Cases match, the whole Select Case block will be skipped.
For C users: In FreeBASIC,
Select Case works as a switch where all cases have a "break;" at the end. As there is no fall-through, multiple options must be put in an expression list in a single
Case.
End Select can be used to exit the
Select Case...End Select block
Besides integer types, floating point and strings
expressions are also supported with the first syntax.
Syntax of an expression list:
{ expression | expression TO expression | IS relational operator expression }[, ...]
example of expression lists:
Case 1 | constant |
Case 5.4 To 10.1 | range |
Case Is > 3 | bigger than-smaller than |
Case 1, 3, 5, 7, 9 | enumeration |
Case x | value of a variable |
If AS CONST is used, only integer constants in the range 0..4097 can be evaluated and the expression list supports single constants and enumerations only. Being limited to integer values allows
Select Case As Const to be faster than
Select Case.
Example
Dim choice As Integer
Input "Choose a number between 1 and 10: "; choice
Select Case As Const choice
Case 1
Print "number is 1"
Case 2
Print "number is 2"
Case 3, 4
Print "number is 3 or 4"
Case 5 To 10
Print "number is in the range of 5 to 10"
Case Else
Print "number is outside the 1-10 range"
End Select
Differences from QB
- SELECT CASE AS CONST did not exist in QB
See also