Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 35

INTRODUCTION TO COBOL

HISTORY OF COBOL
> During the 1950s, the western parts of the world
experienced a tremendous need for a high level
programming language suitable for business data
processing.
> To meet this demand the U.S. Dept of Defence convened a
conference on the 28th and 29th of May, 1958 to highlight
and stress the importance of a high-level business data
processing language.
The conference led to the formation of a team called
CODASYL (Conference On Data Systems Language).
09/03/16

ER/CORP/CRS/LA0

CONSTITUENTS OF A COBOL PROGRAM

CHARACTERS
The most basic and indivisible unit of the COBOL language is the
character, e.g. A, b, $, /, + etc
The individual characters are combined to form character-strings. A
character-string is a sequence of contiguous characters that forms a
COBOL word which may be of the following three types (1) Reserved
word, (2) User-defined word and (3) Literal, also known as a constant.

RESERVED WORDS Keywords, Optional-Words (e.g. GIVING,


AFTER etc)
USER-DEFINED WORDS Different rules apply Refer to
Material. Typical examples would be Working-Storage variables or
Paragraph names.
LITERALS

09/03/16

ER/CORP/CRS/LA0

CONSTITUENTS OF A COBOL PROGRAM

PHRASE
A phrase is an ordered set of COBOL words that form a portion of a COBOL statement
or a clause.
Examples: AT END, UNTIL COUNT, END-PERFORM, etc.

CLAUSE
Examples: PICTURE X (5), VALUE, SELECT. . . ASSIGN etc
STATEMENT
A statement is a valid combination of a COBOL verb and its operands. It specifies an action to
be taken by the object program. The COBOL statements can be broadly classified into two
types viz. Imperative statements and Conditional statements. An imperative statement begins
with a verb and specifies an unconditional action to be taken. On the other hand, a conditional
statement is one in which the action to be taken is determined by some condition that is
evaluated when the program is executed.

PARAGRAPH
A paragraph is a collection of sentences that form a logical unit in a COBOL program. It is the
basic unit of organisation of a COBOL program and is referred by a user-defined name in the
program.

SECTION
A SECTION is a collection of related paragraphs.

09/03/16

ER/CORP/CRS/LA0

Structure of a COBOL program


Program
Program
Division
Division
Section
Section
Paragraph
Paragraph
Sentence
Sentence
09/03/16

Statement
Statement
ER/CORP/CRS/LA0

CONSTITUENTS OF A COBOL
PROGRAM

DIVISION
A DIVISION is the largest unit in the COBOL program
and is a collection of SECTIONs and/or paragraphs.
Every COBOL program is divided into four DIVISIONs
viz. IDENTIFICATION DIVISION, ENVIRONMENT
DIVISION, DATA DIVISION and PROCEDURE
DIVISION. Although the ENVIRONMENT DIVISION
and DATA DIVISION are optional, most programs will
contain all the four DIVISIONs.

09/03/16

ER/CORP/CRS/LA0

The Four
Divisions
DIVISIONS are used to identify the principal components of

the program text. There are four DIVISIONS in all.


IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
Some divisions are optional but, the order

shown above.

09/03/16

ER/CORP/CRS/LA0

must follow as

IDENTIFICATION DIVISION
The IDENTIFICATION DIVISION is the first and the least significant
DIVISION of every COBOL source program.
The IDENTIFICATION DIVISION is used to supply information
about the program to the programmer and to the compiler.
It has no effect on the execution of the program, but is nevertheless,
mandatory. It is further divided into paragraphs, not SECTIONs.
The only mandatory paragraph of this DIVISION is the PROGRAMID paragraph.
IDENTIFICATION DIVISION.
PROGRAM-ID.
Program-name.
AUTHOR.
Comment-entry.
INSTALLATION.
Comment-entry.
DATE-WRITTEN.
Comment-entry.
DATE-COMPILED.
Comment-entry.
SECURITY.
Comment-entry.
NOTE Our compiler generates the DATE-COMPILED on its own at
the time of compilation.

09/03/16

ER/CORP/CRS/LA0

