EVALUATE
Part of the Decisions and Loops section of Coddy's COBOL Programming journey. Lesson 8 of 20.
EVALUATE picks one branch out of many. It compares a subject with each WHEN value, in order, and runs the first match:
EVALUATE LIGHT
WHEN "G"
DISPLAY "Go"
WHEN "R"
DISPLAY "Stop"
WHEN OTHER
DISPLAY "Wait"
END-EVALUATE.WHEN OTHER runs when nothing else matched. Without it, an unmatched value runs no branch and the program goes on after END-EVALUATE.
WHEN lines in a row share the branch under them, and THRU covers a range:
WHEN 4
WHEN 6
DISPLAY "30 days"
WHEN 1 THRU 12
DISPLAY "31 days"Only the first matching WHEN runs. Put the narrow cases before the wide ones, or the wide range catches them first.
Challenge
EasyRead CMD and print Deposit for D, Withdrawal for W, Balance for B and Unknown command for anything else.
Example input, one line per ACCEPT: D.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. COMMAND.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CMD PIC X.
PROCEDURE DIVISION.
ACCEPT CMD.
*> EVALUATE CMD here
STOP RUN.