Case

Case is used to provide an alternative control structure based on the value of a given expression.

Syntax

 Case EXPRESSIONWhen EXPRESSION_LISTINSTRUCTIONS...When DefaultINSTRUCTIONSEndcase
* `EXPRESSION_LIST` is a list of expressions (at least one) separated by commas.* `EXPRESSION` is an expression that is evaluated and compared to the values found by calculating successively the expressions in EXPRESSION_LIST for the following `When EXPRESSION_LIST` instructions. The instructions found after the first matching `When` instruction are executed, and then the execution resumes after the `Endcase` instruction.

The When Default is optional; if present, it must be the last When instruction of the Case. The instructions placed after it will be executed only if no matching expression was found in all the When instruction following the Case.

Examples

 # Depending on the value of CHOICE, a particular action will be performed.# If "R" is chosen, nothing is done.Case toupper(CHOICE)When "E","D" : Gosub DELETIONWhen "Q","S" : Goto QUITWhen "R"When "M" : Gosub MODIFICATIONWhen Default : Gosub HELPEndcase# Alternative on the expression date$-NUMBER_OF_DAYS that is compared to# other variables or complex expressions# For all the other cases, as there is no "When Default" instruction, nothing will be executed#Case date$-NUMBER_OF_DAYSWhen [29/05/1959]RESULT="This is my birthday"When [18/06/1944],[08/05/1945],[11/11/1918]RESULT= "This was a historical date"RESULT+=" ("+format$(D:DD[/]MM[ ]4Y",date$-NUMBER_OF_DAYS)+")"When date$RESULT "We are today"Gosub ACTIONS_TODAYEndcase

Description and comments

Errors

CodeDescription
10An expression found in a list cannot be compared to the expression following the Case (the data types are not compatible).

See also

When, Endcase, If, Then, Else, Endif.