ENVIRONMENT DIVISION
The ENVIRONMENT DIVISION is an optional DIVISION and
comprises of the two SECTIONs namely CONFIGURATION
SECTION and INPUT-OUTPUT SECTION.
The CONFIGURATION SECTION consists of two paragraphs
namely SOURCE-COMPUTER paragraph and OBJECTCOMPUTER paragraph. The SOURCE-COMPUTER paragraph
specifies the computer on which the source program is to be compiled
and the OBJECT-COMPUTER paragraph specifies the computer for
which the object program is designated.
The INPUT-OUTPUT SECTION consists of two paragraphs namely
FILE CONTROL paragraph and I-O CONTROL paragraph. In the
FILE CONTROL paragraph a file name is selected for each file to be
used in the program and assigned to a device.
The I-O CONTROL paragraph specifies information needed for
efficient transmission of data between the external file and the
COBOL program.

09/03/16

ER/CORP/CRS/LA0

DATA DIVISION
The DATA DIVISION is used to declare the data items that will be
processed in the PROCEDURE DIVISION of the source program.
The 2 principal sections are FILE SECTION and WORKINGSTORAGE SECTION.
The FILE SECTION is used to declare the FD (File descriptor) and
SD (Sort descriptor) entries for files and sort files used in the
program.
The WORKING-STORAGE SECTION is used to declare all the
temporary variables and record structures used in the program.
The LINKAGE SECTION is used to describe data items made
available from another program. It is important to note that storage
is not allocated to the data structures defined in the LINKAGE
SECTION as the data exists elsewhere.

09/03/16

ER/CORP/CRS/LA0

10

Structure
IDENTIFICATION DIVISION.
Program Details
DATA DIVISION.
Data Descriptions

NNOTE
OTE

The
Thekeyword
keyword
DIVISION
DIVISIONand
andaa
full-stop
full-stopisisused
usedinin
every
everycase.
case.

PROCEDURE DIVISION.
Algorithm Description

09/03/16

ER/CORP/CRS/LA0

11

BASIC DATA TYPES


There are five basic data types in COBOL. They are
Alphabetic (Made up of only upper and lower case letters),
Numeric (Made up of the digits 0, 1, 2, . . . ,9)
Alphanumeric data (Made of both letters and digits),
Edited Numeric (Made up of digits and special characters) and
Edited Alphanumeric (Made up of letters, digits and special characters).
LITERALS
A Literal is a symbol whose value does not change in a program. It is also known as
constant. There are three types of literals in COBOL namely
Numeric literal, Non-numeric literal and Figurative constant (e.g. ZEROES, SPACES,
HIGH-VALUES, LOW-VALUES)

DATA NAMES
Data-names are named memory locations. They must be described in the DATA
DIVISION before they can be used in the PROCEDURE DIVISION.
Data-names are described with the aid of the following
Level Number, PICTURE Clause and VALUE Clause.

09/03/16

ER/CORP/CRS/LA0

12

PICTURE CLAUSE
The PICTURE clause specifies the data type and the amount of storage required for a data
item. It is denoted by PICTURE (often abbreviated as PIC). A PICTURE clause is
specified only for elementary data items.

The following are the general picture characters and their meaning.
A for alphabetic
X for alphanumeric
9 for numeric
S for sign

V for implied decimal point.

USAGE CLAUSE
In COBOL, a programmer is allowed to specify the internal form of the data item so as to
facilitate its use in the most efficient manner. Broadly speaking, there are only two general
forms of internal representation namely COMPUTATIONAL and DISPLAY.
Only numeric data items can be specified as USAGE IS COMPUTATIONAL and the name
itself suggests a data item specified as USAGE IS COMPUTATIONAL can take part in
arithmetic operations more efficiently.

09/03/16

ER/CORP/CRS/LA0

13

USAGE IS DISPLAY
The default USAGE of a data item is DISPLAY.
Each character of the data is represented in one byte

USAGE IS COMPUTATIONAL
In this case the data item is represented in pure binary. The item must be an integer.
Depending on the size of the data item, it can be stored either in a half-word (2 bytes with
range 32,768 to +32767) or full-word (4 bytes with range 2,147,483,648 to
2,147,483,647).
The PICTURE Clause of a COMPUTATIONAL data item should not contain any character
other than 9 or S.

REDEFINES CLAUSE
The REDEFINES clause allows you to use different data description entries to
describe the same computer storage area.

