MainframeMaster

COBOL Tutorial

COBOL END Statement

The END statement in COBOL represents a fundamental paradigm shift toward structured programming, serving as an explicit scope terminator that provides clear, unambiguous boundaries for programming constructs. Far more than a simple syntactic element, the END statement embodies modern programming principles by eliminating the ambiguity inherent in older COBOL coding practices, improving code readability, enhancing maintainability, and reducing the likelihood of logic errors that can plague complex business applications in enterprise environments.

In contemporary COBOL development, the END statement is indispensable for creating robust, maintainable applications that can withstand the rigors of enterprise-scale development and maintenance. It provides the structural foundation for nested programming constructs, exception handling, and complex business logic implementation while ensuring that code remains readable and debuggable throughout its lifecycle. Understanding and properly utilizing END statements is essential for any serious COBOL developer working in modern enterprise environments.

Understanding END Statement Architecture

The END statement architecture in COBOL implements a sophisticated scope management system that provides explicit boundaries for programming constructs. This architecture addresses one of the most significant challenges in traditional COBOL programming: the ambiguity that arises when multiple nested constructs are used without clear termination points. By providing explicit scope terminators, END statements create a hierarchical structure that mirrors the logical flow of business processes.

The design philosophy behind END statements reflects modern software engineering principles, emphasizing clarity, maintainability, and error prevention. Each END statement corresponds to a specific COBOL verb or construct, creating a paired relationship that makes the code structure immediately apparent to developers, maintainers, and automated analysis tools. This pairing relationship is crucial for understanding program flow and identifying potential logic errors.

END statements also play a crucial role in compiler optimization and error detection. Modern COBOL compilers can perform more sophisticated analysis when scope boundaries are explicitly defined, leading to better optimization opportunities and more accurate error reporting. This capability is particularly important in enterprise environments where performance and reliability are paramount concerns.

Comprehensive END Statement Categories:

  • Conditional END Statements: END-IF, END-EVALUATE provide explicit termination for conditional logic constructs, eliminating ambiguity in nested conditional statements and improving code readability.
  • Iterative END Statements: END-PERFORM, END-SEARCH terminate loop and search constructs, providing clear boundaries for repetitive operations and making loop logic more maintainable.
  • Arithmetic END Statements: END-ADD, END-SUBTRACT, END-MULTIPLY, END-DIVIDE, END-COMPUTE provide explicit termination for arithmetic operations with error handling capabilities.
  • I/O END Statements: END-READ, END-WRITE, END-REWRITE, END-DELETE terminate file operation constructs, enabling sophisticated error handling and status checking.
  • String Manipulation END Statements: END-STRING, END-UNSTRING, END-INSPECT provide boundaries for string processing operations with comprehensive error handling.
  • Call END Statements: END-CALL, END-INVOKE terminate subprogram and method invocation constructs, supporting modern object-oriented and modular programming practices.
  • Transaction END Statements: END-RECEIVE, END-SEND, END-RETURN terminate communication and transaction processing constructs for enterprise integration.
  • Data Manipulation END Statements: END-SET, END-INITIALIZE, END-MOVE provide explicit boundaries for data manipulation operations with enhanced error handling.

Structured Programming Benefits

The introduction of END statements in COBOL represents a significant evolution toward structured programming principles that have proven essential for developing maintainable enterprise applications. These benefits extend far beyond simple syntax improvements to encompass fundamental changes in how COBOL programs are designed, implemented, and maintained throughout their lifecycle.

Structured programming with END statements enables developers to create more modular, readable code that can be easily understood by team members and maintained over time. This is particularly important in enterprise environments where applications may be maintained for decades and worked on by multiple generations of developers. The explicit scope boundaries provided by END statements make it easier to understand program logic and identify potential issues.

The use of END statements also facilitates better testing and debugging practices by providing clear boundaries for unit testing and making it easier to isolate and identify the source of runtime errors. This improved testability is crucial for maintaining the high reliability standards expected in enterprise COBOL applications.

Modern COBOL Development Practices

In modern COBOL development environments, END statements are considered essential for creating professional-quality code that meets contemporary software engineering standards. They enable developers to implement complex business logic while maintaining code clarity and reducing the likelihood of errors that can be costly in production environments.

