MainframeMaster

COBOL Tutorial

EVALUATE Statement in COBOL

Progress0 of 0 lessons

EVALUATE Statement Basics

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.

Basic EVALUATE Syntax

cobol
1
2
3
4
5
6
7
8
9
EVALUATE 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.

Simple Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
EVALUATE 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.

WHEN Clauses and WHEN OTHER

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.

Types of WHEN Clauses

cobol
1
2
3
4
5
6
7
8
9
10
EVALUATE 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 Importance of 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.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
EVALUATE 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.

Multiple WHEN Conditions

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.

Multiple Values in a WHEN Clause

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
EVALUATE 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.

Comparison to IF/ELSE Structures

The EVALUATE statement offers several advantages over nested IF/ELSE structures for complex decision logic. Let's compare the two approaches:

Using Nested IF/ELSE

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
IF 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.

Using EVALUATE

cobol
1
2
3
4
5
6
7
8
9
10
11
12
EVALUATE 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:

  • More readable - The structure is flatter and easier to follow
  • More maintainable - Adding or removing cases is simpler
  • Less error-prone - Fewer chances for mismatched END-IFs
  • More efficient - The compiler can often optimize EVALUATE statements better than nested IFs

Best Practices for EVALUATE Usage

To make the most effective use of the EVALUATE statement in your COBOL programs, follow these best practices:

Always Include WHEN OTHER

Always include a WHEN OTHER clause to handle unexpected values. This improves program robustness and makes it easier to diagnose issues.

cobol
1
2
3
4
5
6
7
8
9
10
11
EVALUATE 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 Cases

Group related conditions together for clarity and maintenance.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
EVALUATE 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.

Use EVALUATE TRUE for Complex Conditions

EVALUATE TRUE allows you to use complex conditions in WHEN clauses, similar to a series of IF statements but with cleaner structure.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
EVALUATE 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.

Prefer EVALUATE Over Nested IFs

When testing the same variable against multiple values, use EVALUATE instead of nested IF statements for better readability and maintainability.

Use Comments for Clarity

Add comments to explain the purpose of complex EVALUATE statements, especially when using ranges or multiple values.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
EVALUATE 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.

Exercises

  1. 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.
  2. Create a Tax Calculation Logic

    Write an EVALUATE statement that calculates income tax based on income brackets:

    • Income up to $10,000: 10% tax
    • Income $10,001 to $50,000: 15% tax
    • Income $50,001 to $100,000: 20% tax
    • Income $100,001 to $250,000: 25% tax
    • Income over $250,000: 30% tax
  3. 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.

  4. 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).

  5. 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.

FAQ Section

Test Your Knowledge

1. What is the primary purpose of the EVALUATE statement in COBOL?

  • Looping through data
  • Making complex decisions based on multiple conditions
  • Performing calculations
  • Defining data structures

2. Which part of the EVALUATE statement handles cases not covered by any WHEN clause?

  • CASE OTHER
  • DEFAULT
  • ELSE
  • WHEN OTHER

3. What happens when multiple WHEN conditions are satisfied in an EVALUATE statement?

  • All matching conditions execute their statements
  • Only the first matching condition executes its statements
  • The program raises an error
  • The WHEN OTHER clause executes

4. How does the EVALUATE statement terminate?

  • With END-EVALUATE
  • With a period (.)
  • With TERMINATE
  • With END-CASE

5. Which of the following is NOT a valid subject for an EVALUATE statement?

  • A variable
  • TRUE
  • An expression
  • A paragraph name