COBOL Coding Divisions

Coding Divisions

COBOL works on data encapsulation property, where the data is encapsulated (wrapped in lower sets) between different divisions. Each division is a set of code that restrain one or more than one section.

We can identify a division once we encounter the division name, and it ends before the beginning of the new division.

Types of Divisions in COBOL

COBOL is majorly divided among four divisions comprising different elements, which are given below:

· Identification Division

· Environment Division

· Data Division

· Procedure Division

Let’s have a look on each of these divisions.

  1. Identification Division

This division allows the programs and compiler to identify code elements and definitions of class. Since 2002, COBOL supports object-oriented programming, therefore, it consists of classes and objects using OOP methods. It also supports inheritance and polymorphism.

The objects of identification divisions are stored as a reference, and can be used by calling a method called "INVOKE".

SYNTAX =>

EXAMPLE =>

IDENTIFICATION DIVISION .
PROGRAM – ID . FST-PGM .
AUTHOR . Spiderman.
INSTALLATION .  JAN 1969 .
DATE – WRITTEN . 11 / 11 / 20 .
DATE COMPILED .  11 / 12 / 20 . 16 : 47 : 56 .
SECURITY .  PRIVATE .

Let’s understand the key terms used in the example:

PROGRAM – ID => It contains the name of the program and it is listed under program id. The name of program must be less than or equal to 8 characters max, and this identity is mandatory to be filled and must be different for each program.

AUTHOR => It consists of name of the coder or the programming entity. The name consists of set of characters and must be declared in area – a. It is not mandatory to fill this field

INSTALLATION => This holds the name of location or the company details, where the program is installed.

DATE - WRITTEN => It holds the actual date of the program at the time when program is being written.

DATE - COMPILED => It describes the exact date and time at which the program is precompiled.

SECURITY => It holds the security patch of the program and it is an optional field.

SAMPLE PROGRAM:

This is a demo program to learn and understand various types of identification division through examples.

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
AUTHOR  Spiderman.
INSTALLATION.  JAN 1969.
DATE–WRITTEN. 11 / 11 / 20.
DATE-COMPILED.  11 / 12 / 20. 16 : 47 : 56.
SECURITY.  PRIVATE.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
DISPLAY " ALL ENTITIES OF IDENTIFICATION DIVISION . . . "
DISPLAY " AUTHOR = Spiderman "
DISPLAY " INSTALLATION = JAN 1969 "
DISPLAY " DATE – WRITTEN = 11 / 11 / 20 "
DISPLAY " DATE-COMPILED = 11 / 12 / 20. 16 : 47 : 56. "
STOP RUN.

THE OUTPUT WILL BE:

 ALL ENTITIES OF IDENTIFICATION DIVISION . . .
 AUTHOR = Spiderman
 INSTALLATION = JAN 1969
 DATE – WRITTEN = 11 / 11 / 20
 DATE-COMPILED = 11 / 12 / 20. 16 : 47 : 56.
 SECURITY = PRIVATE

In this program, we have explained all types of identification division and displayed all the valid identity division in the output.

  • Environment Division

This environment division is used to identify and distinguish between character sets and currency signs.

It helps to identify input-output operations using file format.

Further, this division is specified by two categories:

  • Configuration section:

All the information regarding the system, which is used to write and run is accumulated in this section

This section has two paragraphs:

  • Source computer – It containscomplete details about the program that is to be executed.
    • Object computer – It executes the program.
  • Input-Output sections:

These sections work through the files and give complete information about which file is used in the program.

SYNTAX =>

ENVIRONMENT DIVISION.

SAMPLE PROGRAM

