Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

INTRODUCTION TO COBOL

PROGRAMMING

CISB323 Business Programming

Chapter 1

Structure of COBOL Programs


PROGRAM
DIVISION(s)
SECTION(s)
Paragraph(s)
Sentence(s)
Statement(s)

A COBOL Program
000100
000200
000300
000400
000500
000600
000700
000800
000900
001000
001100

IDENTIFICATION DIVISION.
PROGRAM-ID. Hello.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
PROGRAM-BEGIN. Paragraph
DISPLAY "Hello world".
Statement

PROGRAM-DONE.
STOP RUN.

Divisions

Sentence

COBOL Program Parts : Divisions


There are four divisions in COBOL.
IDENTIFICATION DIVISION
ENVIRONMENT DIVISION
DATA DIVISION
PROCEDURE DIVISION
DON'T make typing errors. Some of the typing errors that can cause
serious problems when you are compiling include misspelling the name
of a DIVISION (for example, INDENTIFICATION DIVISION), adding an
unnecessary hyphen (for example, DATA-DIVISION), or omitting any of
the periods. Note that everything ends with a period (.)

COBOL program parts : Divisions


IDENTIFICATION DIVISION marks the beginning of a
COBOL program. The name of the program, which
you assign, will be entered as a statement in this
division
The program name should be unique for each
program and should be linked with the name of source
file
This division is used to identify basic information about
the program. In previous example, the
IDENTIFICATION DIVISION contains only the
PROGRAM-ID, Hello.

COBOL program parts : Divisions


IDENTIFICATION DIVISION must contain at least the
program name. Other useful information that you can
include are:
AUTHOR - so we will know who to blame when things go
wrong!
DATE-WRITTEN - this gives us the age of the original version.
DATE-COMPILED - this is automatically inserted by the
COBOL compiler, and shows the age of the current version of
this program.

COBOL program parts : Divisions


ENVIRONMENT DIVISION, which is used to identify
the environment in which the program is running.
Remember that COBOL is intended to run on many
different types of machines, and this section is used to
handle the differences between various computers.
Contains statements or commands to describe the
physical environment in which the program is running.
The main use of the ENVIRONMENT DIVISION is to
describe the physical structure of files that will be used
in the program.

COBOL program parts : Divisions


DATA DIVISION contains statements describing the
data used by the program.
Contain any data that the program operates on.
All the data that the program uses will be defined in
this division.
DATA DIVISION and PROCEDURE DIVISION are the
most important divisions in a COBOL program; they
do 95 percent of the work.

COBOL program parts : Divisions


PROCEDURE DIVISION. This is the meat of the
program--the part that does the work intended by the
programmer.
Contains the COBOL statements that the program will
execute after the program starts running.
The real workhorse of a COBOL program.
Without a PROCEDURE DIVISION, you wouldn't have
a program, because all the other divisions are used to
create the environment and data that are used by the
PROCEDURE DIVISION to actually do something.

COBOL program parts : Program-ID and Stop Run

Most compilers require that only two things be present in


a COBOL program--other than the four divisions--in
order to compile it:
PROGRAM-ID
STOP RUN

PROGRAM-ID is a paragraph that must appear in the


IDENTIFICATION DIVISION and is used to give the
program a name.
There must also be one paragraph in the PROCEDURE
DIVISION that contains the STOP RUN statement.
Most versions of COBOL require this explicit command
as a way of identifying the point in the program where
the program terminates.

COBOL Sample program


000100
000200
000300
000400
000500
000600
000700
000800
000900
001000
001100
001200

IDENTIFICATION DIVISION.
PROGRAM-ID. Sentences.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
PROGRAM-BEGIN.
DISPLAY "This program contains four DIVISIONS,".
DISPLAY "three PARAGRAPHS".
DISPLAY "and four SENTENCES".
PROGRAM-DONE.
STOP RUN.

COBOL Source Code


A COBOL source code file has five areas, extending
from left to right across the page.

