MainframeMaster

COBOL Control Flow

Control flow determines the order of statement execution in COBOL programs. Learn sequential execution, branching, looping, and procedure calls for structured, maintainable programs.

Sequential Execution

cobol
1
2
3
4
5
6
7
8
PROCEDURE DIVISION. MAIN-PROCEDURE. DISPLAY 'Program starting' PERFORM INITIALIZE-DATA PERFORM PROCESS-RECORDS PERFORM FINALIZE-PROCESSING DISPLAY 'Program completed' STOP RUN.

COBOL executes statements sequentially by default. Statements are processed in the order they appear unless control flow statements change the execution path. Use meaningful paragraph names for clarity.

Branching with IF Statements

cobol
1
2
3
4
5
6
7
8
9
10
IF CUSTOMER-BALANCE > 0 DISPLAY 'Customer has positive balance' PERFORM PROCESS-POSITIVE-BALANCE ELSE DISPLAY 'Customer has zero or negative balance' PERFORM PROCESS-NEGATIVE-BALANCE END-IF. *> Continue with next statement DISPLAY 'Balance processing completed'.

Use IF statements to branch execution based on conditions. The condition determines which path executes. After the IF statement completes, execution continues with the next sequential statement.

Multi-Way Branching with EVALUATE

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
EVALUATE TRANSACTION-TYPE WHEN 'DEPOSIT' PERFORM PROCESS-DEPOSIT WHEN 'WITHDRAWAL' PERFORM PROCESS-WITHDRAWAL WHEN 'TRANSFER' PERFORM PROCESS-TRANSFER WHEN OTHER PERFORM PROCESS-INVALID-TRANSACTION END-EVALUATE. *> Continue with next statement PERFORM UPDATE-ACCOUNT-SUMMARY.

EVALUATE provides multi-way branching based on a single variable or complex conditions. Each WHEN clause handles a specific case. WHEN OTHER catches unmatched values. Execution continues after END-EVALUATE.

Looping with PERFORM UNTIL

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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-READ END-PERFORM. *> Loop completed, continue with next statement PERFORM CLOSE-FILES.

PERFORM UNTIL creates loops that continue until a condition becomes true. The loop condition is checked before each iteration. Multiple conditions can be combined with AND/OR operators.

Looping with PERFORM VARYING

cobol
1
2
3
4
5
6
7
8
PERFORM VARYING COUNTER FROM 1 BY 1 UNTIL COUNTER > MAX-RECORDS DISPLAY 'Processing record ' COUNTER PERFORM PROCESS-RECORD END-PERFORM. *> Loop completed, continue with next statement DISPLAY 'All records processed'.

PERFORM VARYING creates counted loops with automatic counter management. The counter is initialized, incremented, and tested automatically. Use this for processing a known number of iterations.

Procedure Calls with PERFORM

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
MAIN-PROCEDURE. PERFORM INITIALIZE-PROGRAM PERFORM PROCESS-DATA PERFORM FINALIZE-PROGRAM STOP RUN. INITIALIZE-PROGRAM. MOVE 0 TO RECORD-COUNT OPEN INPUT INPUT-FILE DISPLAY 'Program initialized'. PROCESS-DATA. PERFORM UNTIL EOF READ INPUT-FILE AT END SET EOF TO TRUE NOT AT END ADD 1 TO RECORD-COUNT PERFORM PROCESS-RECORD END-READ END-PERFORM. FINALIZE-PROGRAM. CLOSE INPUT-FILE DISPLAY 'Records processed: ' RECORD-COUNT.

Use PERFORM to call procedures (paragraphs). This creates structured programs with clear separation of concerns. Each procedure has a specific purpose and can be called multiple times.

Nested Control Flow

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PERFORM UNTIL EOF READ INPUT-FILE AT END SET EOF TO TRUE NOT AT END EVALUATE RECORD-TYPE WHEN 'HEADER' PERFORM PROCESS-HEADER WHEN 'DETAIL' PERFORM PROCESS-DETAIL WHEN 'TRAILER' PERFORM PROCESS-TRAILER WHEN OTHER DISPLAY 'Unknown record type: ' RECORD-TYPE END-EVALUATE END-READ END-PERFORM.

Nest control flow structures for complex logic. Combine loops with conditional statements. Each structure has its own scope and execution flow. Use proper indentation to show nesting levels.

Control Flow Best Practices

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
*> Good: Clear, structured flow MAIN-PROCEDURE. PERFORM INITIALIZE PERFORM PROCESS-DATA PERFORM CLEANUP STOP RUN. *> Avoid: Excessive nesting *> IF condition1 *> IF condition2 *> IF condition3 *> PERFORM complex-logic *> END-IF *> END-IF *> END-IF *> Better: Use EVALUATE or extract procedures EVALUATE TRUE WHEN condition1 AND condition2 AND condition3 PERFORM complex-logic WHEN OTHER PERFORM alternative-logic END-EVALUATE.

Keep control flow simple and readable. Avoid deep nesting by using EVALUATE statements or extracting complex logic into separate procedures. Use meaningful names for procedures and variables.