Blog enCOBOL

Basic Structure of a COBOL Program

COBOL (Common Business-Oriented Language) is a programming language primarily designed for commercial and business applications. Below, I will provide you with a basic structure of a COBOL program and explain its main sections:

COBOL Program

IDENTIFICATION DIVISION.
PROGRAM-ID. MyProgram.
AUTHOR. YourName.
DATE-WRITTEN. DD/MM/YYYY.

ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM-PC.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 Variable-1 PIC 9(5).
01 Variable-2 PIC X(20).

PROCEDURE DIVISION.
Program-Start.
    DISPLAY 'Hello, World!'.
    ACCEPT Variable-1 FROM CONSOLE.
    MOVE 'Welcome' TO Variable-2.
    DISPLAY 'Number entered: ' Variable-1.
    DISPLAY 'Message: ' Variable-2.
Program-End.

STOP RUN.

IDENTIFICATION DIVISION

  • PROGRAM-ID: Identifies the program name.
  • AUTHOR: Name of the program’s author.
  • DATE-WRITTEN: Date when the program was created.

ENVIRONMENT DIVISION

  • CONFIGURATION SECTION: Defines the execution environment configuration.
  • SOURCE-COMPUTER: Specifies the type of computer for which the program is developed.

DATA DIVISION

  • WORKING-STORAGE SECTION: Reserves space for working variables.
  • 01: Entry level of the variable.
  • Variable-1: Example of a numeric variable with 5 digits.
  • Variable-2: Example of an alphanumeric variable with a length of 20.

PROCEDURE DIVISION

  • Program-Start: Label marking the beginning of program execution.
  • DISPLAY: Outputs messages to the console.
  • ACCEPT: Reads input from the console.
  • MOVE: Assigns a value to a variable.
  • DISPLAY: Outputs variable values to the console.
  • Program-End: Label marking the end of program execution.

STOP RUN

  • Indicates the end of the program.

This is a very simple and basic example of a COBOL program. In larger applications, you will find additional sections such as FILE SECTION, PROCEDURE DIVISION, among others. Additionally, COBOL uses a fixed-format style where each line has a specific structure based on character positions. This format is a unique feature of COBOL.