MainframeMaster

COBOL Tutorial

COBOL ENDING Statement

The ENDING statement represents a fundamental component of program termination and cleanup operations in COBOL, serving as a mechanism for performing finalization procedures and resource cleanup before program termination. This statement embodies modern programming principles by providing structured termination points for program execution, enabling sophisticated cleanup operations for system resources, and supporting the development of robust applications that require precise control over program finalization and resource management procedures.

Basic ENDING Usage

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
IDENTIFICATION DIVISION. PROGRAM-ID. ENDING-DEMO. DATA DIVISION. WORKING-STORAGE SECTION. 01 PROGRAM-CONTROLS. 05 OPERATION-COUNT PIC 9(7) VALUE 0. 05 ERROR-COUNT PIC 9(5) VALUE 0. 05 WARNING-COUNT PIC 9(5) VALUE 0. 05 CLEANUP-STATUS PIC X VALUE 'N'. 88 CLEANUP-COMPLETE VALUE 'Y'. 88 CLEANUP-PENDING VALUE 'N'. 01 RESOURCE-MANAGEMENT. 05 FILES-OPENED PIC 9(3) VALUE 0. 05 MEMORY-ALLOCATED PIC 9(7) VALUE 0. 05 CONNECTIONS-ACTIVE PIC 9(3) VALUE 0. PROCEDURE DIVISION. MAIN-PROCESSING. PERFORM INITIALIZE-PROGRAM PERFORM EXECUTE-BUSINESS-LOGIC PERFORM CLEANUP-RESOURCES STOP RUN. INITIALIZE-PROGRAM. DISPLAY 'Program starting...' MOVE 0 TO OPERATION-COUNT MOVE 0 TO ERROR-COUNT SET CLEANUP-PENDING TO TRUE. EXECUTE-BUSINESS-LOGIC. DISPLAY 'Executing business logic...' PERFORM VARYING OPERATION-COUNT FROM 1 BY 1 UNTIL OPERATION-COUNT > 100 *> Simulate business operations IF FUNCTION MOD(OPERATION-COUNT, 10) = 0 DISPLAY 'Checkpoint: ' OPERATION-COUNT ' operations completed' END-IF *> Simulate occasional errors IF FUNCTION MOD(OPERATION-COUNT, 25) = 0 ADD 1 TO ERROR-COUNT DISPLAY 'Error occurred at operation: ' OPERATION-COUNT END-IF END-PERFORM. CLEANUP-RESOURCES. DISPLAY 'Performing cleanup operations...' *> Close any open files IF FILES-OPENED > 0 DISPLAY 'Closing ' FILES-OPENED ' open files' MOVE 0 TO FILES-OPENED END-IF *> Free allocated memory IF MEMORY-ALLOCATED > 0 DISPLAY 'Freeing ' MEMORY-ALLOCATED ' bytes of memory' MOVE 0 TO MEMORY-ALLOCATED END-IF *> Close network connections IF CONNECTIONS-ACTIVE > 0 DISPLAY 'Closing ' CONNECTIONS-ACTIVE ' active connections' MOVE 0 TO CONNECTIONS-ACTIVE END-IF SET CLEANUP-COMPLETE TO TRUE DISPLAY 'Program ending statistics:' DISPLAY 'Operations completed: ' OPERATION-COUNT DISPLAY 'Errors encountered: ' ERROR-COUNT DISPLAY 'Warnings generated: ' WARNING-COUNT DISPLAY 'Cleanup status: Complete'.

Best Practices and FAQ