Release 10.1A: OpenEdge Development:
Progress 4GL Handbook

> ## The CASE statement

> If you have a sequence of `IF,THEN,ELSE` clauses that all operate on different values of the same variable or expression, you can combine these into a single block using the `CASE` statement, which has this syntax:

>     CASE expression :
>       { WHEN value [ OR WHEN value ] ... THEN
>            { block | statement }
>       } ...
>       [ OTHERWISE
>            { block |statement }
>       ]
>     END [ CASE ].

> The `expression` can be a simple field or variable or any other expression involving multiple fields or values. Part of the optimization of the `CASE` statement is that it evaluates the expression only once, when the `CASE` statement is entered. By contrast, nested `IF` statements evaluate the expression in each `IF` clause, even if the expression is the same each time.

> Following the block header are a number of `WHEN` clauses, each of which starts with a `value` for the `expression`, followed by `THEN`, followed by a statement or block to execute if the expression has that value. You can combine multiple `WHEN` clauses with `OR` if the same block or statement executes on multiple values of the expression.

> Finally, the `CASE` block can conclude with an optional `OTHERWISE` clause with a statement or block to execute if the expression matches none of the values in the `WHEN` clauses.

> The `CASE` statement is most useful when a variable or field can have one of a small number of possible values, and the procedure needs to react differently to each value.

> Here’s a simple example that uses the `CASE` statement to count the number of Orders of different types. There are four valid values for the OrderStatus field. The `OTHERWISE` clause tallies any that don’t match any of the valid values (as it turns out, there aren’t any):

>     DEFINE VARIABLE iOrderStatus AS INTEGER EXTENT 5     NO-UNDO.
>       FOR EACH Order:
>         CASE Order.OrderStatus:
>             WHEN "Shipped" THEN
>                 iOrderStatus[1] = iOrderStatus[1] + 1.
>             WHEN "Ordered" THEN
>                 iOrderStatus[2] = iOrderStatus[2] + 1.
>             WHEN "Back Ordered" THEN
>                 iOrderStatus[3] = iOrderStatus[3] + 1.
>             WHEN "Partially Shipped" THEN
>                 iOrderStatus[4] = iOrderStatus[4] + 1.
>             OTHERWISE
>                 iOrderStatus[5] = iOrderStatus[5] + 1.
>         END CASE.
>       END.  /* END FOR EACH Order */
>       DISPLAY iOrderStatus[1] LABEL "Shipped Orders" SKIP
>               iOrderStatus[2] LABEL "Ordered Orders" SKIP
>               iOrderStatus[3] LABEL "Back Ordered Orders" SKIP
>               iOrderStatus[4] LABEL "Partially Shipped Orders" SKIP
>               iOrderStatus[5] LABEL "Invalid Orders"
>          WITH FRAME DisplayFrame SIDE-LABELS.

> Figure 21–1 shows the result.

> Figure 21–1: Result of CASE statement example

Copyright © 2005 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095