In this topic, we described about the below sections -
EVALUATE provides a multi selection control during the program execution. EVALUATE used to validate multiple conditions in a single shot. EVALUATE statement provides a shorthand notation for a series of nested IF statements.
If any EVALUATE WHEN conditions satisfy, the list of statements provided under the WHEN phrase executed and control transfers to the next executable statement after EVALUATE.
If none of the EVALUATE satisfies, the statements coded under WHEN OTHER gets executed by default and control transfers to the next executable statement after EVALUATE. END-EVALUATE is optional with EVALUATE.
Individually, the operands with EVALUATE statement are called subjects. Collectively, they are called a set of subjects. Individually, operands in WHEN phrase called objects. Collectively, they are called a set of objects.
Separates subjects within a set of subjects. Separates objects within a set of objects.
THROUGH and THRU -
THROUGH and THRU are equivalent. Two operands connected by a THRU phrase must be of the same class. The number of objects must be equal to the number of subjects.
END-EVALUATE phrase -
The END-EVALUATE used to end the scope of the EVALUATE statement. END-EVALUATE is not required when EVALUATE statement WHEN phase ended with period.
EVALUATE Working -
Each subject in the EVALUATE tested by using the corresponding object-value in the WHEN phrase. After the comparison is completed, execution of the EVALUATE statement proceeds as follows -
- If a WHEN phrase is selected, execution continues with the first imperative-statement-1 following the selected WHEN phrase.
- If no WHEN phrase is selected and WHEN OTHER phrase is specified, execution continues with imperative-statement-2.
- If no WHEN phrase is selected and no WHEN OTHER phrase is specified, execution continues with the next executable statement followed the scope delimiter END-EVALUATE.
- The scope of the EVALUATE statement is terminated when execution reaches the end of the selected WHEN phrase or WHEN OTHER phrase or when no WHEN phrase is selected and no WHEN OTHER phrase is specified.
EVALUATE Types -
EVALAUTE can divide logically into below types based on its usage in the program -
- Simple EVALUATE
- EVALUATE TRUE
- EVALAUTE with THRU
- EVALUATE with multiple WHEN conditions
- EVALUATE with MULTIPLE conditions
Simple EVALUATE -
EVALUATE have only one condition to validate. EVALUATE validates the data-item and WHEN phrases have the values to validate the data-item. The statements under the WHEN phrase that matches the data item value gets executed.
Practical Example -
The above simple program is used to receive the current day number in week and displays what is today.
EVALUATE validates the WEEK-DAY with all WHEN conditions which have values.
Based on the number entered, the corresponding WHEN condition will be executed.
EVALUATE TRUE -
EVALUATE have the boolean expression and WHEN phrases have the logical expressions. If EVALUATE specified with TRUE, the statements under WHEN phrase with logical expression is TRUE gets executed. If none of the WHEN logical-expression satisfies, then WHEN OTHER phrase gets executed.
EVALUATE TRUE works in the same way how the simple EVALUATE working but the condition always with WHEN instead of EVALUATE.
EVALUATE with THROUGH/THRU -
EVALUATE THRU used to validate the data item with values range that are contiguous.The values range should be in ascending order. The EVALUATE statement data-item value should fall in the values range specified with WHEN phase to execute the corresponding statements. If none of the values range satisfies, WHEN OTHER phrase gets executed.
EVALUATE using multiple WHEN phrase -
EVALUATE with multiple WHEN phases used to validate the data item with set of values (different values) that are having the same action needs to perform. Multiple WHEN phrases are grouped together and set of statements specified for all WHEN phrases. If any one of the WHEN phrase value matches with the data item value, the corresponding set of statements gets executed.
In the above example, program receives the student grade as input and displays the student class.
Grades A, B, C and D are FIRST CLASS. Grade E is SECOND CLASS. Grades F and G is THIRD CLASS. For all other Grades then student got failed.
So our requirement is for A, B, C and D we need to display as FIRST CLASS.
The EVALUATE can be coded the like below.
EVALUATE STD-CODE WHEN ‘A’ WHEN ‘B’ WHEN ‘C’ WHEN ‘D’ DISPLAY ‘STUDENT GOT FIRST CLASS’
If user enters any of the above grades, then STUDENT GOT FIRST CLASS will be displayed.
EVALUATE with Multiple Conditions -
EVALUATE with multiple conditions used to validate the set of conditions that are combining with ALSO. The number of subjects in the EVALUATE statement should equal to the number of objects with WHEN phrase. If all the logical expressions satisfied for the WHEN phrase, the corresponding set of statements gets executed.