THIS IS A DEMO PROGRAM TO LEARN AND UNDERSTAND VARIOUS TYPES OF ENVIRONMENT DIVISION THROUGH EXAMPLES.

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM-370.
OBJECT-COMPUTER. IBM-370.
SPECIAL-NAMES.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
I-O-CONTROL.
DATA DIVISION.
PROCEDURE DIVISION.
DISPLAY " ALL ENTITIES OF ENVIRONMENT DIVISION . . . "
DISPLAY " SOURCE-COMPUTER. IBM-370. "
DISPLAY " OBJECT-COMPUTER. IBM-370. "
STOP RUN.

THE OUTPUT WILL BE:

ALL ENTITIES OF ENVIRONMENT DIVISION . . . 
 SOURCE-COMPUTER. IBM-370.
 OBJECT-COMPUTER. IBM-370.

In this program, we have used and explained all types of environment division and displayed all the valid environment division in the output.

  • Data Division

Data division holds some of the most important information required for a program to run efficiently. Thisdivision describes the data item, their name, length, decimal point, storing format, etc.

Some anomalies of data division:

  • Working-storage section-for static variables
  • Local storage section-automatic variables
  • Linkage section-parameters and return value
  • report/screen section-text-based user interface

· File section - Allows the user to create and define the recorded file structure.

· Working storage section: This section allows users to define static variables.

· Local-storage section: This section is also used to define variables, but the main difference between the working storage section and the local storage section is that the program starts executing in this section.

· Linkage section: This section consists of the names of the data types received from storage.

· Report/screen section: Thissection is specified for text-based user interface.

COBOL data is divided into three main divisions for better functioning

  • FILE SECTION
  • WORKING STORAGE SECTION
  • LINKAGE SECTION
  1. FILE SECTION =>

This section is responsible to hold all the information regarding from where the data is coming and where the data have to be at the time of processing.

SYNTAX =>

DATA DIVISION.
FILE SECTION .
FD/SD filename
            [ RECORD CONTAINS integer CHARACTERS ]
            [ BLOCK CONTAINS integer RECORDS ]
            [ DATA RECORD IS record - name ]
            [ RECORDING MODE IS { F / V / U / S } ]
  • FD: It refers to the file description level indicator, this usually helps to describe the layout of the file that we have defined.
  • SD: It stands for sort file description level indicator, this helps us to describe the details of the short file.
  • RECORD CONTAINS CLAUSE: It is used to define the physical size of the file in character format about the records of data as every particular data of a file is mandatory to define in record description entry.

Record clause is used in the format of record length and there are three types of record length that we can use:

1 – fixed length - In this, the file declaration of the file record length can be integer.

[ RECORD CONTAINS integer CHARACTERS ]

2 – dynamic length – In dynamic file declaration, the dynamic file record length will increase on the basis of data processed in data – name section at the time of compiling.  The length can increase from integer 1 to integer 2 bytes.

Here integer 1 will hold the minimum value and similarly integer 2 will holds the maximum value.

[ RECORD IS VARYING IN SIZE integer 1 to integer 2 [ DEPENDING ON data – name ] ]

3 – variable length - - here in this type the record length will be varying in between fixed lengths.

Length will be increasing on the basis of data processed in data – name section during the time of compiling. The length can increase from integer 1 to integer 2 bytes.

Here integer 1 will hold the minimum value and similarly integer 2 will hold the maximum value.

[ RECORD CONTAINS integer 1 TO integer 2 CHARACTERS ]

  • BLOCK CONTAINS CLAUSE: As the name suggests, BLOCK CONTAINS clause is used to define the blocking feature of the file that is being used.

If we activate the blocking feature of the file while being described, then there will be no way for the operating system to determine.

NOTE :- BLOCK CONTAINS clause depends directly on RECORD CONTAINS clause

Similar to RECORD CONTAINS, BLOCK CONTAINS clause also use three types of file records

1 – fixed length - - In fixed record the file declaration of the file record length can be integer.

[ RECORD CONTAINS integer CHARACTERS ]

[ BLOCK CONTAINS integer1 CHARACTERS ]

