MainframeMaster

COBOL Condition Testing

Condition testing in COBOL involves evaluating various types of conditions to make decisions. Learn about IF statements, EVALUATE statements, and different condition types for effective decision-making.

Basic IF Statements

cobol
1
2
3
4
5
6
7
IF CUSTOMER-BALANCE > 1000 DISPLAY 'High balance customer' PERFORM PROCESS-HIGH-BALANCE ELSE DISPLAY 'Standard customer' PERFORM PROCESS-STANDARD-CUSTOMER END-IF.

Use IF statements for simple true/false decisions. The condition is evaluated, and if true, the statements after IF execute. If false, the ELSE clause (if present) executes. Always use END-IF to close the statement.

Nested IF Statements

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
IF CUSTOMER-TYPE = 'I' IF CUSTOMER-BALANCE > 5000 DISPLAY 'Premium individual customer' PERFORM PROCESS-PREMIUM-INDIVIDUAL ELSE DISPLAY 'Standard individual customer' PERFORM PROCESS-STANDARD-INDIVIDUAL END-IF ELSE IF CUSTOMER-TYPE = 'C' DISPLAY 'Corporate customer' PERFORM PROCESS-CORPORATE ELSE DISPLAY 'Unknown customer type' PERFORM PROCESS-UNKNOWN END-IF.

Nest IF statements for complex decision trees. Use ELSE IF for multiple conditions. Proper indentation makes nested logic easier to read and maintain. Each IF must have a corresponding END-IF.

EVALUATE Statements

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
EVALUATE CUSTOMER-STATUS WHEN 'A' DISPLAY 'Active customer' PERFORM PROCESS-ACTIVE WHEN 'I' DISPLAY 'Inactive customer' PERFORM PROCESS-INACTIVE WHEN 'S' DISPLAY 'Suspended customer' PERFORM PROCESS-SUSPENDED WHEN OTHER DISPLAY 'Unknown status' PERFORM PROCESS-UNKNOWN END-EVALUATE.

Use EVALUATE for multi-way decisions based on a single variable. It's cleaner than nested IF statements when you have many possible values. WHEN OTHER handles any values not explicitly listed.

Class Conditions

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
IF CUSTOMER-ID IS NUMERIC DISPLAY 'Valid numeric ID' PERFORM PROCESS-NUMERIC-ID ELSE DISPLAY 'Invalid non-numeric ID' PERFORM PROCESS-INVALID-ID END-IF. IF CUSTOMER-NAME IS ALPHABETIC DISPLAY 'Valid alphabetic name' ELSE DISPLAY 'Name contains non-letters' END-IF.

Class conditions test the type of data. NUMERIC tests if data contains only digits (0-9). ALPHABETIC tests if data contains only letters (A-Z, a-z). Use these for data validation and type checking.

Sign Conditions

cobol
1
2
3
4
5
6
7
8
9
10
IF TRANSACTION-AMOUNT IS POSITIVE DISPLAY 'Credit transaction' PERFORM PROCESS-CREDIT ELSE IF TRANSACTION-AMOUNT IS NEGATIVE DISPLAY 'Debit transaction' PERFORM PROCESS-DEBIT ELSE IF TRANSACTION-AMOUNT IS ZERO DISPLAY 'Zero amount transaction' PERFORM PROCESS-ZERO END-IF.

Sign conditions test the sign of numeric values. POSITIVE tests for values greater than zero, NEGATIVE for values less than zero, and ZERO for exactly zero. These are useful for financial calculations.

Complex Conditions

cobol
1
2
3
4
5
6
7
8
IF (CUSTOMER-TYPE = 'I' AND CUSTOMER-BALANCE > 1000) OR (CUSTOMER-TYPE = 'C' AND CUSTOMER-BALANCE > 5000) DISPLAY 'Eligible for premium service' PERFORM PROCESS-PREMIUM ELSE DISPLAY 'Standard service only' PERFORM PROCESS-STANDARD END-IF.

Combine conditions using AND, OR, and NOT operators. Use parentheses to control evaluation order. Complex conditions allow sophisticated business logic while keeping code readable.

EVALUATE with Multiple Conditions

cobol
1
2
3
4
5
6
7
8
9
10
11
12
EVALUATE TRUE WHEN CUSTOMER-TYPE = 'I' AND CUSTOMER-BALANCE > 1000 PERFORM PROCESS-PREMIUM-INDIVIDUAL WHEN CUSTOMER-TYPE = 'I' AND CUSTOMER-BALANCE <= 1000 PERFORM PROCESS-STANDARD-INDIVIDUAL WHEN CUSTOMER-TYPE = 'C' AND CUSTOMER-BALANCE > 5000 PERFORM PROCESS-PREMIUM-CORPORATE WHEN CUSTOMER-TYPE = 'C' AND CUSTOMER-BALANCE <= 5000 PERFORM PROCESS-STANDARD-CORPORATE WHEN OTHER PERFORM PROCESS-UNKNOWN-TYPE END-EVALUATE.

Use EVALUATE TRUE with WHEN clauses for complex multi-condition logic. This is cleaner than deeply nested IF statements and makes all conditions visible at the same level.