SELECT CASE

Compatible with:
DOS Maximite CMM MM150 MM170 MM+ MMX Picromite ArmiteL4 Armite F4 ArmiteH7 Picomite CMM2

Syntax:
SELECT CASE value
CASE testexp [[, testexp] ?]
CASE IS comparison
CASE ELSE
END SELECT

Description:

Executes one of several groups of statements, depending on the value of an expression. 'value' is the expression to be tested. 
It can be a number or string variable or a complex expression. 'testexp' is the value that 'exp' is to be compared against. 
It can be:
A single expression (ie, 34, "string" or PIN(4)*5) to which it may equal
A range of values in the form of two single expressions separated by the keyword "TO" (ie, 5 TO 9 or "aa" TO "cc")
A comparison starting with the keyword "IS" (which is optional). For example: IS > 5, IS <= 10.
A space is required after "IS "
When a number of test expressions (separated by commas) are used the  CASE statement will be true if any one of these tests evaluates to true.
If 'value' cannot be matched with a 'testexp' it will be automatically matched to the CASE ELSE. 
If CASE ELSE is not present the program will not execute any <statements> and continue with the code following the END SELECT.
When a match is made the <statements> following the CASE statement will be executed until END SELECT or another CASE is encountered.
The program will then continue with the code following the END SELECT.
An unlimited number of CASE statements can be used but there must be only one CASE ELSE and that should be the last before the END SELECT.
Example:
SELECT CASE nbr%
CASE 4, 9, 22, 33 TO 88
statements
CASE IS < 4, IS > 88, 5 TO 8
statements
CASE ELSE
statements
END SELECT
Each SELECT CASE must have one and one only matching END SELECT statement. 
Any number of SELECT CASE statements can be nested inside the CASE statements of other SELECT CASE statements.

Example responding to different keypresses:

 maxmode = 12
 a = 1
 CLS
 DO
   DO
     k$ = INKEY$
   LOOP UNTIL k$<>""
   '
   SELECT CASE k$
     CASE "Q","q" ' test for upper and lower case
       EXIT DO ' quit the program
     CASE "P","p"
       ' etc
     CASE CHR$(128) ' up arrow
       m = m - 1
       IF m < 1 THEN m = maxMode
     CASE CHR$(129) ' down arrow
       m = m + 1
       IF m > maxMode THEN m = 1
     CASE CHR$(131) ' right arrow res up
       cd = cd + 4
       IF cd > 16 THEN cd = 4
     CASE CHR$(130) ' left arrow  res down
       cd = cd - 4
       IF cd < 4 THEN cd = 16
     CASE "+" ' ratio plus
       IF a < 1.4 THEN a = a + 0.01
     CASE "-" ' ratio minus
       IF a > 0.25 THEN a = a - 0.01
     CASE ELSE ' any other key, same as down arrow
       m = m + 1
       IF m > maxMode THEN m = 1
   END SELECT
   PRINT m,cd,a
 LOOP
END

Another example:

 OPTION EXPLICIT
 OPTION DEFAULT NONE
 DIM INTEGER choice
 DIM k$
 DO
   DO
     k$ = INKEY$
   LOOP UNTIL k$<>""
   choice = VAL(k$)
   
   SELECT CASE choice
     CASE 1
       PRINT "one"
     CASE 2
       PRINT "two"
     CASE 3 TO 9
       PRINT "big number"
     CASE ELSE
       PRINT "Try again"
   END SELECT
 LOOP

If SELECT CASE is not available, a series of IF.. ESLEIF can be used

Last edited: 07 December, 2020