2 – dynamic length - - The length in dynamic file is defined at runtime. Dynamic file declaration of the dynamic file record length will increase on the basis of data processed in data – name section at the time of compiling. The length can increase from integer 3 to integer 4 bytes

Here integer 3 will hold the minimum value and integer 4 will hold the maximum value.

[ RECORD IS VARYING IN SIZE integer 1 to integer 2 [ DEPENDING ON data – name ] ]

[ BLOCK CONTAINS IN SIZE integer 3 to integer 4 [ DEPENDING ON data – name ] ]

3 – variable length - - Here in this type, the record length will be varying in between fixed lengths.

Length will increase on the basis of data processed in data – name section at the time of compiling. It can increase from integer 3 to integer 4 bytes.

Here integer 3 will hold the minimum value and similarly integer 4 will hold the maximum value.

[ RECORD CONTAINS integer 1 TO integer 2 CHARACTERS ]

[ BLOCK CONTAINS integer 3 TO integer 4 CHARACTERS ]

  • DATA RECORD CLAUSE : Data record clause is not mandatory and have least impact on working of the program. Data record clause is just used to specify the file record layout to manipulate the data using the record layout.

[ DATA RECORD IS FILE-Z1 ]

  • RECORDING MODE CLAUSE: As the name specifies, recording mode clause holds all the logical records that are stored in the file and how these records being read and written.

Parameters of recording file clause { F / U / V }

F parameterV parameterU parameter
Random access and relative files option is availableBlock descriptor files are required to store the block lengthNo need to use BLOCK CONTAINER clause
Specifies that file contains fixed length.Specifies that file contains either fixed length or variable length.Specifies that file contains either fixed length or variable length logical record.
More than one record can be stored in BLOCK clauseMore than one record can be stored in BLOCK clauseOne BLOCK can contain only one record.
OCCOUR DEPENDING clause is not linked with record descriptionThe block description file stores 4 bytes of each record and is not available for future use.No record length and block description is required.
All records are stored in fixed length record file therefore no additional memory is required to store record apart the actual memory.The record length file stores 4 bytes of each record and is not available for future use.Specifies undefined logical record length.
  • WORKING STORAGE SECTION =>

WORKING STORAGE SECTION is used to allot variables those will be later used in the program, this section also holds data name, which will later be used for internal manipulation of data. The initialisation starts here after the program start executing.

SYNTAX =>

DATA DIVISION .
WORKING – STORAGE SECTION .
                 [ Working – storage fields declaration ]

EXAMPLE =>

SAMPLE PROGRAM

THIS IS A DEMO PROGRAM TO LEARN AND UNDERSTAND VARIOUS TYPES OF WORKING STORAGE SECTION THROUGH EXAMPLES.

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
* * * INDIVIDUAL DATA ITEM * * *
* * * ELEMENTRY DATA ITEM * * *
01 LEVEL-1 PIC 9(03) VALUE 256.
* * * GROUP DATA ITEM * * *
01 LEVEL-GROUP.
* * * ELEMENTRY DATA ITEM * * *
   05 LEVEL-21 PIC 9(03) VALUE 256.
   05 LEVEL-22 PIC 9(03) VALUE 128.
PROCEDURE DIVISION.
DISPLAY ' INDIVIDUAL DATA ITEM : ' LEVEL-1.
DISPLAY ' GROUP DATA ITEM : ' LEVEL-GROUP.
DISPLAY ' ELEMENTRY DATA ITEM - 1 : ' LEVEL-21.
DISPLAY ' ELEMENTRY DATA ITEM - 2 : ' LEVEL-22.
STOP RUN.

THE OUTPUT WILL BE :

 INDIVIDUAL DATA ITEM : 256
GROUP DATA ITEM : 256128
ELEMENTRY DATA ITEM - 1 : 256
ELEMENTRY DATA ITEM - 2 : 128

In this program, we have used and explained all types of working storage section and displayed all the valid data division in the output.

  • LINKAGE SECTION =>

Linkage section uses CALL function to describe the data available to another program.

