Mainframes 360
The one stop destination for System Z professionals

Wednesday, January 6, 2010

EVALUATE Statement – Program Flow Control

Q. What is the EVALUATE Statement used for? How does it work?
When there are multiple options to choose from, the EVALUATE statement works best. When there are several choices to be made, IF-ELSE logic can become very complicated. To avoid writing complex nested IF Conditionals(condition-within-condition), the EVALUATE Statement can be used. The EVALUATE statement is the COBOL equivalent of switch-case in most conventional programming languages.

The general format of the EVALUATE Statement is as follows :

Syntax:

EVALUATE <expression>
   WHEN condition-1
     Statement-1
     ...

   WHEN condition-2
     Statement-2
     ...


   WHEN OTHER
     Statement-n
     ...

END-EVALUATE
STATEMENT-X

Interpretation:

The EVALUATE computes the value of the main expression.

1. If the value satisfies condition-1, then Statement-1 is performed, and after execution, control jumps to STATEMENT-X(Outside the EVALUATE Block).

2. Else If the value satisfies condition-2, then Statement-2 is performed, and after execution, control jumps to STATEMENT-X(Outside the EVALUATE Block).

...

n. If the values satisfies none of the above conditions, then by default WHEN OTHER case is executed(Statement-n), and then the control jumps to STATEMENT-X.(Outside the EVALUATE Block)

Thus, at each level of the EVALUATE block, the condition is checked, if it holds true, the case is executed, if it doesn’t hold true, you descend to the next lower level and so on... This is called a fall-through.

Example:

Image60[1] 
Upon running the above COBOL Program, you should get the following output-

Image61[1] 
Q. What are subjects and objects in the EVALUATE Block? How are they used?
Generally, the expression being tested, specified by EVALUATE Clause is called the subject. The values against which it is tested, specified by the WHEN Clause is called the object. Thus, you test a subject expression against an object value using the EVALUATE case structure.

The EVALUATE Block allows you to specify the multiple subjects and objects. Each subject in the EVALUATE Clause would be tested against the corresponding object-value in the WHEN Clause.

Example:
Suppose you want to write a simple COBOL Program to decide the pay of an employee. The pay of the employee is calculated as per this table.

CONDITION
DESIGNATION  LEVEL-OF-EXP  FLEXIBLE

Salary

            ACCOUNTANT    EXPERIENCED   WILLING-TO-TRAVEL 2000
            ACCOUNTANT    INEXPERIENCED WANTS-TO-STAY-PUT 1000
     COMPUTER-SCIENTIST   ANY           ANY 3000
     SYSTEMS-ANALYST      EXPERIENCED   ANY 4000
     ANY                  EXPERIENCED   ANY 500

There are 3 subjects that you need to evaluate in above table, to decide the salary of the employee. The variables for the above data in COBOL are as shown below:

Image62[1] 
To calculate the salary of the employee according to the above decision chart, I have written the following code in COBOL :

Image63[1]
Now, in the above COBOL Program, I have set ACCOUNTANT flag to true, and he is INEXPERIENCED, and wants to STAYPUT. For such a condition, the salary should be 1000. Upon running the above COBOL Program, you should get the following output :

Image64[1] 

To many people who are thrown to work at a mainframe computer on their first job, they feel lost. Mainframe people seem to speak a completely different language and that doesn't make life easy. What's more, the books and manuals are incredibly hard to comprehend.

"What on earth is a Mainframe?" is an absolute beginner's guide to mainframe computers. We'll introduce you to the hardware and peripherals. We'll talk about the operating system, the software installed on a mainframe. We'll also talk about the different people who work on a mainframe. In a nutshell, we'll de-mystify the mainframe.

Readers based in India, can buy the e-book for Rs. 50 only or the print book. International readers based in the US and other countries can click here to purchase the e-book.