Let's understand Mainframe
Home Tutorials Interview Q&A Quiz Mainframe Memes Contact us About us

MODULE 7: COBOL Control statements


EVALUATE statement

  • EVALUATE statement introduced in COBOL-85
  • It is used to implement the case structure. It is similar to SWITCH statement of Java programming language
  • EVALUATE statement is COBOL’s multi way branch statement. It provides an easy way to dispatch execution to different parts of your code based on the value of expression
  • It works as better alternative than large series of IF-ELSE-IF statements
  • EVALUATE statement’s general format is as given below:-

    EVALUATE subject-1 [ALSO subject-2]… WHEN object-1 [ALSO object-2]… <statement-block-1> WHEN object-3 [ALSO object-4]… <statement-block-2> WHEN OTHER <statement-block-3> END-EVALUATE.

  • Where,
    • subject can be identifier/literal/arithmetic or conditional expression/ TRUE/ FALSE
    • object can be either of following:-
      • ANY
      • condition
      • TRUE
      • FALSE
      • [NOT] {identifier-1/literal-1/arithmetic-expression-1} {THROUGH / THRU} {identifier-2/literal-2/arithmetic-expression-2}
  • Some important points to be considered while using EVALUATE:-
    • Number of subjects in EVALUATE clause should be equal to number of objects in every WHEN clause
    • When one of the WHEN clauses has been executed, the control is automatically passed to the statement following END-EVALUATE
    • EVALUATE work like this :-
      • The value of subject is compared with each of the object in WHEN phrase. If a match is found, statement-block following that WHEN clause is executed. If none of the object matches the subject, then the WHEN OTHER path will be executed
      • However, WHEN OTHER is optional to specify. If no object matches and WHEN OTHER condition is not present, then no further action is taken.

EVALUATE Examples:-

  • 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



Advertisements






© copyright mainframebug.com
Privacy Policy