|
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 } { identifier-2 }
{ literal-1 } { literal-2 }
EVALUATE { expression-1 } ALSO { expression-2 }
{ TRUE } { TRUE }
{ FALSE } { FALSE }
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.
|