EVALUATE statement is used for conditional processing in COBOL.
EVALUATE
It performs the various set of tasks-
- If multiple conditions need to be checked then EVALUATE is a better than IF-ELSE.
- Using a single EVALUATE condition, we can check multiple conditions.
- Nesting of ‘IF-ELSE’ statements can be messy hence instead of using IF-ELSE, we can code a single EVALUATE which is much more powerful than IF-ELSE
The Syntax of EVALUATE Statement
EVALUATE {cond-for-evaluate} [ALSO] {cond-for-evaluate} [ALSO] …
WHEN {possible-cond-1} [ALSO] {possible-cond-2} [ALSO] …
{executable statement/statements}
WHEN {possible-cond-3} [ALSO] {possible-cond-4} [ALSO] …
{executable statement/statements}
…..
…..
[WHEN OTHER] {executable statement/statements}
END-EVALUATE.
Here, {cond-for-evaluate} —> Can be any one of the following:
a) Constant/literals
b) expression/identifier
c) reference
d) TRUE
e) FALSE
Most used EVALUATE
Format/Example 1
EVALUATE TRUE
WHEN A = 5
COMPUTE A = (A*B)/C
WHEN A = 10
MOVE A TO B
WHEN A = 15
ADD 5 TO A
WHEN OTHER
DISPLAY “UNEXPECTED VALUE of A”
END-EVALUATE.
EVALUATE Format/Example 2
EVALUATE LEVEL-88-COND
WHEN 5
DISPLAY “A HAS A VALUE OF 5”
WHEN 10
DISPLAY “A HAS A VALUE OF 10”
WHEN 15
DISPLAY “A HAS A VALUE OF 15”
END-EVALUATE.
EVALUATE Format/Example 3
EVALUATE AGE
WHEN 0 THRU 9
DISPLAY “Children”
WHEN 10 THRU 19
DISPLAY “Adolescents”
WHEN 20 THRU 45
DISPLAY “Adults”
WHEN 46 THRU 60
DISPLAY “Middle age”
WHEN 60 THRU 100
DISPLAY “Old”
WHEN OTHER
DISPLAY “you are a miracle”
END-EVALUATE.
EVALUATE Format/Example 4
EVALUATE AGE
WHEN 1
WHEN 5
WHEN 9
DISPLAY “CHILDREN”
WHEN 10
WHEN 15
DISPLAY “ADOLESCENTS”
WHEN 20
WHEN 45
DISPLAY “ADULTS”
WHEN 46
WHEN 59
DISPLAY “MIDDLE AGE”
WHEN 60
WHEN 100
DISPLAY “OLD”
WHEN OTHER
DISPLAY “YOU ARE A MIRACLE”
END-EVALUATE.
EVALUATE Format/Example 5
EVALUATE TRUE ALSO AGE
WHEN A = 5 ALSO 1
DISPLAY ” A IS 5 AND CHILDREN GROUP”
MOVE 10 TO B
WHEN A = 10 ALSO 10
DISPLAY ” A IS 10 AND ADOLESCENTS GROUP”
ADD 15 TO C
WHEN A = 15 ALSO 25
DISPLAY ” A IS 15 AND ADULT GROUP”
END-EVALUATE.