MainframeMaster

COBOL Conditional Statements

Conditional statements control program flow based on conditions. Learn IF-THEN-ELSE, EVALUATE-WHEN, nested conditions, and other decision-making constructs for effective program control.

Basic IF Statement

cobol
1
2
3
4
IF CUSTOMER-BALANCE > 0 DISPLAY 'Customer has positive balance' PERFORM PROCESS-POSITIVE-BALANCE END-IF.

The basic IF statement executes code when a condition is true. The condition is evaluated, and if true, the statements between IF and END-IF execute. If false, execution continues after END-IF.

IF-THEN-ELSE Statement

cobol
1
2
3
4
5
6
7
IF TRANSACTION-TYPE = 'DEPOSIT' ADD TRANSACTION-AMOUNT TO ACCOUNT-BALANCE DISPLAY 'Deposit processed' ELSE SUBTRACT TRANSACTION-AMOUNT FROM ACCOUNT-BALANCE DISPLAY 'Withdrawal processed' END-IF.

IF-THEN-ELSE provides alternative execution paths. If the condition is true, the THEN clause executes. If false, the ELSE clause 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 = 'INDIVIDUAL' IF CUSTOMER-AGE >= 18 DISPLAY 'Adult individual customer' PERFORM PROCESS-ADULT-INDIVIDUAL ELSE DISPLAY 'Minor individual customer' PERFORM PROCESS-MINOR-INDIVIDUAL END-IF ELSE IF CUSTOMER-TYPE = 'CORPORATE' DISPLAY 'Corporate customer' PERFORM PROCESS-CORPORATE ELSE DISPLAY 'Unknown customer type' PERFORM PROCESS-UNKNOWN END-IF.

Nest IF statements for complex decision logic. Each IF must have a corresponding END-IF. Use proper indentation to show the nesting level. ELSE IF provides multiple condition checks.

EVALUATE Statement

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

EVALUATE is ideal for multi-way decisions based on a single variable. Each WHEN clause handles a specific value. WHEN OTHER catches any values not explicitly handled. Use END-EVALUATE to close.

EVALUATE with Multiple Conditions

cobol
1
2
3
4
5
6
7
8
9
10
11
12
EVALUATE TRUE WHEN CUSTOMER-TYPE = 'INDIVIDUAL' AND CUSTOMER-AGE >= 65 PERFORM PROCESS-SENIOR-INDIVIDUAL WHEN CUSTOMER-TYPE = 'INDIVIDUAL' AND CUSTOMER-AGE < 65 PERFORM PROCESS-NON-SENIOR-INDIVIDUAL WHEN CUSTOMER-TYPE = 'CORPORATE' AND COMPANY-SIZE = 'LARGE' PERFORM PROCESS-LARGE-CORPORATE WHEN CUSTOMER-TYPE = 'CORPORATE' AND COMPANY-SIZE = 'SMALL' PERFORM PROCESS-SMALL-CORPORATE WHEN OTHER PERFORM PROCESS-UNKNOWN-CUSTOMER END-EVALUATE.

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

Conditional Expressions

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
*> Relational conditions IF CUSTOMER-BALANCE > 1000 DISPLAY 'High balance customer' END-IF. *> Class conditions IF CUSTOMER-ID IS NUMERIC DISPLAY 'Valid numeric ID' END-IF. *> Sign conditions IF TRANSACTION-AMOUNT IS POSITIVE DISPLAY 'Credit transaction' END-IF. *> Complex conditions IF (CUSTOMER-TYPE = 'PREMIUM' AND CUSTOMER-BALANCE > 5000) OR (CUSTOMER-TYPE = 'GOLD' AND CUSTOMER-BALANCE > 2000) DISPLAY 'Eligible for special service' END-IF.

Use various condition types: relational (>, <, =), class (NUMERIC, ALPHABETIC), sign (POSITIVE, NEGATIVE, ZERO), and complex conditions with AND, OR, NOT operators.

Conditional Loops

cobol
1
2
3
4
5
6
7
8
9
10
11
*> Conditional loop with IF PERFORM UNTIL EOF OR ERROR-FLAG = 'Y' READ INPUT-FILE AT END SET EOF TO TRUE NOT AT END IF RECORD-TYPE = 'VALID' PERFORM PROCESS-VALID-RECORD ELSE DISPLAY 'Invalid record: ' RECORD-TYPE SET ERROR-FLAG TO TRUE END-IF END-PERFORM.

Use conditional statements within loops to control processing based on data conditions. Check for both normal termination (EOF) and error conditions to provide robust error handling.

Early Exit Patterns

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
*> Early exit with error handling IF CUSTOMER-ID = 0 DISPLAY 'Error: Invalid customer ID' PERFORM ERROR-HANDLING EXIT PARAGRAPH END-IF. IF CUSTOMER-STATUS = 'CLOSED' DISPLAY 'Error: Customer account closed' PERFORM ERROR-HANDLING EXIT PARAGRAPH END-IF. *> Main processing continues here DISPLAY 'Processing valid customer' PERFORM MAIN-PROCESSING.

Use early exit patterns to handle error conditions first, reducing nesting levels. Check for invalid conditions early and exit using EXIT PARAGRAPH or EXIT SECTION.