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
   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]

No comments:

Post a Comment

Please Provide your feedback here