English 中文(简体)
COBOL - Program Structure
  • 时间:2024-09-17

COBOL - Program Structure


Previous Page Next Page  

A COBOL program structure consists of spanisions as shown in the following image −

Program Structure

A brief introduction of these spanisions is given below −

    Sections are the logical subspanision of program logic. A section is a collection of paragraphs.

    Paragraphs are the subspanision of a section or spanision. It is either a user-defined or a predefined name followed by a period, and consists of zero or more sentences/entries.

    Sentences are the combination of one or more statements. Sentences appear only in the Procedure spanision. A sentence must end with a period.

    Statements are meaningful COBOL statements that perform some processing.

    Characters are the lowest in the hierarchy and cannot be spanisible.

You can co-relate the above-mentioned terms with the COBOL program in the following example −

PROCEDURE DIVISION.
A0000-FIRST-PARA SECTION.
FIRST-PARAGRAPH.
ACCEPT WS-ID            - Statement-1  -----|
MOVE  10  TO WS-ID      - Statement-2       |-- Sentence - 1
DISPLAY WS-ID           - Statement-3  -----|
.

Divisions

A COBOL program consists of four spanisions.

Identification Division

It is the first and only mandatory spanision of every COBOL program. The programmer and the compiler use this spanision to identify the program. In this spanision, PROGRAM-ID is the only mandatory paragraph. PROGRAM-ID specifies the program name that can consist 1 to 30 characters.

Try the following example using the Live Demo option onpne.

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
DISPLAY  Welcome to Tutorialspoint .
STOP RUN.

Given below is the JCL to execute the above COBOL program.

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

When you compile and execute the above program, it produces the following result −

Welcome to Tutorialspoint

Environment Division

Environment spanision is used to specify input and output files to the program. It consists of two sections −

    Configuration section provides information about the system on which the program is written and executed. It consists of two paragraphs −

      Source computer − System used to compile the program.

      Object computer − System used to execute the program.

    Input-Output section provides information about the files to be used in the program. It consists of two paragraphs −

      File control − Provides information of external data sets used in the program.

      I-O control − Provides information of files used in the program.

ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
   SOURCE-COMPUTER. XXX-ZOS.
   OBJECT-COMPUTER. XXX-ZOS.

INPUT-OUTPUT SECTION.
   FILE-CONTROL.
   SELECT FILEN ASSIGN TO DDNAME
   ORGANIZATION IS SEQUENTIAL.

Data Division

Data spanision is used to define the variables used in the program. It consists of four sections −

    File section is used to define the record structure of the file.

    Working-Storage section is used to declare temporary variables and file structures which are used in the program.

    Local-Storage section is similar to Working-Storage section. The only difference is that the variables will be allocated and initiapzed every time a program starts execution.

    Linkage section is used to describe the data names that are received from an external program.

COBOL Program

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.
   SELECT FILEN ASSIGN TO INPUT.
      ORGANIZATION IS SEQUENTIAL.
      ACCESS IS SEQUENTIAL.

DATA DIVISION.
   FILE SECTION.
   FD FILEN
   01 NAME PIC A(25).
   
   WORKING-STORAGE SECTION.
   01 WS-STUDENT PIC A(30).
   01 WS-ID PIC 9(5).

   LOCAL-STORAGE SECTION.
   01 LS-CLASS PIC 9(3).
   
   LINKAGE SECTION.
   01 LS-ID PIC 9(5).
   
PROCEDURE DIVISION.
   DISPLAY  Executing COBOL program using JCL .
STOP RUN.

The JCL to execute the above COBOL program is as follows −

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO
//INPUT DD DSN = ABC.EFG.XYZ,DISP = SHR

When you compile and execute the above program, it produces the following result −

Executing COBOL program using JCL

Procedure Division

Procedure spanision is used to include the logic of the program. It consists of executable statements using variables defined in the data spanision. In this spanision, paragraph and section names are user-defined.

There must be at least one statement in the procedure spanision. The last statement to end the execution in this spanision is either STOP RUN which is used in the calpng programs or EXIT PROGRAM which is used in the called programs.

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-NAME PIC A(30).
   01 WS-ID PIC 9(5) VALUE 12345.

PROCEDURE DIVISION.
   A000-FIRST-PARA.
   DISPLAY  Hello World .
   MOVE  TutorialsPoint  TO WS-NAME.
   DISPLAY "My name is : "WS-NAME.
   DISPLAY "My ID is : "WS-ID.
STOP RUN.

JCL to execute the above COBOL program −

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS = A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

When you compile and execute the above program, it produces the following result −

Hello World
My name is : TutorialsPoint
My ID is : 12345
Advertisements