BLANK WHEN ZERO CLAUSE


The BLANK WHEN ZERO clause specifies that an item contains nothing but
spaces when its value is zero. It can be specified only for elementary numeric or
numeric-edited items.

09/03/16

ER/CORP/CRS/LA0

14

DATA DIVISION Syntax

The DATA DIVISION has the following structure


DATA DIVISION.
FILE SECTION.

File Section entries.


WORKING-STORAGE SECTION.

WS entries.

IDENTIFICATION
IDENTIFICATIONDIVISION.
DIVISION.
PROGRAM-ID.
PROGRAM-ID. FIRSTPGM.
FIRSTPGM.
AUTHOR.
AUTHOR. Michael
MichaelCoughlan.
Coughlan.
DATA
DATADIVISION.
DIVISION.
WORKING-STORAGE
WORKING-STORAGESECTION.
SECTION.
01
PIC
01 WS-NUM-1
WS-NUM-1
PIC9(001)
9(001)VALUE
VALUEZEROS.
ZEROS.
01
PIC
01 WS-NUM-2
WS-NUM-2
PIC9(001)
9(001)VALUE
VALUEZEROS.
ZEROS.
01
WS-RESULT-1
PIC
9(002)
VALUE
ZEROS.
01
WS-RESULT-1
PIC
9(002)
VALUE
ZEROS.
09/03/16
ER/CORP/CRS/LA0

15

Paragraph
s

Each section consists of one or more 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.
The paragraph-name consists of a name devised by the
programmer or defined by the language followed by a full
stop.
P0000-PRINT-FINAL-TOTALS.
PROGRAM-ID.

09/03/16

ER/CORP/CRS/LA0

16

Statements

A PARAGRAPH consists of one or more sentences.


A SENTENCE consists of one or more statements and is
terminated by a full stop.

MOVE .21 TO VAT-RATE


COMPUTE VAT-AMOUNT = PRODUCT-COST * VAT-RATE.
DISPLAY "Enter Name " WITH NO ADVANCING
ACCEPT STUDENT-NAME
DISPLAY "Name Entered was " STUDENT-NAME.

A STATEMENT consists of a COBOL verb and an operand or


operands.

SUBTRACT T-TAX FROM GROSS-PAY GIVING NET-PAY


READ STUDENT-FILE
AT END SET END-OF-FILE TO TRUE
END-READ

09/03/16

ER/CORP/CRS/LA0

17

program
IDENTIFICATION
IDENTIFICATIONDIVISION.
DIVISION.
PROGRAM-ID.FIRSTPGM.
PROGRAM-ID.FIRSTPGM.
AUTHOR.
AUTHOR.Infosys.
Infosys.
DATA
DIVISION.
DATA DIVISION.
WORKING-STORAGE
WORKING-STORAGESECTION.
SECTION.
01
WS-NUM-1
PIC
01 WS-NUM-1
PIC9(001)
9(001) VALUE
VALUEZEROS.
ZEROS.
01
WS-NUM-2
PIC
9(001)
VALUE
ZEROS.
01 WS-NUM-2
PIC 9(001) VALUE ZEROS.
01
PIC
01 WS-RESULT-1
WS-RESULT-1
PIC9(002)
9(002) VALUE
VALUEZEROS.
ZEROS.
PROCEDURE
DIVISION.
PROCEDURE DIVISION.
A0000-MAIN-PARA.
A0000-MAIN-PARA.
ACCEPT
ACCEPTWS-NUM-1
WS-NUM-1
ACCEPT
WS-NUM-2
ACCEPT WS-NUM-2
MULTIPLY
MULTIPLYWS-NUM-1
WS-NUM-1BY
BYWS-NUM-2
WS-NUM-2GIVING
GIVING
DISPLAY
DISPLAY"Result
"Resultisis==",",WS-RESULT-1
WS-RESULT-1
STOP
RUN
STOP RUN
..

09/03/16

WS-RESULT-1
WS-RESULT-1

ER/CORP/CRS/LA0

18

rules

Almost all COBOL compilers treat a line of COBOL code as if it


contained two distinct areas. These are known as; Area A and

Area B

When a COBOL compiler recognizes these two areas, all division,


section, paragraph names, FD entries and 01 level entries must
start in Area A. All other sentences/statements must start in Area
B.

Area A is four characters wide and is followed by Area B.

09/03/16

ER/CORP/CRS/LA0

19

DATA
There are basically three kinds of data
used in COBOL programs;
Variables.
Literal(s).
Figurative Constants.

09/03/16

ER/CORP/CRS/LA0

20

Variables

A variable is a named location in memory into which a


program can put data and from which it can retrieve data.

A data-name or identifier is the name used to identify the


area of memory reserved for the variable.

Variables must be described in terms of their type and


size.

Every variable used in a COBOL program must have a


description in the DATA DIVISION.

09/03/16

ER/CORP/CRS/LA0

21

Variables
01 STUDENT-NAME

PIC X(006) VALUE SPACES.

MOVE "JOHN" TO STUDENT-NAME.


DISPLAY "My name is ", STUDENT-NAME.

STUDENT-NAME

09/03/16

ER/CORP/CRS/LA0

22

Variables
01 STUDENT-NAME

PIC X(006) VALUE SPACES.

MOVE "JOHN" TO STUDENT-NAME.


DISPLAY "My name is ", STUDENT-NAME.

STUDENT-NAME

J O H N

09/03/16

ER/CORP/CRS/LA0

23

Variables
01 STUDENT-NAME

PIC X(006) VALUE SPACES.

MOVE "JOHN" TO STUDENT-NAME.


DISPLAY "My name is ", STUDENT-NAME.

STUDENT-NAME

O H

09/03/16

My name is JOHN

ER/CORP/CRS/LA0

24

COBOL PICTURE Clause symbols

To create the required picture the programmer uses a set


of symbols.

The following symbols are used frequently in picture


clauses;
9 (the digit nine) is used to indicate the occurrence of a
digit at the corresponding position in the picture.
X (the character X) is used to indicate the occurrence
of any character from the character set at the
corresponding position in the picture
V (the character V) is used to indicate position of the
decimal point in a numeric value! It is often referred
to as the assumed decimal point character.
S (the character S) indicates the presence of a sign and
can only appear at the beginning of a picture.
09/03/16

ER/CORP/CRS/LA0

25

Clauses...

Some examples
PICTURE 999
PICTURE S999
PICTURE XXXX
PICTURE 99V99
PICTURE S9V9

a three digit (+ve only) integer


a three digit (+ve/-ve) integer
a four character text item or string
a +ve real in the range 0 to 99.99
a +ve/-ve real in the range ?

If you wish you can use the abbreviation PIC.

Numeric values can have a maximum of 18 (eighteen) digits


(i.e. 9s).

The limit on string values is usually 256 and it is systemdependent.


09/03/16

ER/CORP/CRS/LA0

26

Literal(s)

String/Alphanumeric literal(s) are enclosed in quotes and


may consists of alphanumeric characters
e.g. "Michael Ryan", "-123", "123.45"

Numeric literal(s) may consist of numerals, the decimal


point and the plus or minus sign. Numeric literal(s) are
not enclosed in quotes.
e.g. 123, 123.45, -256, +2987

09/03/16

ER/CORP/CRS/LA0

27

Constants

COBOL provides its own, special constants called


Figurative Constants.
SPACE
SPACEor
orSPACES
SPACES
ZERO
ZEROor
orZEROS
ZEROSor
orZEROS
ZEROS

==

==

00

QUOTE
QUOTEor
orQUOTES
QUOTES
HIGH-VALUE
HIGH-VALUEor
orHIGH-VALUES
HIGH-VALUES

==

""

==

Max
MaxValue
Value

LOW-VALUE
LOW-VALUEor
orLOW-VALUES
LOW-VALUES
ALL
ALLliteral
literal

==

Min
MinValue
Value

==

Fill
FillWith
WithLiteral
Literal

ER/CORP/CRS/LA0

28

09/03/16

Examples
01
01 GROSS-PAY
GROSS-PAY
ZERO
MOVE
MOVE ZEROS
ZEROES

PIC
PIC 9(005)V99
9(005)V99 VALUE
VALUE 13.5.
13.5.

TO
TO GROSS-PAY.
GROSS-PAY.

GROSS-PAY

0 0 0 1 3 5 0

