Evaluate with Multiple Conditions
Evaluate with Multiple Conditions Example
Scenario - Validating age and gender.
Code -
----+----1----+----2----+----3----+----4----+----5----+
IDENTIFICATION DIVISION.
PROGRAM-ID. EVALWCOM.
AUTHOR. MTH.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-VAR.
05 WS-AGE PIC 9(02) VALUE 39.
05 WS-GENDER PIC X(01) VALUE "M".
PROCEDURE DIVISION.
EVALUATE TRUE ALSO TRUE
WHEN WS-AGE > 18 ALSO WS-GENDER = "M"
DISPLAY "HE IS MAJOR"
WHEN WS-AGE <= 18 ALSO WS-GENDER = "M"
DISPLAY "BOY IS MINOR"
WHEN WS-AGE > 18 ALSO WS-GENDER = "F"
DISPLAY "SHE IS MAJOR"
WHEN WS-AGE <= 18 ALSO WS-GENDER = "M"
DISPLAY "GIRL IS MINOR"
WHEN OTHER
DISPLAY "INVALID INPUT"
END-EVALUATE.
STOP RUN.
Output -
HE IS MAJOR
Explaining Example -
In the above example: it evaluates two conditions simultaneously using the "ALSO" keyword within an EVALUATE statement.
- If WS-AGE is greater than 18 AND WS-GENDER is "M", it displays "HE IS MAJOR".
- If WS-AGE is less than or equal to 18 AND WS-GENDER is "M", it displays "BOY IS MINOR".
- If WS-AGE is greater than 18 AND WS-GENDER is "F", it displays "SHE IS MAJOR".
- If WS-AGE is less than or equal to 18 AND WS-GENDER is "M", it displays "GIRL IS MINOR".
- If none of the specified conditions are met, it displays "INVALID INPUT".
It displays "HE IS MAJOR" because the WHEN "E" clause is satisfied.