The Four Divisions

一月 9, 2007 by billrain
Filed under: Cobol 

The Four Divisions

Introduction

At the top of the COBOL hierarchy are the four divisions. These divide the program into distinct structural elements. Although some of the divisions may be omitted, the sequence in which they are specified is fixed, and must follow the order below.

IDENTIFICATION DIVISION.
Contains program information

ENVIRONMENT DIVISION.
Contains environment information

DATA DIVISION.
Contains data descriptions

PROCEDURE DIVISION.
Contains the program algorithms


The IDENTIFICATION DIVISION

The IDENTIFICATION DIVISION supplies information about the program to the programmer and the compiler.

Most entries in the IDENTIFICATION DIVISION are directed at the programmer. The compiler treats them as comments.

The PROGRAM-ID clause is an exception to this rule. Every COBOL program must have a PROGRAM-ID because the name specified after this clause is used by the linker when linking a number of subprograms into one run unit, and by the CALL statement when transferring control to a subprogram.

The IDENTIFICATION DIVISION has the following structure:

IDENTIFICATION DIVISION
PROGRAM-ID. NameOfProgram.
[AUTHOR. YourName.]
other entries here

The keywords – IDENTIFICATION DIVISION – represent the division header, and signal the commencement of the program text.

PROGRAM-ID is a paragraph name that must be specified immediately after the division header.

NameOfProgram is a name devised by the programmer, and must satisfy the rules for user-defined names.

Here’s a typical program fragment:

IDENTIFICATION DIVISION.PROGRAM-ID. SequenceProgram.AUTHOR. Michael Coughlan.

The ENVIRONMENT DIVISION

The ENVIRONMENT DIVISION is used to describe the environment in which the program will run.

The purpose of the ENVIRONMENT DIVISION is to isolate in one place all aspects of the program that are dependant upon a specific computer, device or encoding sequence.

The idea behind this is to make it easy to change the program when it has to run on a different computer or one with different peripheral devices.

In the ENVIRONMENT DIVISION, aliases are assigned to external devices, files or command sequences. Other environment details, such as the collating sequence, the currency symbol and the decimal point symbol may also be defined here.


The DATA DIVISION

As the name suggests, the DATA DIVISION provides descriptions of the data-items processed by the program.

The DATA DIVISION has two main sections: the FILE SECTION and the WORKING-STORAGE SECTION. Additional sections, such as the LINKAGE SECTION (used in subprograms) and the REPORT SECTION (used in Report Writer based programs) may also be required.

The FILE SECTION is used to describe most of the data that is sent to, or comes from, the computer’s peripherals.

The WORKING-STORAGE SECTION is used to describe the general variables used in the program.

The DATA DIVISION has the following structure and syntax:

Below is a sample program fragment -

IDENTIFICATION DIVISION.PROGRAM-ID. SequenceProgram.AUTHOR. Michael Coughlan.DATA DIVISION.WORKING-STORAGE SECTION.01  Num1           PIC 9  VALUE ZEROS.01  Num2           PIC 9  VALUE ZEROS.01  Result         PIC 99 VALUE ZEROS.

The PROCEDURE DIVISION

The PROCEDURE DIVISION contains the code used to manipulate the data described in the DATA DIVISION. It is here that the programmer describes his algorithm.

The PROCEDURE DIVISION is hierarchical in structure and consists of sections, paragraphs, sentences and statements.

Only the section is optional. There must be at least one paragraph, sentence and statement in the PROCEDURE DIVISION.

Paragraph and section names in the PROCEDURE DIVISION are chosen by the programmer and must conform to the rules for user-defined names.

Sample Program

IDENTIFICATION DIVISION.PROGRAM-ID. SequenceProgram.AUTHOR. Michael Coughlan.                     
DATA DIVISION.WORKING-STORAGE SECTION.01 Num1 PIC 9 VALUE ZEROS.01 Num2 PIC 9 VALUE ZEROS.01 Result PIC 99 VALUE ZEROS.
PROCEDURE DIVISION.CalculateResult.ACCEPT Num1.ACCEPT Num2.MULTIPLY Num1 BY Num2 GIVING Result.DISPLAY "Result is = ", Result.STOP RUN.     

Some COBOL compilers require that all the divisions be present in a program while others only require the IDENTIFICATION DIVISION and the PROCEDURE DIVISION. For instance the program shown below is perfectly valid when compiled with the Microfocus NetExpress compiler.

Minimum COBOL program

IDENTIFICATION DIVISION.PROGRAM-ID.  SmallestProgram.
PROCEDURE DIVISION.DisplayGreeting.DISPLAY "Hello world".STOP RUN.     
VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Comments

Tell me what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!





  • 分类目录

  • 功能

  • 联络订阅

    google reader
    订阅到抓虾
    订阅到鲜果
    订阅到飞鸽
    bloglines
    my yahoo
    netvibes