Friday, September 4, 2009

The EVALUATE Statement
Syntax
Format
EVALUATE { identifier-1
literal-1
arith-expr-1
condition-1
TRUE
FALSE }[ ALSO { identifier-2
literal-2
arith-expr-2
condition-2
TRUE
FALSE }] ...
{{WHEN { ANY
condition-3
TRUE
FALSE }
[NOT] {{ identifier-3
literal-3
arith-expr-3 }[ { THROUGH
THRU } { identifier-4
literal-4
arith-expr-4 }]}
[ALSO { ANY
condition-4
TRUE
FALSE } ] ...}...
[NOT] {{ identifier-5
literal-5
arith-expr-5 }[ { THROUGH
THRU } { identifier-6
literal-6
arith-expr-6 }]}
imperative-statement-1}...
[WHEN OTHER imperative-statement-2]
[END-EVALUATE]
Description
The EVALUATE statement is used to test a variable for multiple different values. As such, it is an implementation of the standard case construct. The ALSO clause is used to test for values of additional variables as the same time. To execute the same code for multiple conditions, code all the applicable conditions on multiple WHEN clauses. When the statement is executed, the variable after EVALUATE is compared to the each of the values in the WHEN clauses in turn. When a match is found the statement(s) following the WHEN clause and before the next WHEN clause, are executed and control is then transferred to the statement following the END-EVALUATE. If no matching WHEN clause is found, the code following the WHEN OTHER statement is executed. If the ALSO clause is used, then the value following the ALSO in the EVALUATE must match the value in the ALSO associated with the WHEN clause. For example:
EVALUATE FILE-STATUS
WHEN "00"
PERFORM SUCCESSFUL-ROUTINE
WHEN "10"
PERFORM END-OF-FILE-ROUTINE
WHEN OTHER
PERFORM IO-ERROR-ROUTINE
END-EVALUATE.
In this example, if the file status has a value of "00", SUCCESSFUL-ROUTINE will be performed, but if the file status has a value of "10", the END-OF-FILE-ROUTINE will be executed. If neither value is in file status, then IO-ERROR-ROUTINE will be executed. Here is the same test using condition names instead of relational tests:
EVALUATE TRUE
WHEN I-O-OK
PERFORM SUCCESSFUL-ROUTINE
WHEN END-OF-FILE
PERFORM END-OF-FILE-ROUTINE
WHEN OTHER
PERFORM IO-ERROR-ROUTINE
END-EVALUATE.
Tips
1. Use multiple WHEN clauses to implement code that works like an OR.
2. Use the ALSO clause to implement code that works like an AND.
3. Use ANY if the value of one of the variables is not needed in one of the conditions.