01
01 STUDENT-NAME
STUDENT-NAME

PIC
PIC X(010)
X(010) VALUE
VALUE "MIKE".
"MIKE".

MOVE
MOVE ALL
ALL "-"
"-" TO
TO STUDENT-NAME.
STUDENT-NAME.
STUDENT-NAME

M I K E
09/03/16

ER/CORP/CRS/LA0

29

Examples...
01
01 GROSS-PAY
GROSS-PAY
ZERO
MOVE
MOVE ZEROS
ZEROES

PIC
PIC 9(5)V99
9(5)V99 VALUE
VALUE 13.5.
13.5.
TO
TO GROSS-PAY.
GROSS-PAY.

GROSS-PAY

0 0 0 0 0 0 0

01
01 STUDENT-NAME
STUDENT-NAME PIC
PIC X(10)
X(10) VALUE
VALUE "MIKE".
"MIKE".
MOVE
MOVE ALL
ALL "-"
"-" TO
TO STUDENT-NAME.
STUDENT-NAME.
STUDENT-NAME

- - - - - - - - - -

09/03/16

ER/CORP/CRS/LA0

30

items

In COBOL the term group item is used to describe a data


item which has been further subdivided.

A Group item is declared using a level number and a data name.


It cannot have a picture clause.
Where a group item is the highest item in a data hierarchy it is
referred to as a record and uses the level number 01.

The term elementary item is used to describe data items


which are atomic, that is, not further subdivided.
An elementary item declaration consists of;

a level number,
a data name
picture clause.

An elementary item must have a picture clause.


Every group or elementary item declaration must be followed
by a full stop.
09/03/16
ER/CORP/CRS/LA0
31

Items

Picture clauses are NOT specified for group data items


because the size of a group item is the sum of the sizes of its
subordinate, elementary items and its type is always
assumed to be PIC X.

The type of a group items is always assumed to be PIC X,


because group items may have several different data items
and types subordinate to them.

An X picture is the only one which could support such


collections.

09/03/16

ER/CORP/CRS/LA0

32

Example
WORKING-STORAGE
WORKING-STORAGE SECTION.
SECTION.
01
STUDENT-DETAILS
PIC
01 STUDENT-DETAILS
PIC X(026).
X(026).

STUDENT-DETAILS
H E N N E S S Y R M 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F

09/03/16

ER/CORP/CRS/LA0

33

Example
WORKING-STORAGE
WORKING-STORAGE SECTION.
SECTION.
01
STUDENT-DETAILS.
01 STUDENT-DETAILS.
02
PIC
02 STUDENT-NAME
STUDENT-NAME
PIC X(010).
X(010).
02
STUDENT-ID
PIC
9(007).
02 STUDENT-ID
PIC 9(007).
02
PIC
02 COURSE-CODE
COURSE-CODE
PIC X(004).
X(004).
02
GRANT
PIC
9(004).
02 GRANT
PIC 9(004).
02
GENDER
PIC
02 GENDER
PIC X(001).
X(001).

STUDENT-DETAILS
H EN N E S S Y RM 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F
STUDENT-NAME

09/03/16

STUDENT-ID

COURSE-CODE GRANT GENDER

ER/CORP/CRS/LA0

34

Items/Records
WORKING-STORAGE
WORKING-STORAGE SECTION.
SECTION.
01
STUDENT-DETAILS.
01 STUDENT-DETAILS.
02
02 STUDENT-NAME.
STUDENT-NAME.
03
PIC
03 SURNAME
SURNAME
PIC X(008).
X(008).
03
PIC
03 INITIALS
INITIALS
PIC X(002).
X(002).
02
STUDENT-ID
PIC
9(007).
02 STUDENT-ID
PIC 9(007).
02
COURSE-CODE
PIC
02 COURSE-CODE
PIC X(004).
X(004).
02
PIC
02 GRANT
GRANT
PIC 9(004).
9(004).
02
GENDER
PIC
X(001).
02 GENDER
PIC X(001).

STUDENT-DETAILS
H EN N E S S Y RM 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F
STUDENT-NAME
SURNAME

09/03/16

STUDENT-ID
INITIALS

COURSE-CODE GRANT GENDER

ER/CORP/CRS/LA0

35

You might also like