Cobol Programing Basics.
Home Pages : Pages : 1 2 3 4 5 6 7 8 9 10

Introduction

This section presents the fundamentals of constructing COBOL programs. It explains the notation used in COBOL syntax diagrams and enumerates the COBOL coding rules. It shows how user-defined names are constructed and examines the structure of COBOL programs.

COBOL syntax

COBOL syntax is defined using particular notation sometimes called the COBOL MetaLanguage.

In this notation, words in uppercase are reserved words. When underlined they are mandatory. When not underlined they are "noise" words, used for readability only, and are optional. Because COBOL statements are supposed to read like English sentences there are a lot of these "noise" words.

Words in mixed case represent names that must be devised by the programmer (like data item names).

When material is enclosed in curly braces { }, a choice must be made from the options within the braces. If there is only one option then that item in mandatory.

Material enclosed in square brackets [ ], indicates that the material is optional, and may be included or omitted as required.

The ellipsis symbol ... (three dots), indicates that the preceding syntax element may be repeated at the programmer's discretion.

COBOL coding rules

Traditionally, COBOL programs were written on coding forms and then punched on to punch cards. Although nowadays most programs are entered directly into a computer, some COBOL formatting conventions remain that derive from its ancient punch-card history.

On coding forms, the first six character positions are reserved for sequence numbers. The seventh character position is reserved for the continuation character, or for an asterisk that denotes a comment line.

The actual program text starts in column 8. The four positions from 8 to 11 are known as Area A, and positions from 12 to 72 are Area B.

Although many COBOL compilers ignore some of these formatting restrictions, most still retain the distinction between Area A and Area B.

When a COBOL compiler recognizes the two areas, all division names, section names, paragraph names, FD entries and 01 level numbers must start in Area A. All other sentences must start in Area B.

In our example programs we use the compiler directive (available with the NetExpress COBOL compiler) - $ SET SOURCEFORMAT"FREE" - to free us from these formatting restrictions.

Ancient COBOL coding form

Name construction

vAll user-defined names, such as data names, paragraph names, section names condition names and mnemonic names, must adhere to the following rules:

vThey must contain at least one character, but not more than 30 characters. They must contain at least one alphabetic character. They must not begin or end with a hyphen.

vThey must be constructed from the characters A to Z, the numbers 0 to 9, and the hyphen.

vThey must not contain spaces.

vNames are not case-sensitive: TotalPay is the same as totalpay, Totalpay or TOTALPAY

The structure of COBOL programs

COBOL programs are hierarchical in structure. Each element of the hierarchy consists of one or more subordinate elements.

The hierarchy consists of Divisions, Sections, Paragraphs, Sentences and Statements.

A Division may contain one or more Sections, a Section one or more Paragraphs, a Paragraph one or more Sentences and a Sentence one or more Statements.

We can represent the COBOL hierarchy using the COBOL metalanguage as follows;

Divisions
A division is a block of code, usually containing one or more sections, that starts where the division name is encountered and ends with the beginning of the next division or with the end of the program text.


Sections
A section is a block of code usually containing one or more paragraphs. A section begins with the section name and ends where the next section name is encountered or where the program text ends.

Section names are devised by the programmer, or defined by the language. A section name is followed by the word SECTION and a period.
See the two example names below
-

SelectUnpaidBills SECTION.
FILE SECTION.

Paragraphs
A paragraph is a block of code made up of one or more sentences. A paragraph begins with the paragraph name and ends with the next paragraph or section name or the end of the program text.

A paragraph name is devised by the programmer or defined by the language, and is followed by a period.
See the two example names below -

PrintFinalTotals.
PROGRAM-ID.

Sentences and statements
A sentence consists of one or more statements and is terminated by a period.
For example:

MOVE .21 TO VatRate
MOVE 1235.76 TO ProductCost
COMPUTE VatAmount = ProductCost * VatRate.


A statement consists of a COBOL verb and an operand or operands.
For example
:

SUBTRACT Tax FROM GrossPay GIVING NetPay

The Four Divisions

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.

General Layout

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. ProgramName.
[AUTHOR. ProgramerName.]
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:

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:

IDENTIFICATION DIVISION.
PROGRAM-ID. SequenceProgram.
AUTHOR. XXXXX.


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 COBOL 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.

Pages : 1 2 3 4 5 6 7 8 9 10