COBOL Evaluate

Working-Storage for all Examples

000340 01  PLANET.
000350     05  PLANET-NUMBER PIC 9.
000360     05  PLANET-NAME   PIC X(7).

Evaluate Example Number 1 - Evaluate a PIC 9 field

004330         WHEN 1     MOVE "Mercury"     TO PLANET-NAME
004340         WHEN 2     MOVE "Venus  "     TO PLANET-NAME 
004350         WHEN 3     MOVE "Earth  "     TO PLANET-NAME 
004360         WHEN 4     MOVE "Mars   "     TO PLANET-NAME 
004370         WHEN 5     MOVE "Jupiter"     TO PLANET-NAME
004380         WHEN 6     MOVE "Saturn "     TO PLANET-NAME
004390         WHEN 7     MOVE "Uranus "     TO PLANET-NAME
004400         WHEN 8     MOVE "Neptune"     TO PLANET-NAME
004410         WHEN 9     MOVE "Pluto  "     TO PLANET-NAME
004420         WHEN OTHER MOVE "       "     TO PLANET-NAME
004430     END-EVALUATE.

Evaluate Example Number 2 - Evaluate a PIC X field

004330         WHEN "Mercury"     MOVE 1 TO PLANET-NUMBER
004340         WHEN "Venus  "     MOVE 2 TO PLANET-NUMBER 
004350         WHEN "Earth  "     MOVE 3 TO PLANET-NUMBER 
004360         WHEN "Mars   "     MOVE 4 TO PLANET-NUMBER 
004370         WHEN "Jupiter"     MOVE 5 TO PLANET-NUMBER
004380         WHEN "Saturn "     MOVE 6 TO PLANET-NUMBER
004390         WHEN "Uranus "     MOVE 7 TO PLANET-NUMBER
004400         WHEN "Neptune"     MOVE 8 TO PLANET-NUMBER
004410         WHEN "Pluto  "     MOVE 9 TO PLANET-NUMBER
004420         WHEN OTHER         MOVE 0 TO PLANET-NUMBER
004420     END-EVALUATE.

Evaluate Example Number 3 - Evaluate True

004320     EVALUATE TRUE
004330         WHEN PLANET-NAME = "Mercury"     MOVE 1 TO PLANET-NUMBER
004340         WHEN PLANET-NAME = "Venus  "     MOVE 2 TO PLANET-NUMBER 
004350         WHEN PLANET-NAME = "Earth  "     MOVE 3 TO PLANET-NUMBER 
004360         WHEN PLANET-NAME = "Mars   "     MOVE 4 TO PLANET-NUMBER 
004370         WHEN PLANET-NAME = "Jupiter"     MOVE 5 TO PLANET-NUMBER
004380         WHEN PLANET-NAME = "Saturn "     MOVE 6 TO PLANET-NUMBER
004390         WHEN PLANET-NAME = "Uranus "     MOVE 7 TO PLANET-NUMBER
004400         WHEN PLANET-NAME = "Neptune"     MOVE 8 TO PLANET-NUMBER
004410         WHEN PLANET-NAME = "Pluto  "     MOVE 9 TO PLANET-NUMBER
004420         WHEN OTHER         MOVE 0 TO PLANET-NUMBER
004420     END-EVALUATE.
Comments Comments are left by visitors to and may or may not be accurate.
Comment by TheMadProfessor on 2007-11-16 Rate this Comment

I would have to disagree with the poster regarding 88-levels in an EVALUATE. Any phrase that will evaluate to true or false at execution time is acceptable, so the following should work just fine:


As far as indentation, etc are concerned, I would actually write it as above if it were a fairly simple case-structure like this with a single action to be taken in each case. For anything more complex, indentation similar to that described already is the way to go. The computer doesn't care - whatever is most likely to increase comprehension by someone performing maintenance months or years down the road is all that's important. The same goes for ordering the WHENs by probability - it may be important in a batch-style application where thousands or millions of comparisons are done or a real-time app when you might have to wring out every last drop of efficiency, but if not, keep them in a logical order that helps others comprehend what's going on. These days, CPU cycles are cheap, man-hours are expensive.

Comment by Anonymous on 2010-02-02 Rate this Comment

The comment about using 88-levels isn't quite correct. You can use a construct such as


