Make your own free website on Tripod.com


USING THE EVALUATE VERB IN COBOL 85

The EVALUATE verb has the following syntax:

EVALUATE{identifier-1 or expression-1}
    WHEN condition-1 imperative-statement1..
    [WHEN OTHER imperative-statement-2]
[END-EVALUATE]

You should become familiar with seeing the syntax of a reserved word. I will give several examples for the three common usage's of EVALUATE.  

1.  EVALUATE (truth statement such as true or false)

        WHEN condition PERFORM...

Example:

EVALUATE TRUE
    WHEN MARITAL-STATUS  1      PERFORM 100-MARRIAGE-RTN
    WHEN MARITAL-STATUS  2      PERFORM 200-SINGLE-RTN
    WHEN MARITAL-STATUS  3      PERFORM 300-DIVORCED-RTN
    WHEN OTHER                               PERFORM 400-ERROR-RTN
END-EVALUATE

In this example a field is in the record we are reading that contains either 1, 2 or 3.  When the MARITAL-STATUS field is 1 we perform 100-MARRIAGE-RTN. The actual interpretation is because 1 is in MARITAL-STATUS then this condition is true.  We could have just as easily said EVALUATE FALSE if we had some reason to do so in our logic.   As with any implementation of EVALUATE you may be tempted to write the code you want to implement for that condition right after the when such as:

    WHEN MARITAL-STATUS 1
           MOVE MARRIED TO STATUS-P
           ETC.......
The beauty of the EVALUATE over a string of IF statements is it's compactness. You should branch to a subroutine to perform your operations.  In this manner you are able to program in a more structured organized manner. Also note in this example that we are not allowing for an out of bounds condition.  If our status is 4 then nothing would be done and if 4 happens to simply be invalid data then we have let a bad record be processed. EVALUATE routines allow us to compactly test many conditions from a status field and also to validate data.

1B. EVALUATE TRUE USING 88 LEVEL (POWERFUL!!!)

(SOMEWHERE EMBEDDED IN YOUR FILE DESCRIPTION)

01 MARITAL-STATUS        PIC 9.
    88    MARRIED    VALUE1.
    88    SINGLE       VALUE 2.
    88    DIVORCED  VALUE 3.

(SOMEWHERE IN YOUR PROCEDURE DIVISION)

EVALUATE TRUE
    WHEN MARRIED     MOVE "MARRIED" TO STATUS-OUT
    WHEN SINGLE         MOVE "SINGLE" TO STATUS-OUT
    WHEN DIVORCED   MOVE "DIVORCED" TO STATUS-OUT
    WHEN OTHER           PERFORM 400-ERROR-RTN
END-EVALUATE

In the preceding code we are also using "EVALUATE TRUE" but with an88 level.  When we say WHEN MARRIED it is the same as saying WHEN MARITAL-STATUS 1 as in the previous example.  The difference is that our statement is more self-documenting.  You could easily look at the code and know what you were testing in the evaluate.  With a series of complex codes it might be easy to have a mix-up but using the actual word it is more readable.  You can embed the 88 level in your FD or you may find one embedded in a copy file that you are bringing in as your FD

You could also code the 88 level code in the working storage section under a group name you define such as MARITAL-STATUS for instance.  Then in your logic you would move STATUS-IN TO MARITAL STATUS before your evaluate.  I would only do this if you can not embed the 88 level in the FD.  Sometimes you are given copy statements you must use and so this is not possible but if you are making your own FD then using the 88 level will make your coding much easier.  See your text for more on 88 level statements.

  2.  EVALUATE (identifier)

        WHEN values(s) PERFORM...

Example:

EVALUATE MARITAL-STATUS
    WHEN 1              PERFORM 100-MARRIED-RTN
    WHEN 2              PERFORM 200-SINGLE-RTN
    WHEN 3              PERFORM 300-DIVORCED-RTN
    WHEN OTHER  PERFORM 400-ERROR-RTN
 END-EVALUATE