The consistent use of END statements throughout a COBOL application creates a uniform coding style that improves team productivity and reduces the learning curve for new team members. This consistency is particularly valuable in large development organizations where multiple teams may work on different components of the same application system.

END statements also support modern development practices such as code reviews, automated testing, and continuous integration by making code structure more explicit and analyzable by both human reviewers and automated tools. This capability is increasingly important as organizations adopt DevOps practices for COBOL application development.

END Statement Implementation Patterns

Basic END Statement Usage

The fundamental implementation of END statements follows a consistent pattern throughout COBOL, where each construct that can contain multiple statements or complex logic is terminated with its corresponding END statement. This pattern creates a clear, hierarchical structure that makes program logic immediately apparent to developers and maintainers.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
IDENTIFICATION DIVISION. PROGRAM-ID. END-STATEMENT-BASICS. DATA DIVISION. WORKING-STORAGE SECTION. *> Comprehensive demonstration of basic END statement usage *> Showing proper structured programming techniques 01 EMPLOYEE-RECORD. 05 EMPLOYEE-ID PIC X(10). 05 EMPLOYEE-NAME PIC X(30). 05 EMPLOYEE-SALARY PIC 9(7)V99. 05 EMPLOYEE-DEPARTMENT PIC X(10). 05 EMPLOYEE-STATUS PIC X(10). 01 CALCULATION-FIELDS. 05 GROSS-PAY PIC 9(7)V99. 05 NET-PAY PIC 9(7)V99. 05 TAX-AMOUNT PIC 9(7)V99. 05 BONUS-AMOUNT PIC 9(7)V99. 01 CONTROL-FLAGS. 05 VALID-EMPLOYEE-FLAG PIC X VALUE 'N'. 88 VALID-EMPLOYEE VALUE 'Y'. 88 INVALID-EMPLOYEE VALUE 'N'. 05 CALCULATION-ERROR-FLAG PIC X VALUE 'N'. 88 CALCULATION-ERROR VALUE 'Y'. 88 CALCULATION-OK VALUE 'N'. 01 CONSTANTS. 05 TAX-RATE PIC V999 VALUE .285. 05 BONUS-THRESHOLD PIC 9(7)V99 VALUE 50000.00. 05 MAX-SALARY PIC 9(7)V99 VALUE 999999.99. PROCEDURE DIVISION. MAIN-PROCESSING. PERFORM INITIALIZE-PROGRAM PERFORM PROCESS-EMPLOYEE-DATA PERFORM FINALIZE-PROGRAM STOP RUN. INITIALIZE-PROGRAM. DISPLAY 'Starting Employee Processing System' MOVE 'N' TO VALID-EMPLOYEE-FLAG MOVE 'N' TO CALCULATION-ERROR-FLAG MOVE ZEROS TO GROSS-PAY, NET-PAY, TAX-AMOUNT, BONUS-AMOUNT. PROCESS-EMPLOYEE-DATA. *> Demonstrate basic IF-END-IF structure IF EMPLOYEE-SALARY > ZERO SET VALID-EMPLOYEE TO TRUE PERFORM CALCULATE-GROSS-PAY PERFORM CALCULATE-NET-PAY PERFORM DISPLAY-RESULTS ELSE SET INVALID-EMPLOYEE TO TRUE DISPLAY 'Invalid employee salary: ' EMPLOYEE-SALARY END-IF. CALCULATE-GROSS-PAY. *> Demonstrate nested IF-END-IF with arithmetic END statements IF EMPLOYEE-SALARY > BONUS-THRESHOLD COMPUTE BONUS-AMOUNT = EMPLOYEE-SALARY * 0.10 ON SIZE ERROR SET CALCULATION-ERROR TO TRUE DISPLAY 'Error calculating bonus for: ' EMPLOYEE-ID END-COMPUTE ADD EMPLOYEE-SALARY TO BONUS-AMOUNT GIVING GROSS-PAY ON SIZE ERROR SET CALCULATION-ERROR TO TRUE DISPLAY 'Error calculating gross pay for: ' EMPLOYEE-ID END-ADD ELSE MOVE EMPLOYEE-SALARY TO GROSS-PAY END-IF. CALCULATE-NET-PAY. *> Demonstrate arithmetic operations with END statements MULTIPLY GROSS-PAY BY TAX-RATE GIVING TAX-AMOUNT ON SIZE ERROR SET CALCULATION-ERROR TO TRUE DISPLAY 'Error calculating tax for: ' EMPLOYEE-ID END-MULTIPLY SUBTRACT TAX-AMOUNT FROM GROSS-PAY GIVING NET-PAY ON SIZE ERROR SET CALCULATION-ERROR TO TRUE DISPLAY 'Error calculating net pay for: ' EMPLOYEE-ID END-SUBTRACT. DISPLAY-RESULTS. *> Demonstrate conditional display with proper END-IF usage IF VALID-EMPLOYEE AND NOT CALCULATION-ERROR DISPLAY 'Employee: ' EMPLOYEE-ID DISPLAY 'Gross Pay: ' GROSS-PAY DISPLAY 'Tax Amount: ' TAX-AMOUNT DISPLAY 'Net Pay: ' NET-PAY IF BONUS-AMOUNT > ZERO DISPLAY 'Bonus Amount: ' BONUS-AMOUNT END-IF ELSE DISPLAY 'Cannot display results due to errors' END-IF. FINALIZE-PROGRAM. DISPLAY 'Employee Processing Complete'.

