The main difference between the EVALUATE and VBA SELECT CASE is that EVALUATE does not require a “break” statement because control automatically exits the EVALUATE statement as soon as a condition is met.
Formats:
Before we look at the syntax and the example of EVALUATE statement, we would like to mention that the COBOL EVALUATE statement has four different formats. Let us discuss each format with each syntax.
The basic syntax of the EVALUATE statement is:
WHEN Condition-1
Statement…
WHEN OTHER
Statement…
EMO-EVALUATE.
The “WHEN” clause is generally used to specify your condition. In the following examples, we specify the statement that needs to be executed for that particular event. The evaluation of the statement ends with a clear explanation of the end of its scope.
Programming Example 1:
PROGRAM-ID. EMP001.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 WS-FILE-SW PIC X(01) VALUE 'N'.
88 E-O-F VALUE 'Y'.
88 NOT-E-O-F VALUE 'N'.
PROCEDURE DIVISION.
000-MAIN-PARA.
EVALUATE TRUE
WHEN E-O-F
PERFORM A00-WRT-TRL-REC
WHEN NOT-E-O-F
PERFORM B00-WRT-DTL-REC
PERFORM B10-REC-NXT-REC
WHEN OTHER
PERFORM Z00-CLL-ERR
END-EVALUATE.
STOP RUN.
A00-WRT-TRL-REC.
DISPLAY 'END OF FILE'.
B00-WRT-DTL-REC.
DISPLAY 'NOT END OF FILE'.
B10-REC-NXT-REC.
DISPLAY 'NOT END OF FILE'.
Z00-CLL-ERR.
DISPLAY 'ERROR'.
Output:
NOT END OF FILE
NOT END OF FILE
Explanation:
In this programming example, we look at the paragraph that is mentioned in the procedure division which is 000-MAIN-PARA. We use the EVALUATE statement in this paragraph. It says “evaluate True” at the start of the EVALUATE statement and it says when.
The first condition states that if there is no record in the file, it means that it is the end of the file. In that case, just go and write the PERFORM A00-WRT-TRL-REC. If it is not the end of the file and there are few more records, just go ahead and write the B00-WRT-DTL-REC record which is already in the buffer, and go to the next record which is B10-REC-NXT-REC.
In case none of the provided condition is satisfied, use control by going to the other section. It then executes Z00-CLL-ERR. It is called “error” which goes to the exception handling.
Programming Example 2: EVALUATE WHEN with ALSO
PROGRAM-ID. EMP002.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 WS-FILE-SW PIC X(01) VALUE 'N'.
88 END-OF-THE-FILE VALUE 'Y'.
88 NOT-END-OF-THE-FILE VALUE 'N'.
01 WS-P PIC 9(3).
01 WS-Q PIC 9(3).
01 WS-R PIC 9(3).
01 WS-S PIC 9(3).
PROCEDURE DIVISION.
000-MAIN-PARA.
DISPLAY "ENTER VALUE OF WS-P".
ACCEPT WS-P.
DISPLAY "ENTER VALUE OF WS-Q".
ACCEPT WS-Q.
DISPLAY "ENTER VALUE OF WS-R".
ACCEPT OF WS-R.
DISPLAY "ENTER VALUE OF WS-S".
ACCEPT OF WS-S.
EVALUATE TRUE ALSO TRUE
WHEN WS-P = WS-Q ALSO WS-R = WS-S
DISPLAY 'CASE 1 IS TRUE'
WHEN WS-P > WS-Q ALSO WS-R < WS-S
DISPLAY 'CASE 2 IS TRUE'
WHEN OTHER
DISPLAY 'OTHER CASE IS TRUE'
END-EVALUATE.
Output:
ENTER VALUE OF WS-P
300
ENTER VALUE OF WS-Q
100
ENTER VALUE OF WS-R
169
ENTER VALUE OF WS-S
500
CASE 2 IS TRUE
Explanation:
Let’s move on to the next variant of the EVALUATE statement which is “EVALUATE WITH ALSO”. This variant of the EVALUATE statement is similar to what we discussed in the previous example. The only difference is that in this variant, we check more than one condition at the same time.
In this example, the EVALUATE TRUE ALSO TRUE means that if both condition that is specified in the WHEN clause is satisfied, only the statement could be executed. The values of “WS-P” and “WS-Q” should match, and the values of “WS-R” and “WS-S” should also match in this statement. Then, only the “true” statement that is in the case is displayed as an output of the particular statement.
Programming Example 3: Evaluate WHEN with Multiple Conditions
PROGRAM-ID. EMP003.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 MARTIAL-STATUS PIC X (01) VALUE 'M'.
01 WS-PGE PIC X (02) VALUE 36.
01 WS-GEN PIC X (01) VALUE 'F'.
PROCEDURE DIVISION.
000-MAIN-PARA.
EVALUATE MARTIAL-STATUS
WHEN 'M'
DISPLAY 'MARRIED'
WHEN 'S'
WHEN 'D'
DISPLAY 'SINGLE'
WHEN OTHER
DISPLAY 'NOT SPECIFIED'
END-EVALUATE.
EVALUATE TRUE
WHEN WS-PGE > 10 AND WS-GEN = 'M'
DISPLAY 'MALE WITH AGE > 10'
WHEN WS-PGE > 10 AND WS-GEN = 'F'
DISPLAY 'FEMALE WITH AGE > 10'
WHEN OTHER
DISPLAY 'CRITERIA NOT SATISFY'
END-EVALUATE.
Output:
MARRIED
FEMALE WITH AGE > 10
Explanation:
In this example, despite of using “true”, we use the actual variable name and check the values of those variables, the possible value of that particular variable in the WHEN clause.
In this example, there is a field called “MARTIAL STATUS”. We check the value of this particular field, whether they are Married, Single, or Divorced. If the person is married, it displays “Married”. If the person is single, it displays “Single”.
Programming Example 4: Evaluate WHEN with THRU
PROGRAM-ID. EMP004.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 WS-FILE-SW PIC X(01) VALUE 'N'.
88 END-OF-THE-FILE VALUE 'Y'.
88 NOT-END-OF-THE-FILE VALUE 'N'.
01 WS-NUMBER PIC 9(2) VALUE ZERO.
PROCEDURE DIVISION.
000-MAIN-PARA.
DISPLAY "Enter a number : "
ENTER VALUE OF WS-NUMBER
EVALUATE WS-NUMBER
WHEN 1
DISPLAY '1'
WHEN 2
DISPLAY '2'
WHEN 3 THRU 6
DISPLAY 'NUMBER IN RANGE OF 3-6'
WHEN OTHER
DISPLAY 'NUMBER NOT IN RANGE'
END-EVALUATE.
Output:
Enter a number:
4
NUMBER IN RANGE OF 3-6
Explanation:
This variant of the EVALUATE statement is also similar to the previous examples. The code uses the “THRU” keyword when specifying the conditions within the WHEN clause of the EVALUATE statement to indicate a range of values for the variable that is being evaluated. In this case, we have the EVALUATE statement and we use a variable called “WS-NUMBER”. What we do is we check the different values in the WHEN clause.
The first one is that the WS-NUMBER value is 1. So, we display “1”. Similarly, if the value is two, the value “2” is displayed, etc. Despite of writing multiple times for a single value, what we do is check for a value between 3 to 6, and the output value is in the range of 3 to 6.
Conclusion
In this particular article, we covered up all the prospects and the application of the EVALUATE statement so that we understand the mechanism of the EVALUATE statement in a well manner. We clarified the whole topic of the EVALUATE statements that are present in the COBOL programming language.