COBOL (COMMON BUSINESS ORIENTED LANGUAGE)
Overview
COBOL Fundamentals DAY1
Session Plan
Day 1:
Introduction to COBOL Evolution, Features & Language Fundamentals Program Structure Data description entry
References
M.K.Roy and D. Ghosh Dastidar, COBOL Programming, Tata McGraw Hill, New York, 1973.
Nancy Stern and Robert Stern, COBOL Programming, John Wiley & Sons, New York, 1973.
Newcomer and Lawrence, Programming with Structured COBOL, McGraw Hill Books, New York, 1973.
History of COBOL
1959 – United States Department of Defense 1960 - COBOL initial specifications presented by CODASYL (Conference on Data Systems Languages)
1964 – BASIC COBOL extended to Visual COBOL 1968 – ANSI (American National Standards Institute) developed American National Standard (ANS) COBOL
1974 – ANSI published revised version of (ANS) COBOL – Business applications needed to manipulate character as well as numeric data – String operations added
1985 – COBOL 85 Standards was introduced with revised version of COBOL-74.
COBOL What does COBOL stand for? COmmon Business Oriented Language.
Which are target area of COBOL applications? Defense, Aircraft, Insurance, Finance, Retail etc (file & data oriented applications involved)
So we can say that COBOL is basically used for writing business applications and not for developing system software
COBOL – Program Structure PROGRAM
DIVISIONS
SECTIONS
PARAGRAPHS
SENTENCES
STATEMENTS
Principal portions of a program. There are 4 divisions – a) Identification (Required) b) Environment (Optional) Userc) defined of code Datachunk (Optional) which consists of one/more d) Procedure (Required) paragraphs. e.g. User defined chunk of code a) U000-CHECK-LOG SECTION. which consists of one/more b) FILE SECTION. sentences. e.g. A consists of one or a) SENTENCE P000-PRINT-FINAL-TOTALS. A STATEMENT of a more statements is b) PROGRAM-ID.andconsists COBOL verb and an terminated by a full stop. or operands. e.g. a) MOVEoperand .21 TO VAT-RATE RESERVEDb) WORDS e.g. COMPUTE VAT-AMOUNT =
SUBTRACT T-TAX. FROM GROSSPRODUCT-COST * VAT-RATE CHARACTERS PAY GIVING NET-PAY
USER DEFINED WORDS
COBOL CHARACTER SET
Overview
Character
Meaning
Space +
Plus sign
-
Minus sign or hyphen
*
Asterisk
/
Forward slash or solidus
=
Equal sign
$
Currency sign1
,
Comma
;
Semicolon
.
Decimal point or period
"
Quotation mark2
(
Left parenthesis
)
Right parenthesis
>
Greater than
<
Less than
:
Colon
'
Apostrophe
A-Z
Alphabet (uppercase)
a-z
Alphabet (lowercase)
0-9
Numeric characters
IDENTIFICATION DIVISION …
IDENTIFICATION DIVISION. PROGRAM-ID. PROG1. AUTHOR. R.R. BHATT. INSTALLATION. ABC CORP. DATE-WRITTEN. 01-JAN-2005. DATE-COMPILED. 01-JAN-2005. SECURITY. HIGH.
Compiler takes this as Program Identifier. PROGRAM-ID comes immediately after ID Division.
OPTIONAL
ENVIRONMENT DIVISION ENVIRONMENT DIVISION
CONFIGURATION SECTION
INPUT-OUTPUTT SECTION
Identifies the computer used for compiling of programs
Identifies the resources used for executing the program
DATA DIVISION
The DATA DIVISION is used to describe the data structures used in the program.
There are sections in the DATA DIVISION
FILE SECTION WORKING-STORAGE SECTION LINKAGE SECTION REPORT SECTION
The two most commonly used components (sections) are a) WORKING-STORAGE SECTION Internal data structures are defined here. b) FILE SECTION File I/O buffer areas are defined here.
DATA DIVISION DATA DIVISION. FILE SECTION. FD INVENTORY-FILE RECORD CONTAINS 78 CHARACTERS. 01 INVENTORY-REC. 05 IF-PART-NUMBER 05 05 IF-WHSE-LOCS. 10 IF-MAIN-LOC 10 IF-ALT-LOC 05 FD PRINT-FILE. 01 PRINT-REC. 05 05 P-PART-NUMBER 05 05 P-MAIN-LOC 05 05 P-ALT-LOC WORKING-STORAGE SECTION. 01 FLAGS. 05 F-MORE-RECORDS
PIC X(09). PIC X(24). PIC X(06). PIC X(06). PIC X(33). PIC PIC PIC PIC PIC PIC
X(10). X(09). X(05). X(06). X(05). X(06).
PIC X VALUE 'Y'.
PROCEDURE DIVISION ..
The PROCEDURE DIVISION consists of the following –
Sections
Paragraphs
Sentences
Statements
PROCEDURE DIVISION Section
Section contain one or more Paragraphs.
PROCEDURE DIVISION. 0001-ACCOUNT-SECTION.
Paragraph A PARAGRAPH comprises of one or more sentences
001-ACCOUNT-READ-PARA. READ ACC-FILE AT END MOVE ‘Y’ TO EOF. MOVE TAX-REDUCT TO TAX-AMOUNT 001-ACCOUNT-VALIDATE-PARA. ADD AMOUNT TO TOT-AMOUNT. ACCEPT EMPLOYEE-SALARY DISPLAY “Current Employee Salary “ EMPLOYEE-SALARY. 001-EXIT-PARA. STOP RUN.
Sentences
statement
A SENTENCE is a combination of one or more statements and is terminated by a full stop.
A STATEMENT is a combination of a COBOL verb and one or more operands.
First COBOL program
IDENTIFICATION DIVISION. PROGRAM-ID. FIRSTPG. PROCEDURE DIVISION. A0000-MAIN-PARA. DISPLAY ‘-------------------------------’. DISPLAY ‘ WELCOME TO COBOL’. DISPLAY ‘--------------------------------’. STOP RUN.
COBOL coding sheet Column numbers 1 2
3
4
5
6
7
Column numbers *
8 9 10
Area A
11
12
72
Area B
80
I D E N T
-
I F
/
I C A T I O N
A R E A
COBOL coding sheet Almost all COBOL compilers treat a line of COBOL code as if it contained two distinct areas. These are AREA A
AREA B
*) Between Column 8 to 11 *) Division, Section, Paragraph names, FD entries & 01 level entries must start in Area A
*) Between Column 12 to 72 *) All Sentences & Statements start in Area B
COBOL coding rules Each line is considered to be made up of 80 columns. Columns 1 to 6 are reserved for line numbers. Column 7 is an indicator column and has special meaning to the compiler. Asterisk ( * ) indicates comments Hyphen ( - ) indicates continuation Slash ( / ) indicates form feed
Columns 8 to 11 are called Area A.
All COBOL DIVISIONs, SECTIONs, paragraphs and some special entries must begin in Area A.
Columns 12 to 72 are called Area B. statements must begin in Area B.
All COBOL
Columns 73 to 80 are identification area.
Basic data types
Alphabetic ( A) Numeric( 9) Alphanumeric (X) Edited numeric ( Z, $) Edited alphanumeric(/,-)
Data names
Are named memory locations.
Must be described in the DATA DIVISION before they can be used in the PROCEDURE DIVISION.
Can be of elementary or group type.
Can be subscripted for Arrays.
Are user defined words .
Rules for forming User-defined words
Can be at most 30 characters in length.
Only alphabets, digits and hyphen are allowed.
Blanks are not allowed.
May not begin or end with a hyphen.
Should not be a COBOL reserved word like ADD,SUBTRACT,MOVE,DISPLAY etc….
Description of data names
All the data names used in the PROCEDURE DIVISION must be described in the DATA DIVISION.
The description of a data name is done with the aid of the following – (1) Level number (2) PICTURE clause (3) VALUE clause
DATA DIVISION. 01 WS-EMPL-NO PIC X(10) VALUE 1001. LEVEL NO
Data Name
Picture Clause
VALUE Clause
DATA NAME LEVEL NO Level number
Is used to specify the the data hierarchy.
Level Number 01 02 to 49
Purpose
Record description and independent items Fields within records and sub items
66
RENAMES clause
77
Independent items
88
Condition names
Piture Clause Code PICTURE clause
Meaning
9
Numeric
A
Alphabetic
X
Alphanumeric
V
Implicit Decimal
S
Sign bit
COBOL ‘PICTURE’ Clauses
Some examples
PICTURE 999
a three digit (+ive only) integer
PICTURE S999
a three digit (+ive/-ive) integer
PICTURE XXXX
a four character text item or
string
PICTURE 99V99
a +ive ‘real’ in the range 0 to
99.99
PICTURE S9V9
a +ive/-ive ‘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. 9’s).
Abbreviating recurring symbols
Recurring symbols can be specified using a ‘repeat’ factor inside round brackets
PIC 9(6) is equivalent to PICTURE 999999 PIC 9(6)V99 is equivalent to PIC 999999V99 PICTURE X(10) is equivalent to PIC XXXXXXXXXX PIC S9(4)V9(4) is equivalent to PIC S9999V9999 PIC 9(18) is equivalent to PIC 999999999999999999
Declaring DATA in COBOL
In COBOL a variable declaration consists of a line containing the following items; Œ A level number. A data-name or identifier. Ž A PICTURE clause.
We can give a starting value to variables by means of an extension to the picture clause called the value clause.
DATA DIVISION. WORKING-STORAGE SECTION. 01 Num1 PIC 999 VALUE ZEROS. 01 VatRate PIC V99 VALUE .18. 01 StudentName PIC X(10) VALUE SPACES. DATA Num1
000
VatRate
.18
StudentName
Description of data names .. VALUE clause
Is used to assign an initial value to a elementary data item.
The initial value can be numeric literal, nonnumeric literal or figurative constant.
Is an optional clause.
Literals
Literals are symbols whose value does not change in a program. There are 3 types of literals namely (1) Numeric literals. (2) Non-numeric literals. (3) Figurative constants.
Literals – Figurative Constants Figurative constants
Meaning
ZERO(S) or ZEROES
Represents the value 0, one or more depending on the context
SPACE(S)
Represents one or more spaces
HIGH-VALUE(S)
Represents the highest value
LOW-VALUE(S)
Represents the lowest value
QUOTE(S)
Represents single or double quotes
ALL literal
Fill With Literal
Figurative Constants - Examples 01
GrossPay PIC 9(5)V99 VALUE 13.5. ZERO MOVE ZEROS TO GrossPay. ZEROES GrossPay 0 0 0 1 3 5 0
01
StudentName
PIC X(10) VALUE "MIKE".
MOVE ALL "-" TO StudentName. StudentName
M I K E
Figurative Constants - Examples 01
GrossPay PIC 9(5)V99 VALUE 13.5. ZERO MOVE ZEROS TO GrossPay. ZEROES GrossPay 0
0
0
0
0
0
0
01
StudentName
PIC X(10) VALUE "MIKE".
MOVE ALL "-" TO StudentName. StudentName
- - - - - - - - - -
Group and elementary items
WORKING-STORAGE SECTION. 01 EMPLOYEE-DETAILS PIC X(30). 01 EMPLOYEE-DETAILS. 05 EMP-NUM PIC 9(4). 05 EMP-NAME PIC X(10). 05 EMP-DEPT PIC X(4). 05 EMP-LOC PIC X(12).
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.
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.
Group Items/Records - Example WORKING-STORAGE SECTION. 01 EMPLOYEE-DETAILS PIC X(20). 01 EMPLOYEE-DETAILS. 05 EMP-NUM PIC 9(4). 05 EMP-NAME PIC X(10). 05 EMP-DEPT PIC X(4). 05 EMP-LOC PIC X(12).
Group item
Sub-Items
Group Items/Records - Example Data in input file
123456789012345678901234567890 1234JyothiS E&R Bangalore 2234Archana E&R Marathi 9999Bhushan E&R C++
Variable for file read WORKING-STORAGE SECTION. 01 EMPLOYEE-DETAILS PIC X(30).
(cols)
Value 1234JyothiS
E&R Bangalore
Group Items/Records - Example Data in input file 123456789012345678901234567890 (cols) 1234JyothiS E&R Bangalore 2234Archana E&R Mysore 9999Bhushan E&R Chennai
Variable for file read
Value
WORKING-STORAGE SECTION. 01 EMPLOYEE-DETAILS PIC X(30).
1234JyothiS
01 EMPLOYEE-DETAILS. 05 EMP-NUM PIC 9(4). 05 EMP-NAME PIC X(10). 05 EMP-DEPT PIC X(4). 05 EMP-LOC PIC X(12).
1234 JyothiS E&R Bangalore
E&R Bangalore
LEVEL Numbers & DATA hierarchy
WORKING-STORAGE SECTION. 01 POLICY-DETAILS. 05 POLICY-NO. 10 POLICY-TYP PIC X(4). 10 POLICY-LOC PIC X(2). 10 POLICY-ID PIC X(5). 05 POLICY-TYPE PIC X(10). 05 POLICY-EXPDT PIC X(10).
In COBOL, Level numbers are used to express data hierarchy. The higher the level number, the lower the item is in the hierarchy.
So Group items contain sets of elementary items with lower level numbers. At the lowest level the data is completely atomic.
Description of data names
DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-REGNO PIC X(5). 01 WS-NAME. 05 WS-FIRST-NAME PIC A(15). 05 WS-MID-NAME PIC A(15). 05 WS-LAST-NAME PIC A(10). 01 WS-AGE PIC 99V99. 01 WS-SCHOLARSHIP PIC 9(4) VALUE 1000.
Group Items/Records WORKING-STORAGE SECTION. 01 StudentDetails PIC X(26).
StudentDetails 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
Group Items/Records WORKING-STORAGE SECTION. 01 StudentDetails. 02 StudentName 02 StudentId 02 CourseCode 02 Grant 02 Gender
PIC PIC PIC PIC PIC
X(10). 9(7). X(4). 9(4). X.
StudentDetails H EN N E S S Y RM 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F StudentName
StudentId
CourseCode Grant
Gender
Group Items/Records WORKING-STORAGE SECTION. 01 StudentDetails. 02 StudentName. 03 Surname 03 Initials 02 StudentId 02 CourseCode 02 Grant 02 Gender
PIC PIC PIC PIC PIC PIC
X(8). XX. 9(7). X(4). 9(4). X.
StudentDetails H EN N E S S Y RM 9 2 3 0 1 6 5 L M 5 1 0 5 5 0 F StudentName Surname
StudentId Initials
CourseCode Grant
Gender
MOVE VERB
Overview
The MOVE Verb Identifier TO { Identifier } ... MOVE Literal
MOVE copies data from the source identifier or literal to one or more destination identifiers.
MOVE copies data to Group or elementary data items.
MOVE always performs LEFT JUSTIFICATION to Character
MOVE always perform RIGHT JUSTIFICATION to Numeric data.
When data is MOVEd into an item the contents of the item are completely replaced.
MOVEing Data
MOVE “RYAN” TO Surname. MOVE “FITZPATRICK” TO Surname.
01 Surname PIC X(8). C O U G H L A N
MOVEing Data
MOVE “RYAN” TO Surname. MOVE “FITZPATRICK” TO Surname.
01 Surname R Y
A N
PIC X(8).
MOVEing Data
MOVE “RYAN” TO Surname. MOVE “FITZPATRICK” TO Surname.
01 Surname F I T
Z P
PIC X(8). A T R I C K
MOVEing to a numeric item.
When the destination item is numeric, or edited numeric, then data is aligned along the decimal point with zero filling or truncation as necessary.
When the decimal point is not explicitly specified in either the source or destination items, the item is treated as if it had an assumed decimal point immediately after its rightmost character.
01 GrossPay
PIC 9(4)V99. GrossPay
MOVE ZEROS TO GrossPay.
0 0 0 0 0 0
MOVE 12.4 TO GrossPay.
GrossPay
0 0 1 2 4 0
GrossPay
MOVE 123.456 TO GrossPay. 0 1 GrossPay
2 3 4 5 6
MOVE 12345.757 TO GrossPay. 1 2 3 4 5
7 5 7
01 CountyPop 01 Price
PIC 999. PIC 999V99. CountyPop
MOVE 1234 TO CountyPop.
1 2 3 4
MOVE 12.4 TO CountyPop.
CountyPop
0 1 Price
2 4
MOVE 154 TO Price. MOVE 3552.75 TO Price.
1 5 40 0 Price
3 5 5 2
7 5
Before
After
WS00-OUT1
0000
WS00-OUT1
3456
WS00-OUT2
000000
WS00-OUT2
345678
Before
WS00-OUT3
000000
After
WS00-OUT3
Before
WS00-OUT4
After
00000000
WS00-OUT4
12345678
123456
MOVE .. example
****************************
Output SPOOL
WS00-OUT1 : HARAYANA WS00-OUT2 : HARAYANA ****************************
The DISPLAY Verb Identifier Identifier ... DISPLAY Literal Literal
[ UPON Mnemonic - Name] [ WITH NO ADVANCING]
From time to time it may be useful to display messages and data values on the screen.
A simple DISPLAY statement can be used to achieve this.
A single DISPLAY can be used to display several data items or literals or any combination of these.
The WITH NO ADVANCING clause suppresses the carriage return/line feed.
The ACCEPT verb Format 1. ACCEPT Identifier [ FROM Mnemonic - name] DATE DAY Format 2. ACCEPT Identifier FROM DAY - OF - WEEK TIME
01 CurrentDate
PIC 9(6).
01 DayOfYear
PIC 9(5).
01 Day0fWeek
PIC 9.
01 CurrentTime
PIC 9(8).
* YYMMDD * YYDDD
* D (1=Monday) * HHMMSSss
s = S/100
IDENTIFICATION DIVISION. PROGRAM-ID. AcceptAndDisplay. AUTHOR. Michael Coughlan.
Run of Accept and Display program Enter student details using template below NNNNNNNNNNSSSSSSSCCCCGGGGS COUGHLANMS9476532LM511245M Name is MS COUGHLAN Date is 24 01 94 Today is day 024 of the year The time is 22:23
DATA DIVISION. WORKING-STORAGE SECTION. 01 StudentDetails. 02 StudentName. 03 Surname PIC 03 Initials PIC 02 StudentId PIC 02 CourseCode PIC 02 Grant PIC 02 Gender PIC
X(8). XX. 9(7). X(4). 9(4). X.
01 CurrentDate. 02 CurrentYear 02 CurrentMonth 02 CurrentDay
PIC 99. PIC 99. PIC 99.
01 DayOfYear. 02 FILLER 02 YearDay
PIC 99. PIC 9(3).
01 CurrentTime. 02 CurrentHour 02 CurrentMinute 02 FILLER
PIC 99. PIC 99. PIC 9(4).
PROCEDURE DIVISION. Begin. DISPLAY "Enter student details using template below". DISPLAY "NNNNNNNNNNSSSSSSSCCCCGGGGS ". ACCEPT StudentDetails. ACCEPT CurrentDate FROM DATE. ACCEPT DayOfYear FROM DAY. ACCEPT CurrentTime FROM TIME. DISPLAY "Name is ", Initials SPACE Surname. DISPLAY "Date is " CurrentDay SPACE CurrentMonth SPACE CurrentYear. DISPLAY "Today is day " YearDay " of the year". DISPLAY "The time is " CurrentHour ":" CurrentMinute. STOP RUN.
Example Program Date
Overview