Professional Documents
Culture Documents
ORIENTED LANGUAGE)
Overview
COBOL Fundamentals
DAY1
Session Plan
Day 1:
Introduction to COBOL
Program Structure
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 …
Compiler takes this as
Program Identifier.
PROGRAM-ID comes
IDENTIFICATION DIVISION. immediately after ID
IDENTIFICATION DIVISION. Division.
PROGRAM-ID. PROG1.
PROGRAM-ID. PROG1.
AUTHOR. R.R. BHATT.
AUTHOR. R.R. BHATT.
INSTALLATION. ABC CORP.
INSTALLATION. ABC CORP.
DATE-WRITTEN. 01-JAN-2005. OPTIONAL
DATE-WRITTEN. 01-JAN-2005.
DATE-COMPILED. 01-JAN-2005.
DATE-COMPILED. 01-JAN-2005.
SECURITY.
SECURITY. HIGH.
HIGH.
ENVIRONMENT DIVISION
ENVIRONMENT DIVISION
The DATA DIVISION is used to describe the data structures used in the program.
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 PIC X(09).
05 PIC X(24).
05 IF-WHSE-LOCS.
10 IF-MAIN-LOC PIC X(06).
10 IF-ALT-LOC PIC X(06).
05 PIC X(33).
FD PRINT-FILE.
01 PRINT-REC.
05 PIC X(10).
05 P-PART-NUMBER PIC X(09).
05 PIC X(05).
05 P-MAIN-LOC PIC X(06).
05 PIC X(05).
05 P-ALT-LOC PIC X(06).
WORKING-STORAGE SECTION.
01 FLAGS.
05 F-MORE-RECORDS PIC X VALUE 'Y'.
PROCEDURE DIVISION ..
Sections
Paragraphs
Sentences
Statements
PROCEDURE DIVISION
Section contain one or
Section more Paragraphs.
PROCEDURE DIVISION.
0001-ACCOUNT-SECTION. Paragraph
A PARAGRAPH comprises
001-ACCOUNT-READ-PARA.
of one or more sentences
READ ACC-FILE AT END
MOVE ‘Y’ TO EOF.
MOVE TAX-REDUCT TO TAX-AMOUNT
A SENTENCE is a
001-ACCOUNT-VALIDATE-PARA.
ADD AMOUNT TO TOT-AMOUNT.
Sentences combination of one or more
statements and is
terminated by a full stop.
ACCEPT EMPLOYEE-SALARY
DISPLAY “Current Employee Salary “
EMPLOYEE-SALARY.
001-EXIT-PARA. statement
STOP RUN. A STATEMENT is a
combination of a COBOL
verb and one or more
operands.
First COBOL program
IDENTIFICATION DIVISION.
IDENTIFICATION DIVISION.
PROGRAM-ID. FIRSTPG.
PROGRAM-ID. FIRSTPG.
PROCEDURE DIVISION.
PROCEDURE DIVISION.
A0000-MAIN-PARA.
A0000-MAIN-PARA.
DISPLAY ‘-------------------------------’.
DISPLAY ‘-------------------------------’.
DISPLAY ‘ WELCOME TO COBOL’.
DISPLAY ‘ WELCOME TO COBOL’.
DISPLAY ‘--------------------------------’.
DISPLAY ‘--------------------------------’.
STOP RUN.
STOP RUN.
COBOL coding sheet
Column numbers
1 2 3 4 5 6 7 8 9 10 11 12 72 80
I
Column numbers * Area A Area B D
E
N
T
- I
F
I
/ C
A
T
I
O
N
A
R
E
A
COBOL coding sheet
AREA A AREA B
Alphabetic ( A)
Numeric( 9)
Alphanumeric (X)
Edited numeric ( Z, $)
Edited alphanumeric(/,-)
Data names
DATA DIVISION.
01 WS-EMPL-NO PIC X(10) VALUE 1001.
Level number
Code Meaning
9 Numeric
PICTURE
clause 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 ?
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 VatRate StudentName
Num1 VatRate StudentName
000 .18
000 .18
Description of data names ..
VALUE clause
Is an optional clause.
Literals
GrossPay
0 0 0 1 3 5 0
StudentName
M I K E
Figurative Constants - Examples
01 GrossPay PIC 9(5)V99 VALUE 13.5.
01 GrossPay PIC 9(5)V99 VALUE 13.5.
ZERO
MOVE ZEROS TO GrossPay.
MOVEZEROES TO GrossPay.
GrossPay
0 0 0 0 0 0 0
StudentName
- - - - - - - - - -
Group and elementary items
In COBOL the term “group item” is used to
describe a data item which has been
further subdivided.
WORKING-STORAGE SECTION.
01WORKING-STORAGE SECTION.
EMPLOYEE-DETAILS PIC X(30). A Group item is declared using a
01 EMPLOYEE-DETAILS PIC X(30). level number and a data name. It
01 EMPLOYEE-DETAILS. cannot have a picture clause.
0105EMPLOYEE-DETAILS.
EMP-NUM PIC 9(4).
0505EMP-NAME
EMP-NUM PIC
PICX(10).
9(4).
05 EMP-NAME PIC X(10).
05 EMP-DEPT PIC X(4). Where a group item is the highest
05 EMP-DEPT
05 EMP-LOC PIC X(4).
PIC X(12). item in a data hierarchy it is
05 EMP-LOC PIC X(12). 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.
01WORKING-STORAGE SECTION.
EMPLOYEE-DETAILS PIC X(20).
01 EMPLOYEE-DETAILS PIC X(20).
01 EMPLOYEE-DETAILS. Group item
0105EMPLOYEE-DETAILS.
EMP-NUM PIC 9(4).
0505EMP-NAME
EMP-NUM PIC
PICX(10).
9(4).
05 EMP-NAME PIC X(10).
05 EMP-DEPT PIC X(4).
05 EMP-DEPT
05 EMP-LOC PIC X(4).
PIC X(12). Sub-Items
05 EMP-LOC PIC X(12).
Group Items/Records - Example
123456789012345678901234567890 (cols)
1234JyothiS E&R Bangalore
Data in input file
2234Archana E&R Marathi
9999Bhushan E&R C++
WORKING-STORAGE SECTION.
01WORKING-STORAGE SECTION.
EMPLOYEE-DETAILS PIC X(30).
01 EMPLOYEE-DETAILS PIC X(30). 1234JyothiS
1234JyothiS
E&R Bangalore
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
WORKING-STORAGE SECTION.
01WORKING-STORAGE SECTION.
EMPLOYEE-DETAILS PIC X(30). 1234JyothiS E&R Bangalore
01 EMPLOYEE-DETAILS PIC X(30). 1234JyothiS E&R Bangalore
01 EMPLOYEE-DETAILS.
0105EMPLOYEE-DETAILS.
EMP-NUM PIC 9(4). 1234
0505EMP-NAME
EMP-NUM PIC
PICX(10).
9(4). 1234
JyothiS
05 EMP-NAME PIC X(10).
05 EMP-DEPT PIC X(4). JyothiS
05 EMP-DEPT PIC X(4). E&R
05 EMP-LOC PIC X(12). E&R
Bangalore
05 EMP-LOC PIC X(12). Bangalore
LEVEL Numbers & DATA hierarchy
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.
WORKING-STORAGE
01 StudentDetailsSECTION. PIC X(26).
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.
WORKING-STORAGE
01 SECTION.
StudentDetails.
01 02
StudentDetails.
StudentName PIC X(10).
02 StudentId
02 StudentName PIC9(7).
PIC X(10).
02 StudentId
02 CourseCode PIC 9(7).
PIC X(4).
02 Grant
02 CourseCode PIC9(4).
PIC X(4).
02 Grant
02 Gender PIC 9(4).
PIC X.
02 Gender PIC 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.
WORKING-STORAGE
01 SECTION.
StudentDetails.
01 02
StudentDetails.
StudentName.
02 03
StudentName.
Surname PIC X(8).
03 Surname
03 Initials PICXX.
PIC X(8).
03 Initials
02 StudentId PIC9(7).
PIC XX.
02 StudentId
02 CourseCode PIC 9(7).
PIC X(4).
02 Grant
02 CourseCode PIC9(4).
PIC X(4).
02 Grant
02 Gender PIC 9(4).
PIC X.
02 Gender PIC 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
Surname Initials
MOVE VERB
Overview
The MOVE Verb
Identifier
MOVE TO Identifier ...
Literal
MOVE copies data from the source identifier or literal to one
or more destination identifiers.
GrossPay
MOVE ZEROS TO GrossPay.
0 0 0 0 0 0
GrossPay
MOVE 12.4 TO GrossPay.
0 0 1 24 0
GrossPay
MOVE 123.456 TO GrossPay.
0 1 2 3 4 5 6
GrossPay
MOVE 12345.757 TO GrossPay.
1 2 3 4 5 7 5 7
01 CountyPop PIC 999.
01 Price PIC 999V99.
CountyPop
MOVE 1234 TO CountyPop. 1 2 3 4
CountyPop
MOVE 12.4 TO CountyPop.
0 1 2 4
Price
MOVE 154 TO Price.
1 5 4 0 0
Price
MOVE 3552.75 TO Price.
3 5 5 2 7 5
Before After
Before
WS00-OUT3 000000
After
WS00-OUT3 123456
Before After
****************************
Overview