EVALUATE
COBOL's way to choose between many cases. It is clearer than a long chain of IFs.
Also written EVALUATE WHEN, case statement
Why it matters. Account types, status codes and transaction types each need their own action. EVALUATE lists them one by one.
See it
Check several conditions in order. This is COBOL's switch.
Syntax
EVALUATE subject [ALSO subject] WHEN value [ALSO value] statements WHEN OTHER statements END-EVALUATE
- subject
- A field, a value, or TRUE or FALSE.
- WHEN
- One case. The first case that matches runs.
- THRU
- Matches a range of values, such as 1 THRU 9.
- ALSO
- Test two or more subjects together, like a decision table.
- OTHER
- Catches everything not listed.
Try it EVALUATE
- 1
In the program EVTYPE, the field WS-TYPE holds a one-letter account type. Start the choice: tell COBOL which field to look at.
COBOL EVALUATE WS-TYPE - 2
Add the first case: when the type is S, show SAVINGS ACCOUNT.
COBOL WHEN 'S' DISPLAY 'SAVINGS ACCOUNT' - 3
Run EVTYPE. WS-TYPE starts as S.
RUNCOBOL 'ZB0001.DEV.COBOL(EVTYPE)'
Remember
- EVALUATE looks at a field. The first WHEN that matches runs.
- WHEN OTHER catches every value you did not list. There is no fall-through, so you don't need a break.
- END-EVALUATE closes the statement. EVALUATE TRUE lets each WHEN be a full condition.
The usual mistake
EVALUATE with no WHEN OTHER
A value you did not plan for matches nothing. The program carries on, and nobody is told.
Add WHEN OTHER and show an error or set a flag
On real z/OS
MF/OS runs EVALUATE as the language defines it, including ALSO. It is checked at run time rather than at compile time.
Where can I practise EVALUATE?
Right here, at ZedBank. You take a junior job at a simulated bank and use EVALUATE in real work, with Zed beside you. There's nothing to install.
Start at ZedBankRelated
- PROCEDURE DIVISIONThe part of a COBOL program that contains the actual logic, the statements that run.
- PERFORMThe COBOL statement that runs a paragraph and returns, and the way loops are written.
- Level numberThe two-digit number in front of a COBOL data item that shows how fields nest inside a record.
- COBOLThe programming language most business logic on the mainframe is written in. Verbose by design, and still running the world's payments.