Professional Documents
Culture Documents
Introduction To COBOL
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
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.
09/03/16
ER/CORP/CRS/LA0
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
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
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
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
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.
09/03/16
ER/CORP/CRS/LA0
14
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
09/03/16
ER/CORP/CRS/LA0
16
Statements
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
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
09/03/16
ER/CORP/CRS/LA0
21
Variables
01 STUDENT-NAME
STUDENT-NAME
09/03/16
ER/CORP/CRS/LA0
22
Variables
01 STUDENT-NAME
STUDENT-NAME
J O H N
09/03/16
ER/CORP/CRS/LA0
23
Variables
01 STUDENT-NAME
STUDENT-NAME
O H
09/03/16
My name is JOHN
ER/CORP/CRS/LA0
24
ER/CORP/CRS/LA0
25
Clauses...
Some examples
PICTURE 999
PICTURE S999
PICTURE XXXX
PICTURE 99V99
PICTURE S9V9
ER/CORP/CRS/LA0
26
Literal(s)
09/03/16
ER/CORP/CRS/LA0
27
Constants
==
==
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
a level number,
a data name
picture clause.
Items
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
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
ER/CORP/CRS/LA0
35