CASE Statement
Allows conditional execution of a sequence of statements.
Syntax
BEGIN CASE
CASE expression
statement(s)
CASE expression
statement(s)
.
.
.
END CASE
Syntax Elements
expression is any valid DataBasic expression that evaluates to true (non-zero) or false (0).
statement(s) is a sequence of one or more valid DataBasic statements.
Operation
CASE statements are processed in turn, commencing at the one immediately after the BEGIN CASE statement. Expressions in CASE statements are evaluated in the same way as expressions in IF statements.
If an expression is false (zero), then control passes to the next CASE statement.
If an expression is true (1), the sequence of statement(s) immediately following the CASE statement containing that expression is executed. When the next CASE statement is reached, control passes to the first statement after the END CASE statement.
If all CASE expressions are found to be false, then control passes to the first statement after the END CASE statement.
A CASE 1 condition included as the last CASE statement before END CASE (see example 1), functions like an else clause. The test expression (1) is always true and statement(s) below it will be executed if all previous CASE expressions are false.
Programs containing unmatched BEGIN CASE or END CASE statements will not compile.
Examples
BEGIN CASE
CASE A < 5
PRINT 'A IS LESS THAN 5'
CASE A < 10
PRINT 'A IS GREATER THAN OR EQUAL TO 5 AND LESS THAN 10'
CASE 1
PRINT 'A IS GREATER THAN OR EQUAL TO 10'
END CASE
If A<5, the first PRINT statement is executed. If 5<=A<10, the second PRINT statement is executed. Otherwise, the third PRINT statement is executed.
BEGIN CASE
CASE A=0; GOTO 10
CASE A<0; GOTO 20
CASE 1; GOTO 30
END CASE
Control branches to the statement labeled 10 if A is zero, to 20 is A is negative or to 30 if A is greater than zero.
BEGIN CASE CASE ST MATCHES "1A" MAT LET=1 CASE ST MATCHES "1N" SGL=1; A.1(I)=ST CASE ST MATCHES "2N" DBL=1; A.2(J)=ST CASE ST MATCHES "3N" GOSUB 103 END CASE
If ST is one letter, the value 1 is assigned to all LET elements, and the case ends. If ST is one number, then 1 is assigned to SGL, ST is stored at element A.1(I), and the case ends. If ST is two numbers, 1 is assigned to DBL, ST is stored at A.2(J), and the case ends. If ST is three numbers, subroutine 103 is executed. If none of the cases is true, control passes to the statement following END CASE.