Anda di halaman 1dari 5

Case Structure

The CASE structure is an alternate way to check conditions and direct processing according to the results of the condition. The EVALUATE does just that, it evaluates something, the contents of a field, a literal or a c ondition and then selects from a series of options which action it will take. First, let's examine some examples. EXAMPLE #1:
EVALUATE TYPE-EMPLOYEE WHEN "F" MOVE "FULL TIME" TO EMP-TYPE-PR WHEN "P" MOVE "PART TIME" TO EMP-TYPE-PR WHEN "C" MOVE "CONSULTANT" TO EMP-TYPE-PR WHEN "T" MOVE "TEMPORARY" TO EMP-TYPE-PR WHEN OTHER MOVE "INVALID" TO EMP-TYPE-PR.

In this statement, the field TYPE-EMPLOYEE is evaluated, and depending on the code in the field a move is executed. Only one WHEN will be executed. For example, if the code is equal to P the message PART TIME is moved to EMP-TYPE-PR and the evaluate is complete. Control passes to the next statement. If the code is not equal to either F or P or C or T then the OTHER option is taken. OTHER is a reserved word that means if no other option is taken, do this. If this had been coded using an IF, the statement would read like this:
IF TYPE-EMPLOYEE = "F" MOVE "FULL TIME" TO EMP-TYPE-PR ELSE IF TYPE-EMPLOYEE = "P" MOVE "PART TIME" TO EMP-TYPE-PR ELSE IF TYPE-EMPLOYEE = "C" MOVE "CONSULTANT" TO EMP-TYPE-PR ELSE IF TYPE-EMPLOYEE = "T" MOVE "TEMPORARY" TO EMP-TYPE-PR ELSE MOVE "INVALID" TO EMP-TYPE-PR.

Some programmers code the EVALUATE using this style. Depending on what is being tested and your personal taste either way is acceptable.
EVALUATE TYPE-EMPLOYEE WHEN "F" MOVE WHEN "P" MOVE WHEN "C" MOVE WHEN "T" MOVE "FULL TIME" TO EMP-TYPE-PR "PART TIME" TO EMP-TYPE-PR "CONSULTANT" TO EMP-TYPE-PR "TEMPORARY" TO EMP-TYPE-PR

WHEN OTHER

MOVE "INVALID" TO EMP-TYPE-PR.

EXAMPLE #2:
EVALUATE ON-HAND WHEN 0 PERFORM B-310-NO-INVENTORY WHEN 1 THRU 100 PERFORM B-320-INVENTORY-LOW WHEN 101 THRU 500 PERFORM B-330-INVENTORY-NORMAL WHEN OTHER PERFORM B-340-INVENTORY-HIGH END-EVALUATE.

NOTE: The END-EVALUATE is an optional statement similiar to the END-IF. If it is not there, the EVALUATE will terminate with the period. The THRU statement is used to check for the range of 1 through 100 and the range of 101 thru 500, these numbers are inclusive that is 1 through 100 includes both 1 and 100. Another way to code this example uses the reserved word TRUE. When EVALUATE TRUE is written, it means if the condition in each WHEN statement is TRUE then the command following the condition should be executed. There is also a reserved word FALSE which would check to see if the condition evaluated FALSE. When using EVALUATE, you can have the WHEN clause evaluate a level 88 name as the condition instead of writing out the condition.
EVALUATE TRUE WHEN ON-HAND = 0 PERFORM B-310-NO-INVENTORY WHEN ON-HAND > 0 AND < 101 PERFORM B-320-INVENTORY-LOW WHEN ON-HAND > 101 AND < 501 PERFORM B-330-INVENTORY-NORMAL WHEN OTHER PERFORM B-340-INVENTORY-HIGH END-EVALUATE.

The IF statement for this example would be:


IF ON-HAND = 0 PERFORM B-310-NO-INVENTORY ELSE IF ON-HAND > 0 AND < 101 PERFORM B-320-INVENTORY-LOW ELSE IF ON-HAND > 100 AND < 501 PERFORM B-330-INVENTORY-NORMAL ELSE PERFORM B-340-INVENTORY-HIGH.

