Page 1 of 1

EVALUATE

Posted: Sun Sep 20, 2015 5:55 am
by Sandrit

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

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

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.

Posted: Sun Sep 20, 2015 5:42 pm
by Sandrit
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.