The PROCEDURE DIVISION is the fourth and final division in a COBOL program. It contains all the executable statements that perform the actual work of the program. This division is where the business logic is implemented through various COBOL statements and commands.
12PROCEDURE DIVISION. executable statements...
The simplest form with no parameters or return value.
12PROCEDURE DIVISION USING parameter-1 [parameter-2 ...]. executable statements...
Used when the program receives parameters from a calling program. The parameters must be defined in the LINKAGE SECTION.
12PROCEDURE DIVISION RETURNING return-item. executable statements...
Used when the program returns a value to the calling program.
123PROCEDURE DIVISION USING parameter-1 [parameter-2 ...] RETURNING return-item. executable statements...
Used when the program both receives parameters and returns a value.
The PROCEDURE DIVISION is typically organized into sections and paragraphs. These organizational units help structure the code and make it more readable and maintainable.
12345MAIN-LOGIC SECTION. PERFORM INITIALIZATION. PERFORM PROCESS-DATA UNTIL END-OF-FILE. PERFORM CLEAN-UP. STOP RUN.
123456INITIALIZATION. OPEN INPUT CUSTOMER-FILE. OPEN OUTPUT REPORT-FILE. READ CUSTOMER-FILE AT END SET END-OF-FILE TO TRUE END-READ.
12345678910111213141516171819202122232425262728293031323334353637383940414243PROCEDURE DIVISION. MAIN-CONTROL SECTION. MAIN-PARAGRAPH. PERFORM INITIALIZATION. PERFORM PROCESS-RECORDS UNTIL END-OF-FILE. PERFORM TERMINATION. STOP RUN. FILE-HANDLING SECTION. INITIALIZATION. OPEN INPUT DATA-FILE. OPEN OUTPUT REPORT-FILE. PERFORM READ-RECORD. READ-RECORD. READ DATA-FILE AT END SET END-OF-FILE TO TRUE END-READ. TERMINATION. CLOSE DATA-FILE. CLOSE REPORT-FILE. DATA-PROCESSING SECTION. PROCESS-RECORDS. PERFORM PROCESS-CURRENT-RECORD. PERFORM READ-RECORD. PROCESS-CURRENT-RECORD. IF RECORD-TYPE = "CUSTOMER" PERFORM PROCESS-CUSTOMER ELSE PERFORM PROCESS-ORDER END-IF. PROCESS-CUSTOMER. MOVE CUSTOMER-ID TO REPORT-CUSTOMER-ID. MOVE CUSTOMER-NAME TO REPORT-CUSTOMER-NAME. WRITE REPORT-RECORD. PROCESS-ORDER. ADD ORDER-AMOUNT TO TOTAL-ORDERS. WRITE REPORT-RECORD.
This example shows a typical PROCEDURE DIVISION organized into sections and paragraphs, with a clear flow of control.
Within paragraphs, COBOL code is organized into sentences and statements. Understanding these elements is crucial for writing and reading COBOL code effectively.
12345READ CUSTOMER-FILE. *> This is a sentence with one statement MOVE CUSTOMER-NAME TO REPORT-NAME ADD 1 TO RECORD-COUNT. *> This is a sentence with two statements
1234MOVE SPACES TO CUSTOMER-NAME. READ CUSTOMER-FILE. ADD ITEM-AMOUNT TO TOTAL-AMOUNT. IF AMOUNT > 1000 PERFORM PROCESS-LARGE-ORDER.
Modern COBOL uses scope terminators to clearly indicate the end of statements, especially for nested statements. This improves code readability and reduces errors caused by ambiguous statement boundaries.
123456IF AMOUNT > 1000 IF CUSTOMER-TYPE = "PREMIUM" MULTIPLY AMOUNT BY 0.90 MOVE "DISCOUNT APPLIED" TO REPORT-MESSAGE ELSE MOVE "NO DISCOUNT" TO REPORT-MESSAGE.
Without scope terminators, it's not immediately clear which IF statement the ELSE belongs to.
12345678IF AMOUNT > 1000 IF CUSTOMER-TYPE = "PREMIUM" MULTIPLY AMOUNT BY 0.90 MOVE "DISCOUNT APPLIED" TO REPORT-MESSAGE ELSE MOVE "NO DISCOUNT" TO REPORT-MESSAGE END-IF END-IF.
With scope terminators, the structure is clear and unambiguous.
Well-organized COBOL programs are easier to understand, maintain, and enhance. There are several strategies for organizing the PROCEDURE DIVISION effectively.
12345MAIN-CONTROL. PERFORM INITIALIZATION. PERFORM PROCESS-DATA UNTIL END-OF-FILE. PERFORM FINALIZATION. STOP RUN.
12345678FILE-HANDLING SECTION. file handling paragraphs... DATA-VALIDATION SECTION. data validation paragraphs... REPORT-GENERATION SECTION. report generation paragraphs...
123456MAIN-PROGRAM SECTION. MAIN-CONTROL. CALL "INITIALIZE-FILES" USING FILE-STATUS. CALL "PROCESS-RECORDS" USING CUSTOMER-FILE, REPORT-FILE. CALL "GENERATE-SUMMARY" USING REPORT-FILE, SUMMARY-COUNTS. STOP RUN.
Practice analyzing and creating PROCEDURE DIVISION code with the following exercise:
Identify the sections, paragraphs, sentences, and statements in the following PROCEDURE DIVISION code:
123456789101112131415161718192021222324252627282930PROCEDURE DIVISION. MAIN-LOGIC SECTION. MAIN-PARAGRAPH. OPEN INPUT CUSTOMER-FILE. OPEN OUTPUT REPORT-FILE. READ CUSTOMER-FILE AT END SET END-OF-FILE TO TRUE END-READ. PERFORM UNTIL END-OF-FILE PERFORM PROCESS-CUSTOMER READ CUSTOMER-FILE AT END SET END-OF-FILE TO TRUE END-READ END-PERFORM. PERFORM WRAP-UP. STOP RUN. PROCESS-CUSTOMER. IF CUSTOMER-STATUS = "A" MOVE CUSTOMER-ID TO REPORT-ID MOVE CUSTOMER-NAME TO REPORT-NAME WRITE REPORT-RECORD END-IF. WRAP-UP. CLOSE CUSTOMER-FILE. CLOSE REPORT-FILE. DISPLAY "Processing complete".
Refactor the following code to use modern structured programming with scope terminators:
123456789101112131415161718PROCEDURE DIVISION. PROCESS-PARAGRAPH. OPEN INPUT DATA-FILE. READ DATA-FILE AT END MOVE "Y" TO EOF-FLAG. PERFORM UNTIL EOF-FLAG = "Y" IF RECORD-TYPE = "C" MOVE CUST-NAME TO OUTPUT-NAME IF CUST-BALANCE > 1000 MOVE "HIGH BALANCE" TO OUTPUT-MESSAGE ELSE MOVE "NORMAL" TO OUTPUT-MESSAGE WRITE OUTPUT-RECORD ELSE IF RECORD-TYPE = "O" ADD ORDER-AMOUNT TO TOTAL-ORDERS. READ DATA-FILE AT END MOVE "Y" TO EOF-FLAG. CLOSE DATA-FILE. STOP RUN.
Write a PROCEDURE DIVISION for a program that reads a customer file and categorizes customers into three groups based on their account balance:
The program should create a report file with customer details and their category, and at the end, display the count of customers in each category.
1. What is the main purpose of the PROCEDURE DIVISION in a COBOL program?
2. Which of the following is the correct hierarchical order in the PROCEDURE DIVISION (from highest to lowest)?
3. What is the purpose of scope terminators in COBOL?
4. Which of the following is NOT a valid PROCEDURE DIVISION header format?
5. What statement is typically used to transfer control to a paragraph in the PROCEDURE DIVISION?