- Example 1:-
Requirement:- Code conditions such that if EMP-RANK is 1, message ‘WINNER’ should be displayed. For EMP-RANK 2 & 3, message ‘1st Runner up’ and ‘2nd Runner up’ should be displayed respectively. For any other ranks, message ‘BETTER LUCK NEXT TIME’ should be displayed
Code to implement above requirement:-
EVALUATE EMP-RANK
WHEN 1
DISPLAY ‘WINNER’
WHEN 2
DISPLAY ‘FIRST RUNNER UP’
WHEN 3
DISPLAY ‘SECOND RUNNER UP’
WHEN OTHER
DISPLAY ‘BETTER LUCK NEXT TIME’
END-EVALAUTE.
In above example, we could have used nested IF, but using EVALUATE is far simple when compared to Nested IF.
- Example 2:-
Below example will demonstrate use of THRU phrase to check if subject’s value is within given range.
Requirement:- We wish to display grade based on employee score:-
|
SCORE
|
GRADE
|
|
70-100
|
A
|
|
40-69
|
B
|
|
0-39
|
C
|
Code to implement above requirement:-
EVALUATE EMP-GRADE
WHEN 70 THRU 100
DISPLAY ‘EMP GRADE : A’
WHEN 40 THRU 69
DISPLAY ‘EMP GRADE : B’
WHEN 0 THRU 39
DISPLAY ‘EMP GRADE : C’
WHEN OTHER
DISPLAY ‘ERROR GENERATING GRADE’
END-EVALAUTE.
- Example 3:-
Below example will demonstrate use of ALSO phrase to check multiple subjects. Requirement:- Suppose you want to assign desk to employee based on their grade and gender; Acceptable grade can be ‘A’, ‘B’ or ‘C’ and employee gender can be ‘M’ for male employees and ‘F’ for female employees
Code to implement above requirement:-
EVALUATE EMP-GRADE ALSO EMP-GENDER
WHEN ‘A’ ALSO ‘M’
DISPLAY ‘DELUX ROOM IN MALE WARD ASSIGNED’
WHEN ‘A’ ALSO ‘F’
DISPLAY ‘DELUX ROOM IN FEMALE WARD ASSIGNED’
WHEN ‘B’ ALSO ‘M’
DISPLAY ‘PERSONAL CABIN IN MALE WARD ASSIGNED’
WHEN ‘B’ ALSO ‘F’
DISPLAY ‘PERSONAL CABIN IN FEMALE WARD ASSIGNED’
WHEN OTHER
DISPLAY ‘GENERAL DESK ASSIGNED’
END-EVALAUTE.
- Example 4:-
Below example will demonstrate use of TRUE as subject. Requirement:- Need to give bonus to employee if they have completed 6 months in organization.
Code to implement above requirement:-
EVALUATE TRUE
WHEN EMP-MONTHS > 6
ADD WS-BONUS TO WS-SALARY
WHEN EMP-MONTHS < 6
DISPLAY '6 MONTHS NOT COMPLETED BY EMPLOYEE'
WHEN OTHER
DISPLAY 'INVALID VALUE OF EMP-MONTHS'
END-EVALAUTE.
- Example 5:-
Below example will demonstrate use of multiple consecutive WHEN phrases to execute same statement block. Requirement:- Code a logic such that employees with grade A and B will get full bonus and grade ‘C’ employee will get half bonus.
Code to implement above requirement:-
EVALUATE EMP-GRADE
WHEN 'A'
WHEN 'B'
ADD FULL-BONUS TO WS-SALARY
WHEN 'C'
ADD HALF-BONUS TO WS-SALARY
WHEN OTHER
DISPLAY 'INVALID EMP-GRADE'
END-EVALAUTE.
In above case, If EMP-GRADE is ‘A’ or ‘B’, FULL-BONUS will be added to WS-SALARY. If EMP-GRADE is ‘C’ then HALF- BONUS will be added to WS-SALARY. In other cases, error message will be displayed