EVALUATE statement is used for conditional processing which helps in eliminating a series of nested IF statements to test several conditions. The EVALUATE statement is very similar to the CASE construct common in many other programming languages. If none of the EVALUATE satisfies, by default the statements coded under WHEN OTHER will be executed and control transfers to the next executable statement after ending of EVALUATE.
EVALUATE Syntax
EVALUATE {subject} [ ALSO {subject} ] ... {TRUE } {TRUE } {FALSE } {FALSE } { { WHEN obj-phrase [ ALSO obj-phrase ] ... } ... statement-1 } ... [ WHEN OTHER statement-2 ] [ END-EVALUATE ] Subject has the following format { IDENTIFIER } { LITERAL } { COND EXPRESSION } { ARITH EXPRESSION } Obj-phrase has the following format { ANY } { TRUE } { FALSE } { cond-obj } { [NOT] obj-item [ {THRU } obj-item ]} { {THROUGH} }
General Rules
- The EVALUATE statement operates as if each subject and object were evaluated and assigned a value or range of values. These values may be numeric, nonnumeric, truth values, or ranges of numeric or nonnumeric values. These values are determined as follows:
- Any subject or object that is a data item or literal, without either the THROUGH or the NOT phrase, is assigned the value and class of that data item or literal.
- Any subject or object that is an arithmetic expression, without either the THROUGH or the NOT phrase, is assigned a numeric value according to the rules for evaluating arithmetic expressions.
- Any subject or object that is a conditional expression is assigned a truth value according to the rules for evaluating conditional expressions.
- Any subject or object specified by the words TRUE or FALSE is assigned a truth value corresponding to that word.
- Any object specified by the word ANY is not evaluated.
- If the THROUGH phrase is specified for an object, without the NOT phrase, the range of values includes all permissible values of the corresponding subject that are greater than or equal to the first operand and less than or equal to the second operand, according to the rules for comparison.
- If the NOT phrase is specified for an object, the values assigned to that object are all permissible values of the corresponding subject not equal to the value, or range of values, that would have been assigned had the NOT phrase been omitted.
- The EVALUATE statement then proceeds as if the values assigned to the subjects and objects were compared to determine if any WHEN phrase satisfies the subject set. Each object within the object set for the first WHEN phrase is compared to the subject having the same ordinal position within the subject set. The comparison is satisfied if one of the following is true:
- If the items being compared are assigned numeric or nonnumeric values, the comparison is satisfied if the value (or one of the range of values) assigned to the object is equal to the value assigned to the subject.
- If the items being compared are assigned truth values, the comparison is satisfied if the truth values are the same.
- If the object is the word ANY, the comparison is always satisfied.
- If the comparison is satisfied for every object within the object set, the corresponding WHEN phrase is selected.
- If the comparison is not satisfied for one or more objects within the object set, the procedure repeats for the next WHEN phrase. This is repeated until a WHEN phrase is selected or all the object sets have been tested.
- If a WHEN phrase is selected, the corresponding statement-1 is executed.
- If no WHEN phrase is selected and a WHEN OTHER phrase is specified, statement-2 is executed. If no WHEN OTHER phrase is present, control transfers to the end of the EVALUATE statement.
- The scope of execution of the EVALUATE statement is terminated when the end of statement-1 or statement-2 is reached, or when no WHEN phrase is selected and no WHEN OTHER phrase is specified.
Types of EVALUATE:
EVALAUTE can be divided into below types based on its usage in the program.
- Simple EVALUATE
- EVALUATE TRUE
- EVALUATE with THRU
- EVALUATE with multiple WHEN conditions
- EVALUATE with MULTIPLE conditions
Example 1: Simple EVALUATE EVALUATE WS-RENT-APTNO WHEN 1 PERFORM 100-RENT-REPORT WHEN 2 PERFORM 200-RENT-REPORT WHEN OTHER PERFORM 999-ERROR-REPORT END-EVALUATE.
Example 2: EVALUATE TRUE EVALUATE TRUE WHEN WS-RENT-APTNO = 1 AND WS-RENT-BUILDING = A PERFORM 100-RENT-REPORT WHEN WS-RENT-APTNO = 2 AND WS-RENT-BUILDING = A PERFORM 200-RENT-REPORT WHEN OTHER PERFORM 999-ERROR-REPORT END-EVALUATE.
Example 3: EVALUATE with THRU EVALUATE WS-RENT-APTNO WHEN 1 THRU 99 PERFORM 100-RENT-REPORT WHEN 101 THRU 999 PERFORM 200-RENT-REPORT WHEN OTHER PERFORM 999-ERROR-REPORT END-EVALUATE.
Example 4: EVALUATE with multiple WHEN conditions EVALUATE WS-RENT-APTNO WHEN 1 WHEN 2 WHEN 3 PERFORM 100-RENT-REPORT WHEN 4 WHEN 5 WHEN 6 PERFORM 200-RENT-REPORT WHEN OTHER PERFORM 999-ERROR-REPORT END-EVALUATE.
Example 5: EVALUATE with MULTIPLE conditions EVALUATE WS-RENT-APTNO ALSO TRUE WHEN 1 THRU 99 ALSO WS-RENT-BUILDING = "A" PERFORM 100-RENT-REPORT WHEN 101 THRU 999 ALSO WS-RENT-BUILDING = "A" PERFORM 200-RENT-REPORT WHEN OTHER PERFORM 999-ERROR-REPORT END-EVALUATE.
Example 6: EVALUATE with MULTIPLE conditions EVALUATE TRUE ALSO TRUE WHEN WS-RENT-APTNO <= 99 ALSO WS-RENT-BUILDING = "A" PERFORM 100-RENT-REPORT WHEN WS-RENT-APTNO > 99 ALSO WS-RENT-BUILDING = "A" PERFORM 200-RENT-REPORT WHEN OTHER PERFORM 999-ERROR-REPORT END-EVALUATE.
COBOL Blog: Click Here IBM Reference: Click Here