Divisions
- A division is one of the primary components of a program, that organizes its code and functionality.
- It is a collection of one or more sections and paragraphs.
- Division begins with the division name and ends at the beginning of the subsequent division or the program ends.
- All divisions are system-defined and should begin in Area A.
COBOL has four system-defined divisions, and those are -
- IDENTIFICATION DIVISION
- ENVIRONMENT DIVISION
- DATA DIVISION
- PROCEDURE DIVISION
IDENTIFICATION DIVISION
It provides metadata about the program, such as its name, author, installation, and other descriptive information used to uniquely identify the program. It should be coded as the first division in the COBOL program. It is a mandatory division in the COBOL program.
Syntax -
----+----1----+----2----+----3----+----4----+----5----+
IDENTIFICATION DIVISION.
PROGRAM-ID. NameOfProgram.
[AUTHOR. NameOfProgrammer.]
[INSTALLATION. Installation-details.]
[DATE-WRITTEN. program-written-date.]
[DATE-COMPILED. program-compiled-date.]
[SECURITY. security-status.]
Parameters -
- PROGRAM-ID - Mandatory. It provides the name of the COBOL program. This name is used by the OS when the program is called or executed.
- AUTHOR (optional) - Provides the name of the individual, team, or organization that wrote the program.
- INSTALLATION (optional) - Offers details about the computer installation where the program was developed or is intended to run.
- DATE-WRITTEN (optional) - Specifies the date when the program was originally written.
- DATE-COMPILED (optional) - Gives the date when the program was last compiled. Some compilers or environments may automatically fill in this entry.
- SECURITY (optional) - Indicates the program's security level or classification. It's typically used in environments with specific security requirements.
Example - Explaing about IDENTIFICATION DIVISION entries.
ENVIRONMENT DIVISION
It is responsible for specifying the computer environment (mostly computer name in mainframe environment) on which the program is compiled and executed. In addition, it defines the input and output sources (files) required to run the program and interact with devices like printers, files, etc. It is optional. It has two sections: the CONFIGURATION SECTION and the INPUT-OUTPUT SECTION.
Syntax -
ENVIRONMENT DIVISION.
[CONFIGURATION SECTION.
[SOURCE-COMPUTER. source-computer-name [WITH DEBUGGING MODE]
[OBJECT-COMPUTER. object-computer-name]
[SPECIAL-NAMES. special-names-enties]]
[INPUT-OUTPUT SECTION.
[FILE-CONTROL. file-control-entries]
[I-O-CONTROL. i-o-control-entries]]
Parameters -
- SOURCE-COMPUTER - Specifies the computer on which the source program is compiled. WITH DEBUGGING MODE is used to enable the debugging switch in the program.
- OBJECT-COMPUTER - Specifies the computer where the object program is to be executed.
- SPECIAL-NAMES - Specifies system-specific features, such as mapping symbolic characters to system-specific characters or defining currency signs.
- FILE-CONTROL - Used to map logical files (as named in the program) to physical files on the system.
- I-O-CONTROL - Provides specifications for the computer's input-output operations, like multiple file tapes or devices.
Examples - Example to enable DEBUGGING MODE.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM3278 WITH DEBUGGING MODE.
PROCEDURE DIVISION.
D DISPLAY "DISPLAYING DEBUGGING LINE".
DISPLAY "DISPLAYING NORMAL LINE".
Output -
DISPLAYING DEBUGGING LINE DISPLAYING NORMAL LINE
DATA DIVISION
It is responsible for declaring the variables used to process the data in the application program. It is optional when no variables are declared. It has three sections: FILE SECTION, WORKING-STORAGE SECTION, and LINKAGE SECTION.
Syntax -
DATA DIVISION.
[FILE SECTION.
FD|SD file-description-entry...
[data-description-entry...]]
[WORKING-STORAGE SECTION.
data-description-entry...]
[LINKAGE SECTION.
data-description-entry...]
Parameters -
- FILE SECTION - This section is used to declare the layout of files that are read from or written to. FD is used for physical files. SD is used for sort files.
- WORKING-STORAGE SECTION - Defines variables and constants that are used throughout the program. These are typically initialized each time the program starts.
- LINKAGE SECTION - Used to declare the variables to pass the data into or out of the program. It's especially useful when working with subprograms or modules.
Examples - Declaring a file with length 80.
----+----1----+----2----+----3----+----4----+
DATA DIVISION.
FILE SECTION.
FD file1
RECORD CONTAINS 80 CHARACTERS
BLOCK CONTAINS 800 CHARACTERS
RECORDING MODE IS F
DATA RECORD IS emp-rec.
FILE STATUS IS ws-fs.
01 emp-rec PIC X(80).
WORKING-STORAGE SECTION.
01 ws-fs PIC 9(02).
PROCEDURE DIVISION
The actual logic will be written in PROCEDURE DIVISION, which is responsible for carrying out the data processing tasks. Program execution starts from PROCEDURE DIVISION and ends with STOP RUN or GO BACK statements.
Syntax -
PROCEDURE DIVISION [USING parameter...].
[section-name SECTION.]
paragraph-name.
[statements...]
[additional-paragraphs...]
[... more sections and paragraphs ...]
Parameters -
- USING parameter - It is optional. When a program can be CALLED from another program, we use the USING clause to pass data between them. The parameters correspond to the LINKAGE SECTION entries.
- section-name SECTION - It is optional. The PROCEDURE DIVISION can be organized into sections, each of which can contain one or more paragraphs. A section name ends with the word SECTION.
- paragraph-name - A label that marks a set of related statements.
- statements - The executable COBOL statements that perform operations on data, make decisions, manipulate files, and more.
Examples - PROCEDURE DIVISION with declaratives.
----+----1----+----2----+----3----+----4----+
...
PROCEDURE DIVISION.
DECLARATIVES.
DEBUG-DECLARATIVES SECTION.
USE FOR DEBUGGING ON ALL PROCEDURES.
DEBUG-DECLARATIVES-PARAGRAPH.
DISPLAY "TRACE FOR PROCEDURE-NAME : " DEBUG-NAME.
END DECLARATIVES.
...