GLBasic User Manual

Main sections

CASE

SELECT n
CASE 3; ...
CASE >3; ...
CASE 2 TO 5; ...
DEFAULT; ...
ENDSELECT



The CASE keyword supplies an expression to be matched against the value of n. If the value of n meets the conditions of the particular CASE statement being evaluated, the code-block following that CASE statement will be run. If more than one CASE statement matches the requirements, only the first matching code-block will be executed.

DEFAULT (optional) is a special type of CASE statement. If none of the CASE statements match n then the code-block under the DEFAULT statement (if a DEFAULT case exists) will be run.

CASE statement examples
-CASE 3
'n' must be exactly 3
-CASE >3
'n' must be bigger than 3
-CASE 2 TO 5
'n' must be at least 2 and not more than 5
-DEFAULT
None of the statements above matched 'n'

Example:
 
// SELECT CASE DEFAULT ENDSELECT

FOR n=0 TO 10

PRINT "n="+n, 100, 100

SELECT n
CASE 7
PRINT "n="+n+": CASE 7", 0, n*10
CASE >9
PRINT "n="+n+": CASE >9", 0, n*10
CASE >3
PRINT "n="+n+": CASE >3", 0, n*10
CASE 3 TO 8
PRINT "n="+n+": CASE 3 TO 8", 0, n*10
DEFAULT
PRINT "n="+n+": DEFAULT", 0, n*10
ENDSELECT
NEXT
SHOWSCREEN
MOUSEWAIT



Output:
n=0: DEFAULT
n=1: DEFAULT
n=2: DEFAULT
n=3: CASE 3 TO 8
n=4: CASE >3
n=5: CASE >3
n=6: CASE >3
n=7: CASE 7
n=8: CASE >3
n=9: CASE >3
n=10: CASE >9

See also...