It is usually used in subprograms by defining from where to send and where to receive the data between programs from the main program or the calling program by using layout.

It can access all the data from external program.

SYNTAX =>

DATA DIVISION.
LINKAGE SECTION
  [ linkage section data item ]
* * * * * * *   CALL STATEMENT * * * * * * *
CALL ‘ subprogram ‘ USING name , address .
IDENTIFICATION DIVISION .
PROGRAM – ID subprog IS INITIAL .
DATA DIVISION .
FILE SECTION .
FD/SD filename
            [ RECORD CONTAINS integer CHARACTERS ]
            [ BLOCK CONTAINS integer RECORDS ]
            [ DATA RECORD IS record - name ]
            [ RECORDING MODE IS { F / V / U / S } ]
WORKING – STORAGE SECTION .
                 [ Working – storage fields declaration ]
LINKAGE SECTION .
01 name   PIC X ( 10 )
01 address  PIC X ( 10 )
PROCEDURE DIVISION USING name , address .
  • Procedure Division

Procedural division is one of the most crucial section, this section consists of algorithms and logical commands which finally results in the overall logic of the program.

Apart from logical commands, this section also consists of some executable commands defined in the data section.

Procedure division contains [sections – paragraphs – sentences – statements]

SYNTAX =>

1 ) Main program or calling procedure division

PROCEDURE DIVISION .

2 ) sub – program or called program procedure division

PROCEDURE DIVISION [ USING { data – name – 1 } . . .  . ]

2 ) procedure division with the use of declaratives

PROCEDURE DIVISION .

DECLARATIVES .

 _ _ _ _ _ _ _ _ _ _

_ _ _ _ _ _ _ _ _ _

_ _ _ _ _ _ _ _ _ _

END DECLARATIVE .

( In order to use declarative statement we have to use the USE statement )

USING CLAUSE =>

USING clause is used only in cases in which a program command calls another COBOL program using CALL statement then only we specify using clause in procedure division of the sub program or the called program.

SAMPLE PROGRAM :

THIS IS A DEMO PROGRAM TO LEARN AND UNDERSTAND VARIOUS TYPES OF DATA DIVISION THROUGH EXAMPLES.

IDENTIFICATION DIVISION.
PROGRAM-ID. XYZ.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE - COMPUTER. IBM WITH DEBUGGING MODE.
OBJECT - COMPUTER. IBM-370.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 LEVEL-1 PIC 9(03) VALUE 256.
01 LEVEL-GROUP.
   05 LEVEL-21 PIC 9(03) VALUE 256.
   05 LEVEL-22 PIC 9(03) VALUE 128.
   88 READY-FLAG VALUE 1.
PROCEDURE DIVISION.
DECLARATIVES.
COBOL-DEBUG SECTION.
IF READY - FLAG THEN
 DISPLAY DEBUG NAME
 END - IF.
 END - DECLARATIVE .
DISPLAY ' INDIVIDUAL DATA ITEM : ' LEVEL-1.
DISPLAY ' GROUP DATA ITEM : ' LEVEL-GROUP.
DISPLAY ' ELEMENTRY DATA ITEM - 1 : ' LEVEL-21.
DISPLAY ' ELEMENTRY DATA ITEM - 2 : ' LEVEL-22.
MAIN - PROCESSING.
SET READY - TRACE TO TRUE.
. . .
PARAGRAPH - 001 .
. . .
PARAGRAPH - 002 .
. . .
PARAGRAPH - 003 .
. . .
PARAGRAPH - 004 .
. . .
STOP RUN.

THE OUTPUT WILL BE:

INDIVIDUAL DATA ITEM : 256
 GROUP DATA ITEM : 256128
 ELEMENTRY DATA ITEM - 2 : 128
 ELEMENTRY DATA ITEM - 1 : 256

In this program, we have used and explained all types of DATA DIVISIONS COBOL provides and display all the valid data division in the output.