0

General Anatomy of a COBOL Program

Background

This article explains the general anatomy of a COBOL program.

Software Environment

  • Windows 7 Professional SP1
  • Visual COBOL for Eclipse Personal Edition 2.2 by Micro Focus
  • Java 1.7 (1.7.0_67 – Windows x86)

General Anatomy of a COBOL Program

Just like in Java and C, a COBOL program has structure. Structures organize things. Some are rudimentary, others are elegant and whippy.

In Java, we have package and import statements that must appear before any class declarations. In C, we have the include statements and function declarations that must appear before the main function. COBOL is no exception.

The basic structure of a COBOL program consists of four (4) divisions. Each division can have sections. Each section can have paragraphs. Each paragraph can have sentences. Lastly, each sentence can have statements. In Java or C, these are just valid places where certain things can be placed within a program. For example, package and import statements exist on the “header” part of a Java program. The rest of the part may be considered the “body” of the same program. Same goes with C. A block or method declaration (with implementation) is similar to a section in COBOL.

  1. IDENTIFICATION DIVISION.
    This contains program information like program description, author or date when it was written or updated.

    • In Java, this is like the section between the import statements and main class declaration where Javadoc annotations, e.g., @author, are used.
    • In C, this can be thought of the section just above the any function that describes what it does.
  2. ENVIRONMENT DIVISION.
    This contains environment information that describes the environment in which the program will run or configuration to use during compilation or execution.

    • In C, this can be thought of the section where #pragmas can be used and interpreted (by the C compiler).
    • In Java, this can be the set of environment variables that a program can readily be used.
  3. DATA DIVISION.
    This is a section exclusive for variable declaration and initialization. It has two (2)  main sections:

    1. FILE SECTION.
    2. WORKING-STORAGE SECTION.
      • In C, this is the section just below the function function name but before any processing codes.
      • In Java, this can be anywhere before a variable is used.
  4. PROCEDURE DIVISION.
    This is where you put codes to implement some logic.

Some COBOL compilers may not require all of the divisions but when they do, the order should be as listed above.

Sample COBOL Program

Below is a sample COBOL program. Note that the IDENTIFICATION SECTION is not explicitly defined here but the program has program-id (like a Javadoc annotation in Java). Just short descriptions for some lines:

  • Lines 8 – 10, three (3) variables are declared.
  • Line 13, displays a string of text on the screen.
  • Line 14, accepts keyboard input from screen.
  • Line 17, multiplication of two number and assignment of product to another variable

Sample Output

sample_output20141122

Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert