COBOL - Evaluate Statement
Evaluate verb is a replacement of series of IF-ELSE statement. It can be used to evaluate more than one condition.
EVALUATE statement in COBOL is similar to Case or Switch statements of other languages.
EVALUATE can do multiple IF conditions task. If any EVALUATE WHEN conditions satisfies, the list of statements will be executed under the WHEN and control will transfers to the next executable statement after ending of EVALUATE.
Let us see the syntax and examples of Evaluate statement below:
EVALUATE {Expression1/Constant/Reference/TRUE/FALSE}
WHEN Cond-1
Statement block-1
WHEN Cond-2
Statement block-2
.
.
.
.
WHEN Cond-n
Statement block-n
WHEN other
Statement block- other
END-EVALUATE.
|
Example 1:
The following example shows an EVALUATE command and the equivalent coding for an IF command:
EVALUATE menu-input
WHEN "0"
CALL init-proc
WHEN "1" THRU "9"
CALL process-proc
WHEN "R"
CALL read-parms
WHEN "X"
CALL cleanup-proc
WHEN OTHER
CALL error-proc
END-EVALUATE;
|
The equivalent IF command:
IF (menu-input = "0") THEN
CALL init-proc
ELSE
IF (menu-input >= "1") AND (menu-input <= "9") THEN
CALL process-proc
ELSE
IF (menu-input = "R") THEN
CALL read-parms
ELSE
IF (menu-input = "X") THEN
CALL cleanup-proc
ELSE
CALL error-proc
END-IF;
END-IF;
END-IF;
END-IF;
|
Example 2:
IDENTIFICATION DIVISION.
PROGRAM-ID. TSSWITCH.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-A PIC 9 VALUE 0.
PROCEDURE DIVISION.
MOVE 9 TO WS-A.
EVALUATE TRUE
WHEN WS-A > 2
DISPLAY 'WS-A GREATER THAN 2'
WHEN WS-A < 0
DISPLAY 'WS-A LESS THAN 0'
WHEN OTHER
DISPLAY 'INVALID VALUE OF WS-A'
END-EVALUATE.
STOP RUN.
|
When you compile and execute the above program, it produces the following result −
If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!