BudiBadu Logo
Samplebadu

COBOL by Example: Divisions

COBOL 2002

Exploring the detailed purpose and organization of each COBOL division including sections within divisions, understanding CONFIGURATION SECTION and INPUT-OUTPUT SECTION in the ENVIRONMENT DIVISION, working with WORKING-STORAGE SECTION and FILE SECTION in the DATA DIVISION, and learning how data must be defined before procedural code execution.

Code

       IDENTIFICATION DIVISION.
       PROGRAM-ID. DIVISIONS-DEMO.
       
       ENVIRONMENT DIVISION.
       CONFIGURATION SECTION.
       
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  MESSAGE-VAR PIC X(20) VALUE "Divisions Explained".
       
       PROCEDURE DIVISION.
           DISPLAY MESSAGE-VAR.
           STOP RUN.

Explanation

Each division in COBOL is further organized into sections and paragraphs. The ENVIRONMENT DIVISION often contains the CONFIGURATION SECTION (describing the computer) and the INPUT-OUTPUT SECTION (mapping files). This separation allows the same COBOL program to be ported to different hardware simply by changing the Environment Division settings.

The DATA DIVISION is the heart of COBOL's data processing capabilities. It typically includes the FILE SECTION (describing file layouts) and the WORKING-STORAGE SECTION. The Working-Storage Section is used for variables that are internal to the program—counters, flags, and temporary storage—as opposed to data read directly from a file.

In this example, we define a variable MESSAGE-VAR in the Working-Storage Section. Unlike modern languages where variables can be declared anywhere, COBOL requires all data to be defined before any procedural code is written. This makes the data structure of the program immediately visible and easy to audit.

Code Breakdown

5
CONFIGURATION SECTION: A section within the Environment Division. Historically used to specify the Source-Computer (where it's compiled) and Object-Computer (where it runs).
8
WORKING-STORAGE SECTION: The most common section in the Data Division. It holds variables that persist throughout the program's execution.
9
01 MESSAGE-VAR PIC X(20). This defines a variable named MESSAGE-VAR. '01' indicates it's a top-level record. 'PIC X(20)' means it holds 20 alphanumeric characters.
9
VALUE "Divisions Explained". This clause initializes the variable with a specific string. Without this, the initial content would be undefined (or null).
12
DISPLAY MESSAGE-VAR. Instead of printing a literal string, we are now printing the contents of the variable we defined.