Sunday, 28 April 2019

Evaluate | Different Forms Of Evaluate Statement | Evaluate In COBOL.

In today's tutorial, we will precisely explain the COBOL EVALUATE statement with the help of a different example. Also, general thumb rules that should be kept in mind while using COBOL Evaluate statements will be mention at the end of the tutorial. So, let's start with the tutorial without wasting time anymore.

Evaluate In COBOL.
Different Forms Of Evaluate Statement


Different Forms Of Evaluate Statement 

Introduction.

EVALUATE in COBOL is similar to Microsoft Visual Basic "Select Case Statement". Nested evaluate in COBOL is primarily used to replace nested Ifs. The key difference between EVALUATE and VBA Select Case is that no ‘Break’ is required i.e. control automatically transpire of the EVALUATE statement as soon as condition matched. 

COBOL EVALUATE clause is better than nested ifs in terms of performance it saves a lot of CPU and simplifies the program logic. It is not possible to replace all nested-Ifs in the program but you should try to replace as much as you can.

Let me write down the syntax of COBOL EVALUATE statement.


* The syntax of the Evaluate statement as an If-statement alternative. 

EVALUATE TRUE
    WHEN Condition-1
         Statement ... 
    WHEN OTHER 
         Statement- ... 
END-EVALUATE. 


During the execution of an EVALUATE statement, the values denoted by the list of subjects (items in the EVALUATE statement) are compared with the values denoted by the list of objects in a WHEN phrase to establish a “match” between the two.

The value of a subject is compared with the value/range of values of the object in the corresponding ordinal position In the case of a single-valued (numeric/non-numeric) object, the subject-object comparison is done in the usual way.

When a range of values is specified for the object, the subject-object comparison results in TRUE, if the value of the subject falls within the range In the case of conditional values, the subject-object comparison results in TRUE, if both evaluate to the same value (that is if both are TRUE or both are FALSE).


FORMAT 1: EVALUATE .... WHEN with TRUE.


EVALUATE TRUE 
   WHEN END-OF-THE-FILE 
        PERFORM A00-WRT-TRL-REC 
   WHEN NOT-END-OF-THE-FILE 
        PERFORM B00-WRT-DTL-REC 
        PERFORM B10-RED-NXT-REC 
   WHEN OTHER 
        PERFORM Z00-CLL-ERR End-EVALUATE 
END-EVALUATE.


FORMAT 2: EVALUATE .... WHEN with ALSO.

EVALUATE TRUE ALSO TRUE
   WHEN WS-A = WS-B ALSO WS-C = WS-D
        DISPLAY 'CASE 1 is True' 
   WHEN WS-A > WS-B ALSO WS-C < WS-D 
        DISPLAY 'CASE 2 is True'  
   WHEN OTHER 
        DISPLAY 'OTHER CASE is True' 
END-EVALUATE.


FORMAT 3: EVALUATE .... WHEN with multiple conditions i.e. AND and OR operator.

EVALUATE MARTIAL-STATUS 
   WHEN 'M'
        DISPLAY 'Married' 
   WHEN 'S'
   WHEN 'D'
   WHEN 'W'  
        DISPLAY 'Single'  
   WHEN OTHER 
        DISPLAY 'Not Specified' 
END-EVALUATE.



EVALUATE TRUE 
   WHEN WS-AGE > 10 AND WS-GEN = 'M'
        DISPLAY 'MALE with Age > 10' 
   WHEN WS-AGE > 10 AND WS-GEN = 'F' 
        DISPLAY 'Female with Age > 10'  
   WHEN (WS-AGE > 10 OR WS-AGE < 15) AND WS-GEN = 'F' 
        DISPLAY 'Female with Age between 10 - 15'  
   WHEN OTHER 
        DISPLAY 'Criteria not satisfy' 
END-EVALUATE.

Format 4: EVALUATE .... WHEN with THRU.

EVALUATE WS-NUMBER
    WHEN 1
         DISPLAY '1'
    WHEN 2 
         DISPLAY '2' 
    WHEN 3 THRU 6
         DISPLAY 'Number between 3 - 6'
    WHEN OTHER 
         DISPLAY 'NUMBER not in range' 
END-EVALUATE.

If ANY is specified for an object, the subject-object comparison always results in TRUE 
The list of subjects is said to “match” with the list of object, if all the corresponding subject – object comparisons result in true 

Note: that the values of the subjects need not be of the same class. For example one can be numeric and the other can be alpha numeric After the execution of one of the when clauses, the control is automatically passed on to the next sentence after the EVALUATE statement. There is no need of any extra code.

1 comment:

  1. Nice article, infact author has explain the different form Evaluate precisely.

    Thanks.

    ReplyDelete