Cobol Language Syntax and Structure

In this article, we will explore the basic syntax and structure of Cobol, a high-level programming language.

2025-03-08T09:19:25.233Z Back to posts

Cobol Language Syntax

Cobol (COmmon Business-Oriented Language) is a high-level programming language that was first developed in the 1950s and 1960s. It was designed to be used for business applications, such as accounting and inventory control, and has since become one of the most widely used programming languages.

Basic Syntax

Cobol code begins with a heading section, which contains information about the program, including its name, author, and date. This is followed by the identification division, which defines the layout of the program.

Identification Division


IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
AUTHOR. YOUR NAME.
DATE-WRITTEN. CURRENT DATE.

The identification division is where you define the basic information about your program.

Environment Division


The environment division defines the operating system and environment in which your program will run.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INPUT-FILE ASSIGN TO 'input.txt'.
SELECT OUTPUT-FILE ASSIGN TO 'output.txt'.

This section is used to specify files, disk storage, and other external resources that the program will use.

Data Division


The data division defines the format of the data used by your program.

DATA DIVISION.
FILE SECTION.
FD  INPUT-FILE-1.
01  INPUT-RECORD.
05  NAME        PIC X(20).
05  AGE         PIC 9(3).

FD  OUTPUT-FILE-1.
01  OUTPUT-RECORD.
05  MESSAGE     PIC X(50).

This section is used to define the format of data records, including file layouts and record formats.

Program Structure


Cobol programs are structured into a series of sections, each with its own purpose:

  • Identification Division: defines the basic information about the program.
  • Environment Division: defines the operating system and environment in which the program will run.
  • Data Division: defines the format of data used by the program.
  • Procedure Division: contains the main body of the program, including processing statements.

Control Statements


Cobol provides a range of control statements that allow you to control the flow of your program:

  • IF statement: used for conditional execution.
  • ELSE statement: used for alternative execution.
  • FOR statement: used for looping.
  • PERFORM statement: used for executing procedures.

Arithmetic and Data Manipulation


Cobol provides a range of arithmetic and data manipulation statements, including:

  • ADD: adds two or more numbers together.
  • SUBTRACT: subtracts one number from another.
  • MULTIPLY: multiplies two or more numbers together.
  • DIVIDE: divides one number by another.

Examples


Here is an example of a simple Cobol program that greets the user:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

ENVIRONMENT DIVISION.
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-1.
01  INPUT-RECORD.
05  NAME        PIC X(20).
05  AGE         PIC 9(3).

FD  OUTPUT-FILE-1.
01  OUTPUT-RECORD.
05  MESSAGE     PIC X(50).

PROCEDURE DIVISION.
MAIN-PROGRAM.
DISPLAY 'Hello, world!'.
STOP RUN.

IDENTIFICATION DIVISION.
END PROGRAM HELLO.

This program will output “Hello, world!” when run.

Conclusion


In this article, we have looked at the basic syntax of Cobol and its structure. We have also seen how to use control statements, arithmetic operations, and data manipulation in Cobol programs. With practice, you can become proficient in using Cobol for your business applications.