xHarbour Reference Documentation > Statement Reference xHarbour Developers Network  

DO CASE

Executes a block of statements based on one or more conditions.

Syntax

DO CASE
   CASE <Condition1>
        <Statements1>
 [ CASE <ConditionN>
        <StatementsN> ]
 [ OTHERWISE
        <defaultStatements> ]
END[CASE]

Arguments

CASE <Condition>
<Condition1> to <ConditionN> are logical expressions. The statements following the first expression that yields the value .T. (true) are executed.
OTHERWISE
If no condition results in the value .T. (true), the statements following the OTHERWISE option, if present, are executed.

Description

This statement executes a block of statements if the condition, related to the block, evaluates to true. If a condition evaluates to true, the following statements are executed until the next CASE, OTHERWISE or END[CASE] statement is encountered.

If no condition in the entire DO CASE structure evaluates to true, control is passed to the first statement following the ENDCASE statement.

It is possible to include other control structures like DO WHILE and FOR within a single DO CASE structure.

Note:  The DO CASE...ENDCASE statement is equal to the IF...ELSEIF...ENDIF statement.

An alternative to DO CASE is the SWITCH statement which operates with constant values rather than logical conditions.

Info

See also:BEGIN SEQUENCE, DO WHILE, FOR, IF, SWITCH
Category: Control structures , Statements

Example

// This example shows the use of the DO CASE statement.

   PROCEDURE Main
      ACCEPT "Enter a number: " TO nNumber

      nNumber := Val(Alltrim(nNumber))

      DO CASE
         CASE nNumber = 1
            ? "the number was 1"
         CASE nNumber = 2
            ? "the number was 2"
     OTHERWISE
            ? "the number was not 1 or 2"
     ENDCASE
  RETURN

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe