BEGIN CASE

BEGIN CASE

Top  Previous  Next

 

The CASE statement provides conditional execution dependant on the result of expression evaluation.

 

 

Format

 

BEGIN CASE

CASE expr

statement(s)

CASE expr

statement(s)

END CASE

 

where

 

expris an expression which can be resolved to a numeric value

 

statement(s)is a group of QMBasic statements. There may be any number of statements in the group (including zero).

 

The expressions are evaluated in turn until one evaluates to True. The statements associated with that test expression are then executed and control passes to the statement following the END CASE. Only one of the statement groups is executed. If none of the test expressions evaluates to True, no statements are executed.

 

It is frequently useful to execute a default set of statements where no specific test expression results in non-zero result. This can be achieved by a case of the form

CASE 1

which makes use of the fact that 1 is the numeric representation of the Boolean True. This must be the final element of the CASE statement.

 

 

Example

 

N = DCOUNT(ITEMS, @VM)

BEGIN CASE

  CASE N = 0

     DISPLAY "There are no items"

  CASE N = 1

     DISPLAY "There is 1 item"

  CASE 1

     DISPLAY "There are " : N : " items"

END CASE

 

This program fragment displays a message indicating the number of values in the ITEMS variable. Note the use of the CASE 1 construct.