Advanced END Statement Patterns

Advanced END statement patterns involve complex nested structures, error handling scenarios, and sophisticated business logic implementation. These patterns demonstrate how END statements enable the creation of robust, maintainable code that can handle the complex requirements of enterprise business applications.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
IDENTIFICATION DIVISION. PROGRAM-ID. ADVANCED-END-PATTERNS. DATA DIVISION. WORKING-STORAGE SECTION. *> Advanced END statement patterns for complex business logic *> Demonstrating nested structures and error handling 01 CUSTOMER-PROCESSING. 05 CUSTOMER-ID PIC X(10). 05 CUSTOMER-TYPE PIC X(10). 05 CUSTOMER-BALANCE PIC S9(9)V99. 05 CUSTOMER-CREDIT-LIMIT PIC 9(9)V99. 05 TRANSACTION-AMOUNT PIC S9(9)V99. 01 PROCESSING-CONTROLS. 05 TRANSACTION-COUNT PIC 9(5) VALUE ZERO. 05 ERROR-COUNT PIC 9(5) VALUE ZERO. 05 PROCESSED-COUNT PIC 9(5) VALUE ZERO. 05 BATCH-TOTAL PIC S9(11)V99 VALUE ZERO. 01 VALIDATION-FLAGS. 05 VALID-CUSTOMER-FLAG PIC X VALUE 'N'. 88 VALID-CUSTOMER VALUE 'Y'. 05 CREDIT-APPROVED-FLAG PIC X VALUE 'N'. 88 CREDIT-APPROVED VALUE 'Y'. 05 TRANSACTION-VALID-FLAG PIC X VALUE 'N'. 88 TRANSACTION-VALID VALUE 'Y'. 01 BUSINESS-RULES. 05 PREMIUM-CUSTOMER-LIMIT PIC 9(9)V99 VALUE 100000.00. 05 STANDARD-CUSTOMER-LIMIT PIC 9(9)V99 VALUE 50000.00. 05 BASIC-CUSTOMER-LIMIT PIC 9(9)V99 VALUE 10000.00. 05 OVERDRAFT-LIMIT PIC 9(9)V99 VALUE 5000.00. PROCEDURE DIVISION. MAIN-PROCESSING. PERFORM INITIALIZE-PROCESSING PERFORM PROCESS-CUSTOMER-TRANSACTIONS PERFORM GENERATE-SUMMARY-REPORT STOP RUN. INITIALIZE-PROCESSING. DISPLAY 'Advanced Transaction Processing System Started' MOVE ZEROS TO TRANSACTION-COUNT, ERROR-COUNT, PROCESSED-COUNT MOVE ZERO TO BATCH-TOTAL. PROCESS-CUSTOMER-TRANSACTIONS. *> Complex nested EVALUATE-END-EVALUATE with multiple conditions EVALUATE CUSTOMER-TYPE WHEN 'PREMIUM' MOVE PREMIUM-CUSTOMER-LIMIT TO CUSTOMER-CREDIT-LIMIT PERFORM VALIDATE-PREMIUM-CUSTOMER IF VALID-CUSTOMER PERFORM PROCESS-PREMIUM-TRANSACTION END-IF WHEN 'STANDARD' MOVE STANDARD-CUSTOMER-LIMIT TO CUSTOMER-CREDIT-LIMIT PERFORM VALIDATE-STANDARD-CUSTOMER IF VALID-CUSTOMER PERFORM PROCESS-STANDARD-TRANSACTION END-IF WHEN 'BASIC' MOVE BASIC-CUSTOMER-LIMIT TO CUSTOMER-CREDIT-LIMIT PERFORM VALIDATE-BASIC-CUSTOMER IF VALID-CUSTOMER PERFORM PROCESS-BASIC-TRANSACTION END-IF WHEN OTHER DISPLAY 'Invalid customer type: ' CUSTOMER-TYPE ADD 1 TO ERROR-COUNT END-EVALUATE. VALIDATE-PREMIUM-CUSTOMER. *> Nested IF-END-IF with complex business logic IF CUSTOMER-ID NOT = SPACES IF CUSTOMER-BALANCE > -OVERDRAFT-LIMIT IF TRANSACTION-AMOUNT <= CUSTOMER-CREDIT-LIMIT SET VALID-CUSTOMER TO TRUE SET CREDIT-APPROVED TO TRUE SET TRANSACTION-VALID TO TRUE ELSE DISPLAY 'Transaction exceeds credit limit for: ' CUSTOMER-ID SET VALID-CUSTOMER TO FALSE END-IF ELSE DISPLAY 'Customer overdraft limit exceeded: ' CUSTOMER-ID SET VALID-CUSTOMER TO FALSE END-IF ELSE DISPLAY 'Invalid customer ID' SET VALID-CUSTOMER TO FALSE END-IF. PROCESS-PREMIUM-TRANSACTION. *> Complex arithmetic operations with comprehensive error handling IF TRANSACTION-AMOUNT > ZERO ADD TRANSACTION-AMOUNT TO CUSTOMER-BALANCE ON SIZE ERROR DISPLAY 'Error processing credit transaction: ' CUSTOMER-ID ADD 1 TO ERROR-COUNT NOT ON SIZE ERROR ADD TRANSACTION-AMOUNT TO BATCH-TOTAL ON SIZE ERROR DISPLAY 'Batch total overflow detected' ADD 1 TO ERROR-COUNT NOT ON SIZE ERROR ADD 1 TO PROCESSED-COUNT DISPLAY 'Credit processed: ' CUSTOMER-ID ' Amount: ' TRANSACTION-AMOUNT END-ADD END-ADD ELSE COMPUTE CUSTOMER-BALANCE = CUSTOMER-BALANCE + TRANSACTION-AMOUNT ON SIZE ERROR DISPLAY 'Error processing debit transaction: ' CUSTOMER-ID ADD 1 TO ERROR-COUNT NOT ON SIZE ERROR IF CUSTOMER-BALANCE < -OVERDRAFT-LIMIT DISPLAY 'Warning: Customer approaching overdraft limit: ' CUSTOMER-ID END-IF ADD TRANSACTION-AMOUNT TO BATCH-TOTAL ON SIZE ERROR DISPLAY 'Batch total overflow detected' ADD 1 TO ERROR-COUNT NOT ON SIZE ERROR ADD 1 TO PROCESSED-COUNT DISPLAY 'Debit processed: ' CUSTOMER-ID ' Amount: ' TRANSACTION-AMOUNT END-ADD END-COMPUTE END-IF. PROCESS-STANDARD-TRANSACTION. *> Simplified processing for standard customers PERFORM VALIDATE-TRANSACTION-AMOUNT IF TRANSACTION-VALID PERFORM APPLY-TRANSACTION PERFORM UPDATE-BATCH-TOTALS ELSE ADD 1 TO ERROR-COUNT DISPLAY 'Transaction validation failed: ' CUSTOMER-ID END-IF. VALIDATE-TRANSACTION-AMOUNT. IF TRANSACTION-AMOUNT NOT = ZERO IF TRANSACTION-AMOUNT <= CUSTOMER-CREDIT-LIMIT SET TRANSACTION-VALID TO TRUE ELSE SET TRANSACTION-VALID TO FALSE DISPLAY 'Transaction amount exceeds limit: ' CUSTOMER-ID END-IF ELSE SET TRANSACTION-VALID TO FALSE DISPLAY 'Invalid transaction amount: ' CUSTOMER-ID END-IF. APPLY-TRANSACTION. ADD TRANSACTION-AMOUNT TO CUSTOMER-BALANCE ON SIZE ERROR DISPLAY 'Error applying transaction: ' CUSTOMER-ID ADD 1 TO ERROR-COUNT NOT ON SIZE ERROR ADD 1 TO PROCESSED-COUNT DISPLAY 'Transaction applied: ' CUSTOMER-ID END-ADD. UPDATE-BATCH-TOTALS. ADD TRANSACTION-AMOUNT TO BATCH-TOTAL ON SIZE ERROR DISPLAY 'Batch total calculation error' ADD 1 TO ERROR-COUNT NOT ON SIZE ERROR ADD 1 TO TRANSACTION-COUNT END-ADD. PROCESS-BASIC-TRANSACTION. *> Basic transaction processing with minimal validation IF TRANSACTION-AMOUNT > ZERO AND TRANSACTION-AMOUNT <= BASIC-CUSTOMER-LIMIT PERFORM APPLY-TRANSACTION PERFORM UPDATE-BATCH-TOTALS ELSE ADD 1 TO ERROR-COUNT DISPLAY 'Basic customer transaction rejected: ' CUSTOMER-ID END-IF. GENERATE-SUMMARY-REPORT. DISPLAY 'Transaction Processing Summary:' DISPLAY 'Total Transactions: ' TRANSACTION-COUNT DISPLAY 'Successfully Processed: ' PROCESSED-COUNT DISPLAY 'Errors Encountered: ' ERROR-COUNT DISPLAY 'Batch Total: ' BATCH-TOTAL.