Of course, this simple example would be better simply done as an IF. However, in more complex evaluations, it can be of use. For example, if a sales app calculates shipping cost depending on the customer's total purchase and shipping method, but gives a 1-grade discount to their prime customers, it could look something like this:

WHEN 0.01 THRU 24.99 ALSO 'Ground' ALSO FALSE
WHEN 0.01 THRU 24.99 ALSO '2nd day Air' ALSO TRUE
WHEN 0.01 THRU 24.99 ALSO '2nd day Air' ALSO FALSE
WHEN 0.01 THRU 24.99 ALSO 'Next day Air' ALSO TRUE
WHEN 25.00 THRU 49.99 ALSO 'Ground' ALSO FALSE
WHEN 25.00 THRU 44.99 ALSO '2nd day Air' ALSO TRUE

Of course, this can also be done with IFs, but there are 2 advantages to using the EVALUATE: repeated indenting isn't necessary and the structure becomes very similar to that of a truth-table which (IMO) is far easier to comprehend and maintain.

Also, to address the efficiency aspect: As you determine the relative probability of a set of conditions, resequencing the WHENs to place the highest-probability items first is a lot simpler that reordering nested IFs.

Comment by Larry on 2013-05-22 Rate this Comment

I like you indentation scheme of going in 2 for the WHEN and then going to a new line and indenting 2 more for the MOVE (or whatever will be done). Visually breaking the two up makes sense to me.

I'm not sure just where I got the particular scheme I show here. I think I did this around 1997. Working in a few COBOL shops I always tried to mirror the style of the area I was in - if they had a consistent or official style guide. Generally a good practice, but it sometimes left my own preferences inconsistent.

Comment by rslitman on 2007-03-13 Rate this Comment

I don't know if you have formatted the EVALUATEs the way you did in order to save space or if it is your style, but I prefer to put my WHENs on lines by themselves and then start what is to be done for each WHEN on a new line. I tried to illustrate it here by an example, but this software wouldn't take leading spaces, so I couldn't get it to indent properly.

Also, if two or more values are being test that lead to the same thing to be done, I put each WHEN on a line by itself.

In my coding style, I only indent two spaces under EVALUATE to put in my WHENs. I then indent two more spaces for the statement(s) to be performed for each WHEN. This puts the statement(s) to be performed in the same column in which they would have been if the old IF-construct had been used.

Comment by Anonymous on 2007-04-09 Rate this Comment

You can't use 88-levels in a WHEN statement unless you're saying something like EVALUATE TRUE WHEN LIFE. But you may as well use IF.

The EVALUATE VALUE(1:6) worked fine for me, except I added single quotes around the 1 and the 2. You're evaluating a string - use the quotes.

Comment by programmer123 on 2007-11-14 Rate this Comment

Which is faster EVALUATE or IF? The answer is "who cares?" COBOL is not a speed language. It's value is in clarity.

The Grace Hopper story about the length of wire is often misconstrued that she was interested in efficiency. She was not. I heard the story from her. The story was an anecdote about the speed of computers not the efficency of code.

Comment by archive on 2010-11-16 Rate this Comment

How do you interrogate a field using EVALUATE with level 88s in the input record?


05 pml-policy-master-layout pic 9.
88 life value 1.
88 dental value 2.

then later....

evaluate pml-policy-master-layout
when life
move 'life' to pad-policy-detail-line
when dental
move 'dental' to pad-...

This is not working....what would work?

Comment by archive on 2007-04-09 Rate this Comment

I have a doubt in an Evaluate statement. I am validating a file field with reference modification with the evaluate statement. It seems to work for valid data and not for the other caluse. Why? Any idea I would appreciate your help pls.

for ex:

Evaluate value(1:6)
when 1
do something
when 2
do something
when other
do some other thing

The code seems to work for values 1 & 2 and not for the other value why?

Comment by archive on 2010-03-24 Rate this Comment

We had a discussion today in a meeting about the speeds of the Evaluate and IF statements. Since this has sort of gone to the wayside because of the speed of computers some of the newer programmers like myself are not sure which is faster during runtime. Could you enlighten on which one is faster and under what type of conditions. Such as if the IF statement is structured so the most likely true statement is first would the evaluate still be faster?

Sign in to comment on COBOL Evaluate.