COBOL (Common Business-Oriented Language) is a high-level programming language designed for business applications. Understanding the basic structure of a COBOL program is essential for writing and maintaining COBOL code. This section will cover the fundamental components and layout of a COBOL program.
Key Components of a COBOL Program
A COBOL program is divided into four main divisions:
- Identification Division
- Environment Division
- Data Division
- Procedure Division
Each division serves a specific purpose and contains various sections and paragraphs. Let's break down each division in detail.
- Identification Division
The Identification Division is the first division in a COBOL program. It provides metadata about the program, such as the program name and author.
- PROGRAM-ID: Specifies the name of the program.
- AUTHOR: (Optional) Specifies the name of the programmer.
- Environment Division
The Environment Division specifies the environment in which the program will run. It includes information about the computer and the files used by the program.
ENVIRONMENT DIVISION. CONFIGURATION SECTION. SOURCE-COMPUTER. IBM-370. OBJECT-COMPUTER. IBM-370. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INPUT-FILE ASSIGN TO 'input.txt'. SELECT OUTPUT-FILE ASSIGN TO 'output.txt'.
- CONFIGURATION SECTION: Describes the computer environment.
- SOURCE-COMPUTER: Specifies the computer on which the source program is compiled.
- OBJECT-COMPUTER: Specifies the computer on which the object program will run.
- INPUT-OUTPUT SECTION: Describes the files used by the program.
- FILE-CONTROL: Specifies the files and their assignments.
- Data Division
The Data Division defines the variables and data structures used in the program. It is divided into several sections, such as the File Section, Working-Storage Section, and Linkage Section.
DATA DIVISION. FILE SECTION. FD INPUT-FILE. 01 INPUT-RECORD PIC X(80). FD OUTPUT-FILE. 01 OUTPUT-RECORD PIC X(80). WORKING-STORAGE SECTION. 01 WS-VARIABLE PIC 9(4) VALUE 0.
- FILE SECTION: Defines the structure of the files.
- FD: File Description entry, which describes the file.
- 01: Level number indicating the hierarchy of data items.
- PIC: Picture clause, which defines the data type and size.
- WORKING-STORAGE SECTION: Defines temporary variables used during program execution.
- Procedure Division
The Procedure Division contains the executable instructions of the program. It is where the logic of the program is implemented.
PROCEDURE DIVISION. OPEN INPUT INPUT-FILE. OPEN OUTPUT OUTPUT-FILE. PERFORM READ-INPUT. PERFORM WRITE-OUTPUT. CLOSE INPUT-FILE. CLOSE OUTPUT-FILE. STOP RUN. READ-INPUT. READ INPUT-FILE INTO INPUT-RECORD AT END DISPLAY 'End of file reached' STOP RUN END-READ. WRITE-OUTPUT. MOVE INPUT-RECORD TO OUTPUT-RECORD. WRITE OUTPUT-RECORD.
- OPEN: Opens a file for reading or writing.
- PERFORM: Executes a paragraph or section.
- READ: Reads a record from a file.
- WRITE: Writes a record to a file.
- CLOSE: Closes a file.
- STOP RUN: Terminates the program.
Practical Example
Here is a complete example of a simple COBOL program that reads from an input file and writes to an output file.
IDENTIFICATION DIVISION. PROGRAM-ID. FileCopy. AUTHOR. John Doe. ENVIRONMENT DIVISION. CONFIGURATION SECTION. SOURCE-COMPUTER. IBM-370. OBJECT-COMPUTER. IBM-370. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT INPUT-FILE ASSIGN TO 'input.txt'. SELECT OUTPUT-FILE ASSIGN TO 'output.txt'. DATA DIVISION. FILE SECTION. FD INPUT-FILE. 01 INPUT-RECORD PIC X(80). FD OUTPUT-FILE. 01 OUTPUT-RECORD PIC X(80). WORKING-STORAGE SECTION. 01 WS-VARIABLE PIC 9(4) VALUE 0. PROCEDURE DIVISION. OPEN INPUT INPUT-FILE. OPEN OUTPUT OUTPUT-FILE. PERFORM UNTIL WS-VARIABLE = 1 READ INPUT-FILE INTO INPUT-RECORD AT END MOVE 1 TO WS-VARIABLE NOT AT END MOVE INPUT-RECORD TO OUTPUT-RECORD WRITE OUTPUT-RECORD END-READ END-PERFORM. CLOSE INPUT-FILE. CLOSE OUTPUT-FILE. STOP RUN.
Explanation
- Identification Division: Specifies the program name and author.
- Environment Division: Defines the computer environment and file assignments.
- Data Division: Describes the structure of the input and output files and declares a working-storage variable.
- Procedure Division: Contains the logic to open files, read from the input file, write to the output file, and close the files.
Summary
In this section, we covered the basic structure of a COBOL program, including the Identification Division, Environment Division, Data Division, and Procedure Division. Understanding these components is crucial for writing and maintaining COBOL programs. In the next module, we will delve into the basic COBOL syntax and data types.