- Re-writing of select JCL and VSAM Tutorials is under-way, orientation would be more hands-on, and practical
- New Tutorials on COBOL-VSAM programs shall be put up pretty soon
- IMS DB/DC Tutorials to start up in April, 2010
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:
Upon running the above COBOL Program, you should get the following output-
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:
To calculate the salary of the employee according to the above decision chart, I have written the following code in COBOL :
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 :
0 comments:
Post a Comment