SELECT

SELECT Expression
[ CASE [ Expression ] [ TO Expression #2 ] [ , ... ] ... ]
[ CASE [ Expression ] [ TO Expression #2 ] [ , ... ] ... ]
[ CASE LIKE Expression [ , ... ] ... ]
[ { CASE ELSE | DEFAULT } ... ]
END SELECT

Selects the first true condition from a list of cases, and executes only the code enclosed in the corresponding matching CASE statement ignoring the rest.

If no CASE statement matches, the DEFAULT or CASE ELSE statement is executed.

A CASE statement is a list of single values or interval of two values separated by the TO keyword.

The first expression is optional: CASE TO Expression will match all values up to Expression.

Expressions can be a comma separated list, Eg: CASE "A", "B", "C"

Also, you can match against a regular expression with the CASE LIKE syntax.

Note

The TO keyword can also be used without a lower bound which makes the case like a less than range. For example.
  CASE TO 0
will act as CASE < 0 and since the cases are evaluated in succession
  CASE TO -1
    ...
  CASE TO 1
    ....
  CASE ELSE
    ....
provides an effective range filter for -infinity ... -1 ... 1 ... infinity.

Example

' You want to check the random function of a dice.
' So you repeat the random function a thousand times
' and you count, how many times 1, 2, 3, 4, 5 or 6
' have been thrown.

DIM x AS Integer
DIM w AS Integer
DIM a AS Integer
DIM b AS Integer
DIM c AS Integer
DIM d AS Integer
DIM e AS Integer
DIM f AS Integer

FOR x = 1 TO 1000

  w = Int(Rnd(6) + 1)

  SELECT w
    CASE 1
      a = a + 1
    CASE 2
      b = b + 1
    CASE 3
      c = c + 1
    CASE 4
      d = d + 1
    CASE 5
      e = e + 1
    CASE 6
      f = f + 1
    CASE ELSE
      PRINT "This is impossible!"
  END SELECT

NEXT

PRINT a, b, c, d, e, f

Example using CASE LIKE


  Select sFilename
    CASE LIKE "*.{png,jpg,jpeg,bmp}"
      RunForImages(sFilename)
    CASE ELSE
      RunForOtherFiles(sFilename)
  End Select

See also