Function
The EVALUATE statement describes a multi-branch, multi-join structure. It can cause multiple conditions to be evaluated. The subsequent action of the program depends on the results of these evaluations.
Format
EVALUATE {identifier-1 | literal-1 | expression-1 | TRUE | FALSE }
[ALSO {identifier-2 | literal-2 | expression-2 | TRUE | FALSE} ]...
| ||||||||
{ | { |
| { |
| ||||
| { |
| ||||||
|
| |||||||
] | ||||||||
} | ||||||||
} | ||||||||
[ |
ALSO
| { |
ANY | condition-2 | subcondition-2
| TRUE
| FALSE | |||||
| [
NOT
]
|
{
|
{identifier-5 | literal-5 | arithm-expression-3} | ||||||
|
| |||||||
] | ||||||||
} | ||||||||
} | ||||||||
]...
| ||||||||
}... | ||||||||
imperative-statement-1
| ||||||||
} ... |
[WHEN OTHER imperative-statement-2]
[END-EVALUATE]
Syntax rules
The operands or the words TRUE and FALSE which appear before the first WHEN phrase of the EVALUATE statement are referred to individually as selection subjects and collectively, for all those specified, as the set of selection subjects.
The operands or the words TRUE, FALSE and ANY which appear in a WHEN phrase of an EVALUATE statement are referred to individually as selection objects and collectively, for all those specified in a single WHEN phrase, as the set of selection objects.
The words THROUGH and THRU are equivalent.
Two operands connected by a THROUGH phrase must be of the same class. The two operands thus connected constitute a single selection object. The connected operands may not be of the class “object” or the class “pointer”.
The number of selection objects within each set of selection objects must be equal to the number of selection subjects.
Each selection object within a set of selection objects must correspond to the selection subject having the same ordinal position within the set of selection subjects according to the following rules:
Identifiers, literals, or arithmetic expressions appearing within a selection object must be valid operands (according to the rules for relation conditions) for
comparison with the corresponding operand in the set of selection subjects.condition-1, condition-2, or the words TRUE or FALSE appearing as a selection object must correspond to a conditional expression or the words TRUE or FALSE
in the set of selection subjects.The word ANY may correspond to a selection subject of any type.
subcondition-1 or subcondition-2 may not start with an arithmetic operator.
subcondition-1 or subcondition-2 appearing within a selection object must correspond to an identifier, a literal or an arithmetic expression in the set of selection subjects. They must be specified in such a way that a valid, simple condition can be created by inserting the corresponding subject in front of the subcondition.
General rules
The execution of the EVALUATE statement operates as if each selection subject and selection object were evaluated and assigned a numeric or non-numeric value, a range of numeric or non-numeric values, or a truth value. These values are determined as follows:
Any selection subject specified by identifier-1, identifier-2, and any selection object specified by identifier-3, identifier-5, without either the NOT or the THROUGH phrase, are assigned the value and class of the data item referenced by the identifier.
Any selection subject specified by literal-1, literal-2, and any selection object specified by literal-3, literal-5, without either the NOT or the THROUGH phrase, are assigned the value and class of the specified literal. If literal-3, literal-5, is the figurative constant ZERO, it is assigned the class of the corresponding selection subject.
Any selection subject in which expression-1, expression-2, is specified as an arithmetic expression and any selection object, without either the NOT or the THROUGH phrase, in which arithmetic-expression-1, arithmetic-expression-3, is specified are assigned a numeric value according to the rules for evaluating an arithmetic expression.
Any selection subject in which expression-1, expression-2, is specified as a conditional expression and any selection object in which condition-1, condition-2, is specified are assigned a truth value according to the rules for evaluating conditional expressions.
Any selection subject or any selection object specified by the words TRUE or FALSE is assigned a truth value. The truth value "true" is assigned to those items specified with the word TRUE, and the truth value "false" is assigned to those items specified with the word FALSE.
Any selection specified by the word ANY is not further evaluated.
If the THROUGH phrase is specified for a selection object, without the NOT phrase, the range of values includes all permissible values of the selection 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 a selection object, the values assigned to that item are all permissible values of the selection subject not equal to the value, or not included in the range of values, that would have been assigned to the item had the NOT phrase not been specified.
If a subcondition is specified for a selection object, it is assigned the truth value of the condition resulting from the addition of the object to the result of the evaluation of the related subject.
The execution of the EVALUATE statement then proceeds as if the values assigned to the selection subjects and selection objects were compared to determine if any WHEN phrase satisfies the set of selection subjects. This comparison proceeds as follows:
Each selection object within the set of selection objects for the first WHEN phrase is compared with the selection subject having the same ordinal position within the set of selection subjects. One of the following conditions must be satisfied if the comparison is to be satisfied:
If the items being compared are assigned numeric or non-numeric values, or a range of numeric or non-numeric values, the comparison is satisfied if the value, or one of the range of values, assigned to the selection object is equal to the value assigned to the selection subject according to the rules for comparison (see section "Relation condition").
If the object is a subcondition, the comparison is satisfied if the object was assigned the truth value "true".
If the items being compared are assigned truth values, the comparison is satisfied if the items are assigned the identical truth value.
If the selection object being compared is specified by the word ANY, the comparison is always satisfied regardless of the value of the selection subject.
If the above comparison is satisfied for every selection object within the set of selection objects being compared, the WHEN phrase containing that set of selection objects is selected as the one satisfying the set of selection subjects.
If the above comparison is not satisfied for one or more selection objects within the set of selection objects being compared, that set of selection objects does not satisfy the set of selection subjects.
This procedure is repeated for subsequent sets of selection objects, in the order of their appearance in the compilation unit, until either a WHEN phrase satisfying the set of selection subjects is selected or until all sets of selection objects are exhausted.
After the comparison operation is completed, execution of the EVALUATE statement proceeds as follows:
If a WHEN phrase is selected, execution continues with the first imperative-statement following the selected WHEN phrase. There is a branch to END-EVALUATE after the last imperative-statement is executed.
If no WHEN phrase is selected and a WHEN OTHER phrase is specified, execution continues with the imperative-statement of the WHEN OTHER phrase.
An EVALUATE statement is terminated when one of the following conditions is satisfied:
imperative-statement-1 of the selected WHEN phrase is executed,
imperative-statement-2 is executed,
no WHEN phrase is selected and no WHEN OTHER phrase is specified.
Example 8-36
IDENTIFICATION DIVISION. PROGRAM-ID. EVAL1. ENVIRONMENT DIVISION. CONFIGURATION SECTION. SPECIAL-NAMES. TERMINAL IS T. DATA DIVISION. WORKING-STORAGE SECTION. 77 A PIC 9. 77 B PIC 9. 77 C PIC 9. 77 D PIC 9. PROCEDURE DIVISION. PROC SECTION. DIALOG. DISPLAY "Enter value for A" UPON T. ACCEPT A FROM T. DISPLAY "Enter value for B" UPON T. ACCEPT B FROM T. DISPLAY "Enter value for C" UPON T. ACCEPT C FROM T. DISPLAY "Enter value for D" UPON T. ACCEPT D FROM T. TEST1. EVALUATE A + B ALSO C + D WHEN 5 ALSO 5 DISPLAY "Values correct" UPON T WHEN OTHER DISPLAY "Values incorrect" UPON T END-EVALUATE. FINISH-PAR. STOP RUN.
Example 8-37
IDENTIFICATION DIVISION. PROGRAM-ID. BSP. ENVIRONMENT DIVISION. CONFIGURATION SECTION. SPECIAL-NAMES. TERMINAL IS T. DATA DIVISION. WORKING-STORAGE SECTION. 01 TYPE-OF-ORDER PIC 9. 88 ON-SITE VALUE 1. 88 IN-WRITING VALUE 2 THRU 4. 01 CUSTOMER-TYPE PIC X. 88 PRIVATE VALUE "1". 88 BUSINESS VALUE "2". 01 WEIGHT PIC 9999. 01 SHIPPING-MODE PIC 9. 88 PICK-UP VALUE 1. 88 MAIL VALUE 2. 88 RAIL VALUE 3. 88 UPS VALUE 4. PROCEDURE DIVISION. PROC SECTION. DIALOG. DISPLAY "Enter type-of-order" UPON T. DISPLAY " on-site = 1, in-writing = 2-4 " UPON T. ACCEPT TYPE-OF-ORDER FROM T. DISPLAY "Customer-type" UPON T. DISPLAY "Business = 2 , Private = 1 " UPON T. ACCEPT CUSTOMER-TYPE FROM T. DISPLAY "Enter weight" UPON T. ACCEPT WEIGHT FROM T. DETERMINATION-SHIPPING-MODE. EVALUATE TRUE ALSO TRUE ALSO TRUE WHEN PRIVATE ALSO ON-SITE ALSO ANY WHEN BUSINESS ALSO ON-SITE ALSO ANY SET PICK-UP TO TRUE WHEN PRIVATE ALSO IN-WRITING ALSO WEIGHT < 5 SET MAIL TO TRUE WHEN BUSINESS ALSO IN-WRITING ALSO WEIGHT < 10 SET UPS TO TRUE WHEN OTHER SET RAIL TO TRUE END-EVALUATE. OUTPUT-PAR. DISPLAY "Shipping-mode = " SHIPPING-MODE UPON T. STOP RUN.
Explanation
The selection objects ON-SITE, IN-WRITING, PRIVATE, BUSINESS (condition-names) are evaluated with respect to their truth value. The selection subjects are represented by the three TRUEs; all three selection subjects (= set of selection subjects) have the truth value "true". A set of selection objects satisfies the condition of the set of selection subjects when all selection objects in the set (except those specified by ANY) are assigned the truth value "true" (a WHEN phrase corresponds to a set of selection objects). The statement which follows this WHEN phrase will then be executed. If none of the specified sets of selection objects satisfies the comparison, the statement of the WHEN OTHER phrase is executed. The ANY phrase must be used in the first two WHEN phrases, because only two condition-names are tested for their truth value in these WHEN phrases, whereas three condition-names are tested for their truth value in the other WHEN phrases, and the number of selection objects within the set of selection objects must correspond to the number of selection subjects.