This routine does exactly the same thing as example one.  Here we look at the MARITAL-STATUS field and depending on the number we execute a simple PERFORM. Once again we have not taken into account invalid data which could have slipped into the MARITAL-STATUS field..

 3.  EVALUATE (condition) 

       EVALUATE   [CONDITION]
          W HEN TRUE PERFORM...
          WHEN FALSE PERFORM
      END-EVALUATE

Example:

EVALUATE MARITAL-STATUS =1
        WHEN TRUE PERFORM 100-MARRIED-RTN
        WHEN FALSE PERFORM 200-SINGLE-RTN
END-EVALUATE

In this situation we could only test for two conditions because we are limited to true and false for outcomes of the test we are performing.  This might be more useful for testing a field for sex for instance where only two choices exist.

PRACTICAL USAGE IN YOUR PROGRAM

Now that you have an overview of some of the usage's of EVALUATE let's look at how to apply this knowledge to your assignment 3 and beyond.  First ask yourself how many conditions you are testing.  If you are testing more than 2 then you are limited to example 1 or 2.   Usage 1 can be confusing to some people who have never had a programming class before and so I will give an example for using usage 2 for several conditions including a test for invalid data. 

EVALUATE MARITAL-STATUS
    WHEN 1               PERFORM 100-MARRIED-RTN
    WHEN 2               PERFORM 200-SINGLE-RTN
    WHEN 3               PERFORM 300-DIVORCED-RTN
    WHEN OTHER   PERFORM 400-ERR-RTN
 END-EVALUATE

Notice that with the addition of a WHEN OTHER we can make sure our data is valid.  If the data is not one of the valid values we can perform error processing on the record.  Now that you know how to use evaluate to test multiple conditions and validate your data where do you put it in your program?  The concept behind COBOL is processing records.  When you process a record you read a field of your current record and do any processing on that field and then write data to your output field that you will print in your report.  Thus, in the PROCEDURE division when I processing each field of a record I will use the EVALUATE when I come to a field I need to test for multiple conditions. The code in the PROCEDURE DIVISION may look similar to the following:

    MOVE SOCIAL-NUM-IN TO SOCIAL-NUM-P
    MOVE LAST-NAME-IN TO LAST-NAME-P
    MOVE FIRST-NAME-IN TO FIRST-NAME-P

    EVALUATE MARITAL-STATUS
       WHEN 1              PERFORM 100-MARRIED-RTN
       WHEN 2              PERFORM 200-SINGLE-RTN
       WHEN 3              PERFORM 300-DIVORCED-RTN
       WHEN OTHER  PERFORM 400-ERR-RTN
    END-EVALUATE

    MOVE ADDRESS-IN TO ADDRESS-P
    MOVE ZIP-IN TO ZIP-P
    Etc.....

    Then a subroutine would do the processing you desire. The subroutine 100-MARRIED-RTN might look as follows:

100-MARRIED-RTN.
    MOVE 'MARRIED' TO STATUS-P.

That's the whole subroutine, merely moving the status to STATUS-P in preparation for printing the report line when all the processing of the record is complete. One IMPORTANT thing to remember is to allow enough spaces in the STATUS-P for your largest literal.  For instance if we coded PIC X(07). for STATUS-P we would not have enough space for divorced to print when its code arose.  You must code enough space for the largest literal you will encounter.  In the case of your assignment you will probably need space for some type of invalid message.

I have tried to be exhaustive in everything you need to code the EVALUATE into your assignment.  There is much more to the EVALUATE verb that you can read from your text or other reference material.

HOME

All pictures and material Copyright 1998 - 2004, A+ copyright Comptia, CCNA copyright Cisco Systems.  This page is optimized for Internet Explorer 6.0 at 800 x 600 resolution For problems or questions regarding this web contact [Webmaster]. Last updated: April 20, 2004.