The PROCEDURE DIVISION: Where COBOL Comes to Life




The PROCEDURE DIVISION: Where COBOL Comes to Life


Introduction

The PROCEDURE DIVISION is the core of any COBOL program. It's where the logic resides, actions are defined, and data flows are controlled. While other divisions set the stage by defining variables and external interactions, the PROCEDURE DIVISION executes the instructions, making things happen.


Whether you're displaying text, performing calculations, or processing files, the PROCEDURE DIVISION handles it all. Let’s explore its capabilities through three examples of increasing complexity.


Example 1: A Simple Display Program

This example demonstrates a straightforward PROCEDURE DIVISION with only text output. It’s ideal for beginners looking to understand basic COBOL syntax.


       IDENTIFICATION DIVISION.
       PROGRAM-ID. DisplayExample.

       PROCEDURE DIVISION.
           DISPLAY "----------------------------------------"
           DISPLAY "Hello, World! This is COBOL in action."
           DISPLAY "----------------------------------------"
           STOP RUN.


Expected Output:


----------------------------------------
Hello, World! This is COBOL in action.
----------------------------------------


What’s Happening:

  • The DISPLAY statement outputs text to the console.

  • STOP RUN terminates the program gracefully.


This simple example shows how to start building logic in the PROCEDURE DIVISION.


Example 2: Adding Computation

Now we introduce arithmetic, illustrating how the PROCEDURE DIVISION handles calculations.


       IDENTIFICATION DIVISION.
       PROGRAM-ID. ComputeExample.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 NUM1 PIC 9(2) VALUE 10.
       01 NUM2 PIC 9(2) VALUE 20.
       01 RESULT PIC 9(3).

       PROCEDURE DIVISION.
           COMPUTE RESULT = NUM1 + NUM2
           DISPLAY "----------------------------------------"
           DISPLAY "The sum of " NUM1 " and " NUM2 " is " RESULT
           DISPLAY "----------------------------------------"
           STOP RUN.


Expected Output:


----------------------------------------
The sum of 10 and 20 is 30
----------------------------------------


What’s Happening:

  • Variables are defined in the WORKING-STORAGE SECTION to hold values for the computation.

  • The COMPUTE statement adds NUM1 and NUM2, storing the result in RESULT.

  • DISPLAY outputs the operation and the result.


This demonstrates how COBOL manages arithmetic and integrates variables seamlessly.


Example 3: Display, Computation, and File I/O

This final example combines text output, arithmetic, and file processing to show the PROCEDURE DIVISION in full action.


       IDENTIFICATION DIVISION.
       PROGRAM-ID. FileExample.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
           FILE-CONTROL.
               SELECT EMPLOYEE-FILE ASSIGN TO "employee.dat".

       DATA DIVISION.
       FILE SECTION.
           FD EMPLOYEE-FILE.
           01 EMPLOYEE-RECORD.
               05 EMPLOYEE-ID      PIC 9(5).
               05 EMPLOYEE-NAME    PIC X(20).
               05 EMPLOYEE-SALARY  PIC 9(6)V99.

       WORKING-STORAGE SECTION.
           01 BONUS               PIC 9(5)V99 VALUE 500.00.

       PROCEDURE DIVISION.
           OPEN INPUT EMPLOYEE-FILE
           READ EMPLOYEE-FILE
               AT END
                   DISPLAY "------------------------------------------"
                   DISPLAY "No records found."
                   DISPLAY "------------------------------------------"
                   STOP RUN
               NOT AT END
                   DISPLAY "------------------------------------------"
                   DISPLAY "Employee: " EMPLOYEE-NAME
                   DISPLAY "Base Salary: $" EMPLOYEE-SALARY
                   COMPUTE EMPLOYEE-SALARY = EMPLOYEE-SALARY + BONUS
                   DISPLAY "Total Salary (with bonus): $" EMPLOYEE-SALARY
                   DISPLAY "------------------------------------------"
           CLOSE EMPLOYEE-FILE
           STOP RUN.


Expected Output (for one record):


------------------------------------------
Employee: John Doe
Base Salary: $50000.00
Total Salary (with bonus): $50500.00
------------------------------------------


What’s Happening:

  • The program opens a file (employee.dat) for input and reads the first record.

  • The READ statement retrieves data into the EMPLOYEE-RECORD structure.

  • If the file is not empty, the program:
    • Displays the employee’s name and base salary.
    • Calculates the total salary by adding a bonus.
    • Displays the updated salary.

  • If no records are found, the program exits with a message.


This example highlights how PROCEDURE DIVISION orchestrates file processing alongside computation and text display.


Conclusion

The PROCEDURE DIVISION is where COBOL programs come to life. From simple text output to intricate computations and file operations, this division drives the logic and execution. These three examples demonstrate its flexibility and power, building from beginner-friendly to more advanced functionality.



Image: SoftRadix Technologies from Pixabay

Comments

Popular posts from this blog

The New ChatGPT Reason Feature: What It Is and Why You Should Use It

Raspberry Pi Connect vs. RealVNC: A Comprehensive Comparison

The Reasoning Chain in DeepSeek R1: A Glimpse into AI’s Thought Process