MainframeMaster

COBOL Control Structures

Control structures organize program logic and control execution flow. Learn IF-THEN-ELSE, EVALUATE-WHEN, PERFORM loops, and structured programming patterns for maintainable, readable code.

Selection Structures

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
*> Simple selection with IF-THEN-ELSE 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. *> Multi-way selection with EVALUATE 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.

Selection structures choose between different execution paths. Use IF-THEN-ELSE for binary decisions and EVALUATE-WHEN for multi-way decisions. Always close with END-IF or END-EVALUATE.

Iteration Structures

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
*> Conditional loop with PERFORM UNTIL PERFORM UNTIL EOF OR ERROR-FLAG = 'Y' READ INPUT-FILE AT END SET EOF TO TRUE NOT AT END PERFORM PROCESS-RECORD END-READ END-PERFORM. *> Counted loop with PERFORM VARYING PERFORM VARYING COUNTER FROM 1 BY 1 UNTIL COUNTER > MAX-RECORDS DISPLAY 'Processing record ' COUNTER PERFORM PROCESS-RECORD END-PERFORM. *> Fixed iteration with PERFORM TIMES PERFORM PROCESS-BATCH TIMES BATCH-SIZE PERFORM PROCESS-RECORD END-PERFORM.

Iteration structures repeat code execution. PERFORM UNTIL continues until a condition becomes true. PERFORM VARYING manages counters automatically. PERFORM TIMES executes a fixed number of times.

Nested Control Structures

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
PERFORM UNTIL EOF READ INPUT-FILE AT END SET EOF TO TRUE NOT AT END EVALUATE RECORD-TYPE WHEN 'HEADER' IF HEADER-VALID = 'Y' PERFORM PROCESS-HEADER ELSE DISPLAY 'Invalid header record' END-IF WHEN 'DETAIL' PERFORM VARYING I FROM 1 BY 1 UNTIL I > DETAIL-COUNT PERFORM PROCESS-DETAIL-FIELD END-PERFORM WHEN 'TRAILER' PERFORM PROCESS-TRAILER WHEN OTHER DISPLAY 'Unknown record type: ' RECORD-TYPE END-EVALUATE END-READ END-PERFORM.

Nest control structures for complex logic. Combine loops with selection structures. Use proper indentation to show nesting levels. Each structure must be properly closed with its corresponding END statement.

Structured Programming Principles

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
*> Single entry, single exit procedures PROCESS-CUSTOMER-RECORD. PERFORM VALIDATE-CUSTOMER-DATA IF VALIDATION-SUCCESSFUL PERFORM UPDATE-CUSTOMER-RECORD PERFORM LOG-SUCCESSFUL-UPDATE ELSE PERFORM LOG-VALIDATION-ERROR END-IF EXIT PARAGRAPH. *> Avoid GO TO statements *> Bad: GO TO ERROR-HANDLER *> Good: PERFORM ERROR-HANDLER *> Use meaningful procedure names VALIDATE-CUSTOMER-DATA. MOVE 'Y' TO VALIDATION-SUCCESSFUL IF CUSTOMER-ID = 0 MOVE 'N' TO VALIDATION-SUCCESSFUL END-IF.

Follow structured programming principles: single entry/exit points, avoid GO TO statements, use meaningful procedure names, and organize code into logical procedures. This improves maintainability and readability.

Control Structure Patterns

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
*> Guard clause pattern IF CUSTOMER-ID = 0 DISPLAY 'Error: Invalid customer ID' EXIT PARAGRAPH END-IF. IF CUSTOMER-STATUS = 'CLOSED' DISPLAY 'Error: Customer account closed' EXIT PARAGRAPH END-IF. *> Main processing continues here PERFORM PROCESS-VALID-CUSTOMER. *> Switch pattern with EVALUATE 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' PERFORM PROCESS-CORPORATE WHEN OTHER PERFORM PROCESS-UNKNOWN-TYPE END-EVALUATE.

Use common control structure patterns: guard clauses for early validation, switch patterns for complex decisions, and structured loops for data processing. These patterns improve code clarity and maintainability.

Error Handling Structures

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 'Error: Invalid record type: ' RECORD-TYPE SET ERROR-FLAG TO TRUE END-IF END-READ END-PERFORM. IF ERROR-FLAG = 'Y' DISPLAY 'Processing stopped due to errors' PERFORM ERROR-HANDLING ELSE DISPLAY 'Processing completed successfully' PERFORM SUCCESS-HANDLING END-IF.

Implement robust error handling using control structures. Check for error conditions in loops and provide appropriate error handling. Use flags to track error states and provide meaningful error messages.

Performance Considerations

cobol
1
2
3
4
5
6
7
8
9
10
11
*> Efficient loop structure PERFORM VARYING I FROM 1 BY 1 UNTIL I > RECORD-COUNT IF RECORD-TYPE(I) = 'TARGET-TYPE' PERFORM PROCESS-TARGET-RECORD END-IF END-PERFORM. *> Avoid nested loops when possible *> Instead of nested loops, use separate processing PERFORM PROCESS-FIRST-PASS PERFORM PROCESS-SECOND-PASS.

Design control structures for performance. Minimize nested loops, use efficient loop conditions, and consider separating complex logic into multiple passes. Profile performance-critical sections.

Control Structure Best Practices

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
*> Clear, readable structure IF CUSTOMER-BALANCE > 1000 IF CUSTOMER-TYPE = 'PREMIUM' PERFORM PROCESS-PREMIUM-CUSTOMER ELSE PERFORM PROCESS-STANDARD-CUSTOMER END-IF ELSE PERFORM PROCESS-LOW-BALANCE-CUSTOMER END-IF. *> Use meaningful variable names IF HIGH-VALUE-CUSTOMER AND PREMIUM-STATUS PERFORM PROCESS-VIP-CUSTOMER END-IF.

Follow best practices: use clear, readable structures; choose meaningful variable names; avoid deep nesting; use consistent indentation; and document complex logic with comments.