COBOL Source Code


The first six characters or columns of a line are called
the sequence number area.
This area is not processed by the compiler, or if it is
processed, it provides you only with warnings that numbers
are out of sequence (if they are).

Character position 7 is called the indicator area.


This seventh position is usually blank.
If an asterisk (*) is placed in this column, everything else on
that line is ignored by the compiler.
This is used as a method to include comments in your
source code file.

COBOL source code : Area A vs Area B


The four character positions 8 through 11 are called
Area A.
DIVISIONs and paragraphs (and SECTIONs) must start in
Area A.
It is good coding practice to start DIVISIONs, SECTIONs,
and paragraph names at column 8 rather than some random
place in Area A.

Character positions 12 through 72 are called Area B.


Sentences must start and end within Area B.
It is good coding practice to start sentences at column 12
rather than some random place in Area B.

COBOL source code : Area A vs Area B


COBOL was designed as an 80-column language, but
there is no formal definition of character positions 73
through 80. This is called the identification area (which
has nothing to do with the IDENTIFICATION
DIVISION).
Some special COBOL editors place a modification
code automatically in positions 73 through 80.
This method of marking lines as modified usually
depends on a special editor set up for COBOL that
inserts these codes automatically.
You probably will never see modification codes using
COBOL on a PC.

COBOL source code : Area A vs Area B

000100
IDENTIFICATION DIVISION.
000200
PROGRAM-ID. Comment.
000300
ENVIRONMENT DIVISION.
000400
DATA DIVISION.
MB072197
000500
PROCEDURE DIVISION.
000600
000700 * This is a comment.
MB072197
000800 * This paragraph displays information about the program.
000900
PROGRAM-BEGIN.
003700
DISPLAY "This program contains four DIVISIONS,".
003800
DISPLAY "three PARAGRAPHS".
MB072197
001000
DISPLAY "and four SENTENCES".
001100
PROGRAM-DONE.
001200
STOP RUN.

MB072197
MB072197

What is a Shell Program?


A COBOL program that contains the minimum
requirements usually is called a shell program, a
skeleton program, or a boilerplate program.
IDENTIFICATION DIVISION.
PROGRAM-ID. COBSHL01.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
PROGRAM-BEGIN.
PROGRAM-DONE.
STOP RUN.

GUESS
THE
OUTPUT!

Programming Style
All user-defined names must adhere to following
rules:
Must contain at least 1 character and not more than 30 characters
Must contain at least one alphabetic character and must not begin or
end with a hyphen
Must be constructed from the characters A to Z, numbers 0 to 9, and
hyphen
Names are not case-sensitive. But it is good a good programming
practice to use uppercase for reserved words and mixed case for userdefined names
COBOL reserved words cannot be used as user-defined name

Proper indentation is most necessary to understand


your codes!

Exercises
Answers the following questions based on codes below:
000100
000200
000300
000400
000500
000600
000700
000800
000900
001000

IDENTIFICATION DIVISION.
PROGRAM-ID. BYEBYE.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
PROGRAM-BEGIN.
DISPLAY "Bye bye birdie".
PROGRAM-DONE.
STOP RUN.

1. What is the output of the following program?


2. How many DIVISIONs are in the following program, byebye.cbl?
3. How many paragraphs?
4. How many sentences?

Exercises
6. What is wrong with the following, class02.cbl?
000100
000200
000300
000400
000500
000600
000700
000800
000900
001000

IDENTIFICATION DIVISION.
PROGRAM-ID. CLASS.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
PROGRAM-BEGIN.
DISPLAY First class".
PROGRAM-DONE.
STOP RUN.

Exercises
7. What is wrong with the following, class03.cbl?
000100
000200
000300
000400
000500
000600
000700
000800
000900
001000

IDENTIFICATION DIVISION.
PROGRAM-ID. CLASS.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
This program display a message.
PROGRAM-BEGIN.
DISPLAY First class".
PROGRAM-DONE.
STOP RUN.

You might also like