END Statement Best Practices

Proper Usage
  • Always use END statements for nested constructs
  • Maintain consistent indentation with END statements
  • Use END statements for complex arithmetic operations
  • Include END statements in all new COBOL development
Common Mistakes
  • Omitting END statements in nested constructs
  • Inconsistent use of END statements
  • Poor indentation with END statements
  • Mixing old and new syntax styles

Interactive Tutorial

Hands-On END Statement Practice
Practice implementing END statements in various COBOL constructs

Exercise 1: Basic IF-END-IF

Complete the following code with proper END statements:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
IF CUSTOMER-BALANCE > ZERO DISPLAY 'Customer has positive balance' PERFORM PROCESS-PAYMENT *> Add END-IF here IF PAYMENT-AMOUNT > CUSTOMER-BALANCE DISPLAY 'Insufficient funds' PERFORM HANDLE-INSUFFICIENT-FUNDS ELSE SUBTRACT PAYMENT-AMOUNT FROM CUSTOMER-BALANCE DISPLAY 'Payment processed successfully' *> Add END-IF here

Exercise 2: Complex Nested Structure

Add appropriate END statements to this nested structure:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
EVALUATE CUSTOMER-TYPE WHEN 'PREMIUM' IF TRANSACTION-AMOUNT > PREMIUM-LIMIT DISPLAY 'Amount exceeds premium limit' ELSE PERFORM PROCESS-PREMIUM-TRANSACTION *> Add END-IF here WHEN 'STANDARD' COMPUTE NET-AMOUNT = TRANSACTION-AMOUNT * 0.95 ON SIZE ERROR DISPLAY 'Calculation error' *> Add END-COMPUTE here WHEN OTHER DISPLAY 'Invalid customer type' *> Add END-EVALUATE here

Knowledge Check Quiz

Test Your Understanding

Frequently Asked Questions

Related Concepts

IF Statement

Conditional execution construct that benefits significantly from END-IF termination

PERFORM Statement

Loop and procedure execution statement that uses END-PERFORM for clarity

EVALUATE Statement

Multi-way selection statement that requires END-EVALUATE for proper scope