The EVALUATE statement in COBOL is a powerful and flexible control structure that simplifies complex decision logic. It's similar to the CASE or SWITCH statements in other programming languages but offers more capabilities. EVALUATE provides a cleaner, more maintainable alternative to nested IF/ELSE structures when you need to test a value against multiple possibilities.
123456789EVALUATE subject WHEN selection-1 statement-1... WHEN selection-2 statement-2... ... WHEN OTHER statement-n... END-EVALUATE.
The subject is the expression being evaluated, and each WHEN clause specifies a possible value or condition to match. The statements following a matching WHEN are executed, and then control passes to the statement after END-EVALUATE.
1234567891011121314EVALUATE WS-GRADE WHEN 'A' DISPLAY 'Excellent performance' WHEN 'B' DISPLAY 'Good performance' WHEN 'C' DISPLAY 'Satisfactory performance' WHEN 'D' DISPLAY 'Poor performance' WHEN 'F' DISPLAY 'Failed' WHEN OTHER DISPLAY 'Invalid grade' END-EVALUATE.
This example tests the value of WS-GRADE and displays a message based on the grade value.
Note: Only the first matching WHEN clause will have its statements executed. Once a match is found, the remaining WHEN clauses are ignored, and execution continues after END-EVALUATE.
The WHEN clauses in an EVALUATE statement specify the conditions to test against the subject. Each WHEN can take several forms, allowing for flexible pattern matching. The WHEN OTHER clause serves as a catch-all for values not matched by any WHEN clause.
12345678910EVALUATE WS-NUMBER WHEN 1 *> Exact value match DISPLAY 'One' WHEN 2 THRU 5 *> Range of values DISPLAY 'Between 2 and 5' WHEN 10 12 15 *> Multiple distinct values DISPLAY 'Either 10, 12, or 15' WHEN OTHER *> Default case DISPLAY 'Some other number' END-EVALUATE.
This example demonstrates different ways to specify conditions in WHEN clauses: exact value, range using THRU, list of values, and the default WHEN OTHER.
The WHEN OTHER clause provides a safety net for handling unexpected values. It's similar to the DEFAULT case in SWITCH statements in other languages.
12345678910111213141516171819EVALUATE WS-DAY-OF-WEEK WHEN 1 MOVE 'Monday' TO WS-DAY-NAME WHEN 2 MOVE 'Tuesday' TO WS-DAY-NAME WHEN 3 MOVE 'Wednesday' TO WS-DAY-NAME WHEN 4 MOVE 'Thursday' TO WS-DAY-NAME WHEN 5 MOVE 'Friday' TO WS-DAY-NAME WHEN 6 MOVE 'Saturday' TO WS-DAY-NAME WHEN 7 MOVE 'Sunday' TO WS-DAY-NAME WHEN OTHER MOVE 'Invalid day' TO WS-DAY-NAME PERFORM ERROR-HANDLING END-EVALUATE.
Best Practice: Always include a WHEN OTHER clause to handle unexpected values. This improves program robustness and makes it easier to diagnose issues.
COBOL allows you to specify multiple conditions in a single WHEN clause. When any of these conditions match, the statements for that WHEN clause are executed.
1234567891011121314151617EVALUATE WS-MONTH WHEN 1 3 5 7 8 10 12 MOVE 31 TO WS-DAYS-IN-MONTH WHEN 4 6 9 11 MOVE 30 TO WS-DAYS-IN-MONTH WHEN 2 IF FUNCTION MOD(WS-YEAR, 4) = 0 AND (FUNCTION MOD(WS-YEAR, 100) NOT = 0 OR FUNCTION MOD(WS-YEAR, 400) = 0) MOVE 29 TO WS-DAYS-IN-MONTH ELSE MOVE 28 TO WS-DAYS-IN-MONTH END-IF WHEN OTHER MOVE 0 TO WS-DAYS-IN-MONTH DISPLAY 'Invalid month number' END-EVALUATE.
This example determines the number of days in a month, grouping months with the same number of days in a single WHEN clause.
The EVALUATE statement offers several advantages over nested IF/ELSE structures for complex decision logic. Let's compare the two approaches:
1234567891011121314151617IF WS-CODE = 'A' PERFORM PROCESS-A ELSE IF WS-CODE = 'B' PERFORM PROCESS-B ELSE IF WS-CODE = 'C' PERFORM PROCESS-C ELSE IF WS-CODE = 'D' PERFORM PROCESS-D ELSE PERFORM UNKNOWN-CODE END-IF END-IF END-IF END-IF.
123456789101112EVALUATE WS-CODE WHEN 'A' PERFORM PROCESS-A WHEN 'B' PERFORM PROCESS-B WHEN 'C' PERFORM PROCESS-C WHEN 'D' PERFORM PROCESS-D WHEN OTHER PERFORM UNKNOWN-CODE END-EVALUATE.
The EVALUATE version is:
To make the most effective use of the EVALUATE statement in your COBOL programs, follow these best practices:
Always include a WHEN OTHER clause to handle unexpected values. This improves program robustness and makes it easier to diagnose issues.
1234567891011EVALUATE WS-TRANSACTION-TYPE WHEN 'D' PERFORM PROCESS-DEPOSIT WHEN 'W' PERFORM PROCESS-WITHDRAWAL WHEN 'T' PERFORM PROCESS-TRANSFER WHEN OTHER DISPLAY 'Invalid transaction type: ' WS-TRANSACTION-TYPE PERFORM LOG-ERROR END-EVALUATE.
Group related conditions together for clarity and maintenance.
12345678910111213141516171819202122EVALUATE WS-RESPONSE-CODE * Success codes WHEN 200 PERFORM PROCESS-SUCCESS WHEN 201 202 204 PERFORM PROCESS-SUCCESS-WITH-INFO * Client error codes WHEN 400 PERFORM PROCESS-BAD-REQUEST WHEN 401 403 PERFORM PROCESS-AUTHENTICATION-ERROR WHEN 404 PERFORM PROCESS-NOT-FOUND * Server error codes WHEN 500 THRU 599 PERFORM PROCESS-SERVER-ERROR WHEN OTHER PERFORM PROCESS-UNKNOWN-CODE END-EVALUATE.
EVALUATE TRUE allows you to use complex conditions in WHEN clauses, similar to a series of IF statements but with cleaner structure.
123456789101112EVALUATE TRUE WHEN WS-AGE < 18 PERFORM PROCESS-MINOR WHEN WS-AGE >= 65 PERFORM PROCESS-SENIOR WHEN WS-INCOME > 100000 PERFORM PROCESS-HIGH-INCOME WHEN WS-ACCOUNT-BALANCE < 1000 PERFORM PROCESS-LOW-BALANCE WHEN OTHER PERFORM PROCESS-STANDARD-CUSTOMER END-EVALUATE.
When testing the same variable against multiple values, use EVALUATE instead of nested IF statements for better readability and maintainability.
Add comments to explain the purpose of complex EVALUATE statements, especially when using ranges or multiple values.
12345678910111213141516EVALUATE WS-STATUS-CODE * Active customer statuses WHEN 'A' 'P' 'V' PERFORM PROCESS-ACTIVE-CUSTOMER * Inactive customer statuses WHEN 'I' 'S' 'T' PERFORM PROCESS-INACTIVE-CUSTOMER * Special handling cases WHEN 'X' PERFORM PROCESS-PENDING-REVIEW WHEN OTHER PERFORM PROCESS-UNKNOWN-STATUS END-EVALUATE.
Convert Nested IF to EVALUATE
Convert the following nested IF structure to an EVALUATE statement:
IF DEPARTMENT-CODE = 'HR'
PERFORM PROCESS-HR
ELSE
IF DEPARTMENT-CODE = 'IT'
PERFORM PROCESS-IT
ELSE
IF DEPARTMENT-CODE = 'FIN'
PERFORM PROCESS-FINANCE
ELSE
IF DEPARTMENT-CODE = 'MKT'
PERFORM PROCESS-MARKETING
ELSE
PERFORM PROCESS-OTHER-DEPARTMENT
END-IF
END-IF
END-IF
END-IF.
Create a Tax Calculation Logic
Write an EVALUATE statement that calculates income tax based on income brackets:
Implement Input Validation
Write an EVALUATE TRUE statement that validates a customer record with multiple conditions, checking for valid customer type, valid state code, and valid credit score range.
Grade Classification
Write an EVALUATE statement that assigns a letter grade based on a numeric score (0-100), using standard grade ranges (A: 90-100, B: 80-89, C: 70-79, D: 60-69, F: Below 60).
Day of Week Calculator
Write a COBOL program segment that uses EVALUATE to determine if a given day number (1-7) is a weekday or weekend, and displays an appropriate message.
1. What is the primary purpose of the EVALUATE statement in COBOL?
2. Which part of the EVALUATE statement handles cases not covered by any WHEN clause?
3. What happens when multiple WHEN conditions are satisfied in an EVALUATE statement?
4. How does the EVALUATE statement terminate?
5. Which of the following is NOT a valid subject for an EVALUATE statement?
Various ways to implement decision-making in COBOL
Program flow control mechanisms in COBOL
Techniques for matching values against patterns
Approaches for writing efficient COBOL code
Writing clear, maintainable code through proper structuring