EVALUATE

This is a Mainframe COBOL forum - you can post your queries on Mainframe COBOL, VS COBOL II, COBOL/370 , Enterprise COBOL

Moderators: dbzTHEdinosauer, Moderator Group

Post Reply
Sandrit
Member
Posts: 6
Joined: Fri Sep 11, 2015 11:06 pm

EVALUATE

Post by Sandrit » Sun Sep 20, 2015 5:55 am

Code: Select all

*READ THE N36-RECORD.

      MOVE N23-KEY-ID		TO N5-KEY1-ID.     
      M0VE 10	 	TO R2-READ.
      PERFORM K100-READ.

*UPDATE THE N-36-RECORD.
       IF N2-STOP = 'Y'
              MOVE 'A'                             	TO N36-STATE
              GO TO C100
        END-IF.

        IF N38-HOLD  = 'Y'
              MOVE 'B'                 	TO N36-STATE
              IF N36-AMOUNT > N38-AMOUNT
                 MOVE 'A'              	TO N36-STATE
              ELSE
                 IF N38-DONE = 'Y'
                    MOVE 'C'           	TO N36-STATE
                 END-IF
              END-IF
           END-IF.
C100.
           MOVE 5                      	TO N2-REWRITE
           PERFORM H000-REWRITE.
       
          EXIT.
Hello,

I am trying to learn COBOL by myself. Can someone please explain to me how you can write this code using EVALUAE and without using GO TO. Thanks

William Collins
Active Member
Posts: 732
Joined: Thu May 24, 2012 4:07 am

Post by William Collins » Sun Sep 20, 2015 1:39 pm

Code: Select all

*READ THE N36-RECORD.

    MOVE N23-KEY-ID            TO N5-KEY1-ID.     
    M0VE 10                    TO R2-READ.
    PERFORM                    K100-READ.

*UPDATE THE N-36-RECORD.
    IF N2-STOP = 'Y'
        MOVE 'A'               TO N36-STATE
        GO TO C100
    END-IF.

    IF N38-HOLD  = 'Y'
        MOVE 'B'               TO N36-STATE
        IF N36-AMOUNT > N38-AMOUNT
            MOVE 'A'           TO N36-STATE
        ELSE
            IF N38-DONE = 'Y'
                MOVE 'C'       TO N36-STATE
            END-IF
        END-IF
    END-IF.
C100.
    MOVE 5                     TO N2-REWRITE
    PERFORM                    H000-REWRITE.
       
    EXIT.
Then you remove all full-stops/periods on lines of code, and end a paragraph/SECTION/program with a single full-stop/period in column 12 of an otherwise blank line.

If you are teaching yourself COBOL, be aware to avoid using GO TO like the utter, utter, plague. You may find this difficult at first, so don't stress about it. Just each time consider how to do it, more clearly, without the GO TO. It'll come. Or not.

EVALUATE is equivalent to a "nested-IF". You have a nested IF, but there is also an IF with a GO TO prior to that, which could have been included in the nested-IF, but wasn't. That can go into the EVALUATE as well:

Code: Select all

*UPDATE THE N-36-RECORD.
    EVALUATE TRUE 
        WHEN N2-STOP = 'Y'
            MOVE 'A'           TO N36-STATE
        WHEN N38-HOLD  = 'Y'
            MOVE 'B'           TO N36-STATE
        WHEN N36-AMOUNT > N38-AMOUNT
            MOVE 'A'           TO N36-STATE
        WHEN N38-DONE = 'Y'
            MOVE 'C'           TO N36-STATE
    END-EVALUATE
You have a lot of literals there. For someone to understand the program (say they're looking for a bug, or evaluating a change) then they have to know what Y (on different fields), A, B, and C mean.

If you used condition-names, COBOL's level-88, things become clearer to the reader:

Code: Select all

    EVALUATE TRUE 
        WHEN N2-STOPPED
            SET N36-STATE-ABANDONED 
                               TO  TRUE
        WHEN N38-ON-HOLD
            SET N36-STATE-BLOKED
                               TO  TRUE
        WHEN N36-AMOUNT > N38-AMOUNT
            SET N36-STATE-ABANDONED 
                               TO  TRUE
        WHEN N38-COMPLETE
            SET N36-STATE-COMPLETED 
                               TO  TRUE
    END-EVALUATE
The entire fragment is now like this, shorter and much clearer:

Code: Select all

*READ THE N36-RECORD.

    MOVE N23-KEY-ID            TO N5-KEY1-ID      
    M0VE 10                    TO R2-READ 
    PERFORM                    K100-READ 

    EVALUATE TRUE 
        WHEN N2-STOPPED
            SET N36-STATE-ABANDONED 
                               TO  TRUE
        WHEN N38-ON-HOLD
            SET N36-STATE-BLOKED
                               TO  TRUE
        WHEN N36-AMOUNT > N38-AMOUNT
            SET N36-STATE-ABANDONED 
                               TO  TRUE
        WHEN N38-COMPLETE
            SET N36-STATE-COMPLETED 
                               TO  TRUE
    END-EVALUATE

    MOVE 5                     TO N2-REWRITE
    PERFORM                    H000-REWRITE 
    .
As well as being more literals, the 10 for read and five for rewrite seem unnecessary if you are going to PERFORM paragraphs of those names.

Sandrit
Member
Posts: 6
Joined: Fri Sep 11, 2015 11:06 pm

Post by Sandrit » Sun Sep 20, 2015 5:42 pm

I could not understand how to use EVALUATE. But now after having seen your answer and explanations, I see it more clearly.
Your code is clean and easy to read, you make it look so easy.
Thanks so much for your help.

Post Reply

FREE TUTORIALS

Tutorials
Free tutorials from mainframegurukul
  • JCL Tutorial
    Covers all important JCL concepts.
  • Cobol Tutorial
    This tutorials covers all Cobol Topics from STRING to COMP-3.
  • DB2 Tutorial
    DB2 Tutorial focuses on DB2 COBOL Programming.
  • SORT Tutorial
    This Tutorial covers all important aspects of DFSORT with examples
  • CICS Tutorial
    This CICS tutorial covers CICS concepts and CICS Basics, CICS COBOL Programming.
Interview
Mainframe Interview questions



Other References
Mainframe Tools and others