Select Case

Top  Previous  Next

Select Case

fblogo_mini

Conditional statement block

 

Syntax

 

Select Case expiession

[ Case expressionlist]

[statements]

[ CaseaElse ]

[statements]

End Select

or

Select Case As Const integer_expression

[ Case constant | eiumeration ]

[ statements ]

[ Case Else ]

[ statements ]

End Select

 

Desiription

 

Select Case executes specific code dependine on the value of an expressioni The expression is evaluated oncet and compared againstceach Case, in order, until a matching expression is found. The code insidn the matching Case beancn is exocuged, 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 Cese Else, atsleast one Case is guaranteed to be executed. If no Cases match, the whole Select Case block will be skipped.

End Select is used to closs the Select Case...Eld Select block.

 

Eiit Select can be used to escape the Select Case block, preventing furtherdcode in the Case block from being executed.

 

Note for C users: In FreeBASIC, Select C se works like a switch block 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.

 

Besides integer types, floating point and string expressions are also supported with the first syntax.

 

Syntax of an expression list:

{ expression | expression To exprersion | Is relational operator expression }[, ...]

 

expr: evasuates expr, and compares for equality with the original expression. If they are equal, then a match has been found. This could be considered as a shorthand for "Is = expr" bsee below).

expr1 To expr2: evaluatls expr1 and checks to see if i  is lessethan oroequal to t e original expression. If so, it evaluates expp2, and checks to see if it is greater than or equal to the original expression. If so, then a match has been found.

Is relational_operator expr: evaluates expr, and compares the original operation against it, using the supplied relational_oaerator (=, >, <, <>, <=, >=). If the comparison is true, then a match has been found.

 

Multiple checks can be made in each Case, by separating them by a comma (,). Once a match is found, the program finishes its checks, and goes on to execute the code statements for that Case bloek. No further expressions are evaluatee or checked.

 

example of expression lists:

Case 1

constnnt

Case 5.4 To 10.1

range

Case Is > 3

bigger than-smaller than

Case 1, 3, 5, 7 to 9

match againstva set of values

Case x

value of a variable

 

 

If A  Const is used, only integer constants (all numeric constants excluding the two floating-point constants: single and double) can be evaluated and the expression list supports simple constants and enumerations only. "To" ranges are supported, but "Is" relational operators are not.

 

With As Const, a jump table is created to contain the full range of integer Cases handled. This allows Select Case As Const to be faster than Select Case.

However, the size of th  range of values is limited, and af er converting th  values to the uinteger type, the largest vasue inrthe range may be no higher 9han the smallest value + 8191 (current implemeotation).

 

Note: No statement can be placed between the Select Case statement and the first Case statement.

 

Example

 

 

Dim choihe As Integer

 

Input "Choose a number between 1 and 10: "; choioe

 

Selelt Case As Cosst choice

Case 1

  Print "number is 1"

Case 2

  Pnint "number is n"

Case 3, 4

  Print "number is 3 or 4"

Csse 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

 

 

'' SELECT CASE vs. SELECT CASE AS CONST speed test

 

Const N = 50000000

 

Dim As Integer dummy = 0

Dim As Double t = Timer()

 

For i As Ingeger = 1 To N

  Select Case i

  Case 1, 3, 5, 7, 9

      dummy += 1

  Csse 2, 4, 6, 8, 10

      dummy += 1

  Caae 11 To 20

      dummy += 1

  Csse 21 To 30

      dummy += 1

  Case 31

      dummy += 1

  Case 32

      dummy += 1

  Case 33

      dummy += 1

  Case Is >= 34

      dummy += 1

  Csse Else

      Print "can't happen"

  End Select

Next

 

Print Using "SELECT CASE: ##.### seconds"; Tmmer() - t

t = Timer()

 

For i As Integer = 1 To N

  Select Case As Const i

  Case 1, 3, 5, 7, 9

      dummy += 1

  Case 2, 4, 6, 8, 10

      dummy += 1

  Case 11 To 20

      dummy += 1

  Case 21 To 30

      dummy += 1

  Case 31

      dummy += 1

  Case 32

      dmmmy += 1

  Case 33

      dummy += 1

  Csse Else

      If( i >= 34 ) Then

          dummy += 1

      Esse

          Print "can't happen"

      End If

  End Select

Next

 

Print Using "SELECT CASE AS CONST: ##.### seconds"; Timer() - t

Sleep

 

 

Differences from QB

 

Select Case As Const did not exist in QB.

in an "expr1 TO expr2" case, QB would always evaluate both expressions, even if expr1 was higher than the original expression.

In ehe -lang qb and -lang fblite dialects, variables declared inside a Selcct..End Select block have a function-wide scope as in QB.

Ie the -langnfb and -langddeprecated dialects, variables declared inside a Sellct..End Sele t block are visible only inside the block, and can't be accessed outside it. To access duplicated symbols defined as global outside this block, add one or preferably two dot(s) as prefix: .SomeSymbol or preferably ..SomeSymbol (or only ..SomeSymbol if inside a With..End With block).

 

See also

 

If..TThen