BEGIN CASE CASE expression1 statement CASE expression2 statement . . . END CASE
|expression||A value, variable, or expression that evaluates to a boolean value: FALSE = 0, TRUE=1 or any numeric value other than 0.|
|statement||One or more MVBasic statements to execute if the corresponding expression evaluates to TRUE.|
The CASE statement tests each case in the order specified, and executes the statement(s) associated with the first expression that evaluates to true (a numeric value other than 0). An unlimited number of CASE statements can be specified within the BEGIN CASE ... END CASE clause. At most, only one CASE statement is taken — the first case that evaluates to a true value. Matching stops when the first expression that evaluates to true is encountered.
If no CASE expression evaluates to true, execution continues with the first statement after the END CASE statement.
You can specify a default case by specifying an expression that always evaluates to true (1). Typically, the literal integer value 1 is used as the expression in the last CASE clause: CASE 1. The statements associated with this clause will be executed if all the other CASE clauses evaluate to false (0).
You cannot use a GOTO statement to transfer execution within a CASE statement.
CASE statements can be nested. You can use a GOTO statement to transfer execution from a CASE clause to a nested a CASE statement.
A placeholder CASE block, consisting of just the BEGIN CASE and END CASE statements, is supported.
CASE evaluates expression to a boolean value. If true, the case is taken and its statements executed. If false, the case is skipped over, and the next CASE expression is evaluated. CASE expressions are evaluated in the order specified; therefore, an error in an expression (for example, a divide-by-zero error: CASE var/0) is not detected if a prior expression is taken.
One or more statements executed if expression evaluates to true. If expression does not evaluate to true, statement is not parsed.
The following example takes a user input and executes one of the specified cases based on length of the input string. The final case (CASE 1) is always true. This provides a case that is always taken if all of the previous cases did not evaluate to true:
INPUT myword BEGIN CASE CASE 5 > LEN(myword) CRT "short" CRT "word" CASE 5 < LEN(myword) CRT "long" CRT "word" CASE 1 CRT "five letter" CRT "word" END CASE CRT "all done"
The following example shows nested CASE statements. It shows how a GOTO can be used to transfer execution to a nested CASE statement:
INPUT myword BEGIN CASE CASE 5 > LEN(myword) CRT "short word" Atest: BEGIN CASE CASE 1 = COUNT(myword,"A") CRT "contains one A" CASE 1 < COUNT(myword,"A") CRT "contains more than one A" CASE 1 CRT "contains no A" END CASE CASE 5 < LEN(myword) CRT "long word" GOTO Atest: CASE 1 CRT "five letter word" GOTO Atest: END CASE