Cobol and Mainframe

EVALUATE Statement in COBOL

EVALUTE in COBOL is similar to the “Select Case statement” in Microsoft Visual Basic. The “EVALUATE” statement was introduced with COBOL-85 and can use the COBOL EVALUATE for a variety of purposes. From the performance perspective, the COBOL EVALUATE statement is better than COBOL nested ifs. Due to this reason, it is only recommended that you should replace your nested-if statement with COBOL EVALUATE statement.

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:

EVALUATE TRUE
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:

IDENTIFICATION DIVISION.
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:

papan@papan-VirtualBox:~/Desktop/eva$ cobc -xjF 4.cbl
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

IDENTIFICATION DIVISION.
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:

papan@papan-VirtualBox:~/Desktop/eva$ cobc -xjF 2.cbl
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

IDENTIFICATION DIVISION.
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:

papan@papan-VirtualBox:~/Desktop/eva$ cobc -xjF 1.cbl
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

IDENTIFICATION DIVISION.
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:

papan@papan-VirtualBox:~/Desktop/eva$ cobc -xjF 3.cbl
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.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com