Partager via


DO CASE ... ENDCASE Command

Executes the first set of commands whose conditional expression evaluates to true (.T.).

DO CASE
   CASE lExpression1 
   [Commands]
   [CASE lExpression2 
   [Commands]]
   ...
   [CASE lExpressionN 
   [Commands]]
   [OTHERWISE 
   [Commands]]
ENDCASE

Parameters

  • CASE lExpression1 Commands...
    When the first true (.T.) CASE expression is encountered, the set of commands following it is executed. Execution of the set of commands continues until the next CASE or ENDCASE is reached. Execution then resumes with the first command following ENDCASE.

    If a CASE expression is false (.F.), the set of commands following it up to the next CASE clause is ignored.

    Only one set of commands is executed. These are the first commands whose CASE expression evaluates to true (.T.). Any succeeding true (.T.) CASE expressions are ignored.

  • OTHERWISE Commands
    If all of the CASE expressions evaluate to false (.F.), OTHERWISE determines if an additional set of commands is executed.

    • If you include OTHERWISE, the commands following OTHERWISE are executed and execution skips to the first command following ENDCASE.

    • If you omit OTHERWISE, execution skips to the first command following ENDCASE.

Remarks

DO CASE is used to execute a set of Visual FoxPro commands based on the value of a logical expression. When DO CASE is executed, successive logical expressions are evaluated; the values of the expressions determine which set of commands is executed.

Comments can be placed after DO CASE and ENDCASE on the same line. The comments are ignored during program compilation and execution.

Example

In this example, Visual FoxPro evaluates each CASE clause until the MONTH variable is found in one of the lists. The appropriate string is stored to the variable rpt_title and the DO CASE structure is exited.

STORE CMONTH(DATE( )) TO month  && The month today
   
DO CASE  && Begins loop
   
   CASE INLIST(month,'January','February','March')
      STORE 'First Quarter Earnings' TO rpt_title
   
   CASE INLIST(month,'April','May','June')
      STORE 'Second Quarter Earnings' TO rpt_title
   
   CASE INLIST(month,'July','August','September')
      STORE 'Third Quarter Earnings' TO rpt_title
   
   OTHERWISE
      STORE 'Fourth Quarter Earnings' TO rpt_title
ENDCASE  && Ends loop
WAIT WINDOW rpt_title NOWAIT

See Also

Reference

DO WHILE ... ENDDO Command

EXIT Command

FOR EACH ... ENDFOR Command

FOR ... ENDFOR Command

IF ... ENDIF Command

IIF( ) Function

SCAN ... ENDSCAN Command

Other Resources

Commands (Visual FoxPro)

Language Reference (Visual FoxPro)