Professional Documents
Culture Documents
BUSINESS 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
Division.
PROGRAM-ID. PROG1.
AUTHOR. R.R. BHATT.
INSTALLATION. ABC CORP.
DATE-WRITTEN. 01-JAN-2005.
DATE-COMPILED. 01-JAN-2005. OPTIONAL
SECURITY. 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
001-ACCOUNT-READ-PARA.
comprises of one or
READ ACC-FILE AT END
MOVE ‘Y’ TO EOF. more sentences
MOVE TAX-REDUCT TO TAX-AMOUNT
A SENTENCE is a
001-ACCOUNT-VALIDATE-PARA. combination of one or
ADD AMOUNT TO TOT-AMOUNT. Sentences 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.
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 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
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.
ZERO
MOVE ZEROS TO GrossPay.
ZEROES
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.
01 EMPLOYEE-DETAILS PIC X(30). A Group item is declared using
a level number and a data
name. It cannot have a picture
01 EMPLOYEE-DETAILS. clause.
05 EMP-NUM PIC 9(4).
05 EMP-NAME PIC X(10).
05 EMP-DEPT PIC X(4). Where a group item is the
05 EMP-LOC PIC X(12). 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).
123456789012345678901234567890 (cols)
1234JyothiS E&R Bangalore
Data in input file
2234Archana E&R Marathi
9999Bhushan E&R C++
WORKING-STORAGE SECTION.
01 EMPLOYEE-DETAILS PIC X(30). 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
WORKING-STORAGE SECTION.
01 EMPLOYEE-DETAILS PIC X(30). 1234JyothiS E&R Bangalore
01 EMPLOYEE-DETAILS.
05 EMP-NUM PIC 9(4). 1234
05 EMP-NAME PIC X(10). JyothiS
05 EMP-DEPT PIC X(4). E&R
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.
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 PIC X(10).
02 StudentId PIC 9(7).
02 CourseCode PIC X(4).
02 Grant PIC 9(4).
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.
01 StudentDetails.
02 StudentName.
03 Surname PIC X(8).
03 Initials PIC XX.
02 StudentId PIC 9(7).
02 CourseCode PIC X(4).
02 Grant PIC 9(4).
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 2 4 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 40 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
****************************
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