EXAMPLE #3: The evaluate can also be used to accomplish the same thing that a simple IF statement would accomplish:
EVALUATE ON-ORDER >= 1000 WHEN TRUE PERFORM B-310-CHECK-ONORDER.

Or a second way of coding the above, would be:


EVALUATE ON-ORDER WHEN 0 THRU 1000 PERFORM B-310-CHECK-ONORDER.

SYNTAX OF simple EVALUATE:


{identifier} {literal } {expression} {TRUE } {FALSE } {condition } {TRUE } WHEN {FALSE } {[NOT]{identifier} {identifier}] {literal }[THRU {literal }] {expression} {expression}]

EVALUATE

imperative statement [WHEN OTHER imperative statement]

[END-EVALUATE]

Remember: { means one possible option [ means this entry is optional

6.6 EVALUATE
If there are a large number of conditional alternatives, then using a large number of nested IF statements can be messy: IF A = 1 THEN PERFORM PARA-1 ELSE IF A = 2 THEN PERFORM PARA-2 ELSE IF A = 3 THEN PERFORM PARA-3 ELSE IF A = 4 THEN PERFORM PARA-4 END-IF END-IF END-IF

END-IF

The above example only tested four possible values for 'A'. Suppose there were ten or twenty? This is where the EVALUATE statement is of great use. The format is:
{ { { { { identifier-1 literal-1 expression-1 TRUE FALSE } } } } } { { { { { identifier-2 literal-2 expression-2 TRUE FALSE } } } } }

EVALUATE

ALSO

WHEN {statement-1}... WHEN OTHER {statement-2}... END-EVALUATE

The best way to understand this is to look at the following examples: Example 1. EVALUATE W-NUM WHEN 1 MOVE 10 TO NEW-DATA DISPLAY 'NEW-DATA IS 10' WHEN 2 MOVE 20 TO NEW-DATA DISPLAY 'NEW-DATA IS 20' WHEN 3 MOVE 30 TO NEW-DATA DISPLAY 'NEW-DATA IS 30' WHEN OTHER MOVE ZERO TO NEW-DATA DISPLAY 'NEW-DATA IS 0' END-EVALUATE Example 2. EVALUATE SCORE WHEN 0 THRU 49 MOVE 'FAIL' TO W-GRADE WHEN 50 THRU 59 MOVE 'C' TO W-GRADE WHEN 60 THRU 69 MOVE 'B' TO W-GRADE WHEN OTHER MOVE 'A' TO W-GRADE END EVALUATE

Example 3. EVALUATE (FUEL-TYPE = 'PETROL') ALSO (ENGINE-SIZE > 1.1) WHEN TRUE ALSO TRUE DISPLAY '20% PETEROL DUTY TO PAY' WHEN TRUE ALSO FALSE DISPLAY '10% PETROL DUTY TO PAY' WHEN FALSE ALSO TRUE DISPLAY '15% DIESEL DUTY TO PAY' WHEN FALSE ALSO FLASE DISPLAY '5% DIESEL DUTY TO PAY' END-EVALUATE Example 1 shows how an item (W-NUM) is compared to a set of possibilities, and when true, any number of statements can be executed. Example 2 shows how a range of values can be studied using the THRU clause. Care should be taken to ensure that these ranges do not overlap. Both of these examples use WHEN OTHER. Again, care should be taken: in example 2, as it is coded, a score of -1 would result in an A-grade being awarded. A better coded solution would include:
: WHEN 70 THRU 100 MOVE 'A' TO W-GRADE WHEN OTHER DISPLAY 'ERROR. SCORE NOT VALID' END EVALUATE

Now, when SCORE is less then zero (or greater than 100) an error message will be displayed.

Conditions can use =, <, >, <=, >=, NOT=, or the words, LESS, GREATER, LESS OR EQUAL, GREATER OR EQUAL, EQUAL, NOT EQUAL. You can also combine conditions using AND, OR and NOT

Anda mungkin juga menyukai