Cobol
Cobol
Dec 7, 2021 1
Introduction
Dec 7, 2021 2
The history of COBOL in a bird's-
eye view:
• 1959 The American Department of Defense (DOD) asked a
group of specialists to develop a business language that met
their demands.
• 1960 First proposal for COBOL - COmmon Business Oriented
Language -, named COBOL-60.
• 1961 First COBOL compilers are getting used.
• 1965 From this year the usage of COBOL starts to increase a
lot.
• 1968 The American National Standards Institute (ANSI) sets
the first official COBOL standard: COBOL-68.
• 1970 The International Organization for Standardization (ISO)
makes ANSI's COBOL-68 an international standard.
• 1974 The new COBOL-74 standard is set.
• 1985 The new COBOL-85 standard is set.
• 1989 Intrinsic functions are added to the standard.
• 2002 The long awaited object oriented COBOL 2002 standard
is set.
Dec 7, 2021 3
COBOL
• US defense department took the initiative for
developing a high level computer language in 1958.
Program
Divisions
Section(s)
Paragraph(s)
Sentence(s)
Statement(s)
Dec 7, 2021 6
Sections
– FILE SECTION.
– SelectRecords SECTION.
Dec 7, 2021 7
Paragraphs
• Each section consists of one or more paragraphs.
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
PROGRAM-ID. NameOfProgram.
[AUTHOR. YourName.]
[DATE-WRITTEN. ……….]
[DATE-COMPILED. ……..]
Dec 7, 2021 14
The IDENTIFICATION DIVISION Syntax
• Only first two lines are mandatory
IDENTIFICATION DIVISION.
PROGRAM-ID. NameOfProgram.
[AUTHOR. YourName.]
I-O-CONTROL.
[ RERUN ON file-name [ EVERY ] [ integer
RECORDS] or [END OF REEL] ]
[ SAME AREA [ FOR ] file-name1 [file-name2…..] ]
[ SAME RECORD AREA [ FOR ] file-name1 [file-
name2…..] ]
WORKING-STORAGE SECTION.
entries to describe data/variables used by
the program. These are not part of any file(s).
LINKAGE SECTION.
describes data made available from another
program. Inter program communication.
Dec 7, 2021 21
DATA DIVISION
DATA DIVISION.
FILE SECTION.
FD file-name 1
[ BLOCK [CONTAINS] int-1 [TO int-2] [CHARACTERS or RECORDS] ] 2
[ RECORD [ CONTAINS ] int-3 [ TO int-4 ] CHARACTERS ] 3
[ LABEL [RECORDS ARE] [RECORD IS] [STANDARD] [OMITTED] ] 4
[ VALUE OF ID [ IS ] data-name ] 5
[ RECORDING [ MODE ] [ IS ] [ F or V ]. 6
Dec 7, 2021 23
DATA DIVISION
WORKING-STORAGE SECTION.
entries to describe data/variables used by
the program. These are not part of any file(s).
Level-number data-name [ PIC clause] [ REDEFINES-clause]
[ BLANK WHEN ZERO clause] [ JUSTIFIED clause] [ OCCURS
clause] [ sign clause] [ Synchronized clause] [ USAGE clause]
[ VALUE clause]
level-number : 01 and 77 must begin in Area A.
02 – 49 can begin in area A or B.
88 can begin in area A or B, but must be associated
with (preceding) an elementary or group item and
must be followed by VALUE clause.
Dec 7, 2021 24
DATA DIVISION
05 Month PIC 99.
88 ValidMM VALUE 01 THRU 12.
05 DD PIC 99.
88 ValidDD VALUE 28, 29, 30 31.
VALUE can be a single value or a range of
values or a combination of both.
Each 88-level entry implicitly has the picture
characteristics of the conditional variable.
Associated with one variable, you can have as
many 88 level entries as you require.
blank-when-zero-clause=
• "BLANK" [ "WHEN" ] ( "ZERO" | "ZEROS" | "ZEROES" )
justified-clause=
• ( "JUSTIFIED" | "JUST" ) [ "RIGHT" ]
occurs-clause=
• "OCCURS" integer [ "TIMES" ]
[ "INDEXED" [ "BY" ] { index-name }+ ]
occurs-clause=
• "OCCURS" [ ( integer | zero ) "TO" ] integer [ "TIMES" ]
"DEPENDING“ [ "ON" ] qualified-data-name
[ "INDEXED" [ "BY" ] { index-name }+ ]
Dec 7, 2021 27
DATA DIVISION
picture-clause=
• ( "PICTURE" | "PIC" ) [ "IS" ] picture-string
sign-clause=
• [ "SIGN" ["IS"]] ("LEADING"|"TRAILING") ["SEPARATE" ["CHARACTER"]]
synchronized-clause=
• ( "SYNCHRONIZED" | "SYNC" ) [ ( "LEFT" | "RIGHT" ) ]
usage-clause=
• ["USAGE" [ "IS"]] ("BINARY"| "COMP"| "COMP-1”|”COMP-2”|COMP-3”
|"COMPUTATIONAL" | "COMPUTATIONAL-1/-2/-3”
|"DISPLAY" | "INDEX" | "PACKED-DECIMAL" )
condition-value-clause=
• ("VALUE" ["IS"] | "VALUES" ["ARE"] ) { literal [ ("THROUGH" | "THRU“)
literal ] }
data-value-clause= "VALUE" [ "IS" ] literal
Dec 7, 2021 28
DATA DIVISION
REDEFINES-clause
05 DAT1 …….
05 DAT2 REDEFINES DAT1.
DAT1 redefined item and DAT2 is redefining item.
Level nos. of DAT1 and DAT2 must be identical and
must not be 66 or 88. You can redefine as many times
as you require.
Explicit and Implicit redefinition.
Dec 7, 2021 29
DATA DIVISION
BLANK WHEN ZERO(es) can be specified only for
numeric-edited items with USAGE DISPLAY.
Dec 7, 2021 30
DATA DIVISION
SIGN [ IS] [ LEADING or TRAILING] [ SEPARATE]
can be specified only for signed numeric data (i.e., having
“S” in the PIC). Default is Trailing.
If SEPARATE is specified, it occupies 1 character.
PIC 99 – 2 characters
PIC S99 – 2 characters
PIC S99 SIGN SEPARATE – 3 characters
LINKAGE SECTION.
The data made available from another
program (Inter program communication)
are described under Linkage Section.
You cannot specify VALUE Clause for items
other than level-88 items in this section.
Dec 7, 2021 32
COBOL Program Text Structure
IDENTIFICATION DIVISION.
Program Details
ENVIRONMENT DIVISION. NNOTE
OTE
The
Thekeyword
Environment in which the keyword
DIVISION
DIVISIONand andaa
program is compiled & executed ‘full-stop’
‘full-stop’isis
used
usedininevery
every
DATA DIVISION. case.
case.
Data Descriptions
PROCEDURE DIVISION.
Program instructions
Dec 7, 2021 33
Example
IDENTIFICATION DIVISION.
PROGRAM-ID. MyProgram.
AUTHOR. Globsyn.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM-390.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Num1 PIC 9 VALUE ZEROS.
01 Num2 PIC 9 VALUE ZEROS.
PROCEDURE DIVISION.
PARA-1.
MOVE 5 TO NUM1.
STOP RUN.
Dec 7, 2021 34
COBOL coding 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 numbers must start in Area A. All other sentences
must start in Area B.
IDENTIFICATION
IDENTIFICATION DIVISION.
DIVISION.
PROGRAM-ID. ProgramFragment.
PROGRAM-ID. ProgramFragment.
** This
This is
is aa comment.
comment. It
It starts
starts
** with
with an asterisk in column 11
an asterisk in column
Dec 7, 2021 35
COBOL coding rules
Sequence Area A Area B Pgmr-id
1 2 3 4 5 6 7 8 9 10 11 12 13…………….72 73…80
Indicator area
Sequence area – Serial number of source lines
Indicator area – continuation line (hyphen)
– comment line ( * or / )
– debugging line ( D or d )
Area A – all division, section, paragraph names,
• Characters in VS Cobol II
A-Z, a-z, 0-9 and special characters like < .
( + space $ * ) ; - / , > : ‘ = “
Dec 7, 2021 38
Describing DATA.
ŒVariables.
Literals.
ŽFigurative Constants.
Dec 7, 2021 39
Data-Names / Variables
Dec 7, 2021 40
Name Construction
• All user defined names, such as data names, paragraph
names, section names and mnemonic names, must
adhere to the following rules;
– They must contain at least one character and not
more than 30 characters.
– They must contain at least one alphabetic character
and they must not begin or end with a hyphen.
– They must be contructed from the characters A to
Z, a-z, the number 0 to 9 and the hyphen.
e.g. TotalPay, Gross-Pay, PrintReportHeadings,
Customer10-Rec
• All data-names should describe the data they contain.
• All paragraph and section names should describe the
function of the paragraph or section.
Dec 7, 2021 41
COBOL Literals
Dec 7, 2021 42
Figurative Constants
• COBOL provides its own, special constants called
Figurative Constants. It does not depend on the
length of the associated operands
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 == Min
MinValue
Value
ALLliteral
ALL literal == Fill
FillWith
WithLiteral
Literal
Dec 7, 2021 43
Figurative Constants - Examples
Figurative Constants - Examples
01
01 GrossPay
GrossPay PIC
PIC 9(5)V99
9(5)V99 VALUE
VALUE 13.5
13.5..
ZERO
ZEROS
MOVEZEROES TO GrossPay.
MOVE TO GrossPay.
GrossPay
0 0 0 1 3 5 0
01
01 StudentName
StudentName PIC
PIC X(10)
X(10) VALUE
VALUE "MIKE".
"MIKE".
MOVE
MOVE ALL
ALL "-"
"-" TO StudentName.
TOStudentName
StudentName.
M I K E
Dec 7, 2021 44
Figurative Constants - Examples
Figurative Constants - Examples
01
01 GrossPay
GrossPay PIC
PIC 9(5)V99
9(5)V99 VALUE
VALUE 13.5.
13.5.
ZERO
ZEROS
ZEROES
MOVE
MOVE TO
TO GrossPay.
GrossPay.
GrossPay
0 0 0 0 0 .0
01
01 StudentName0 PIC
StudentName PIC X(10)
X(10) VALUE
VALUE "MIKE".
"MIKE".
MOVE
MOVE ALL
ALL "-"
"-" TO
TO StudentName.
StudentName.
StudentName
- - - - - - - - - -
Dec 7, 2021 45
COBOL Data Types
• Alphabetic
• Numeric
• Alpha-numeric
Dec 7, 2021 46
Files, Records, Fields
• We use the term FIELD to describe an item of
information we are recording about an object
(e.g. StudentName, DateOfBirth, CourseCode).
Dec 7, 2021 52
Declaring DATA in COBOL
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Num1 PIC 999 VALUE ZEROS.
DATA
01 VatRate PIC V99 VALUE 0.18.
Num1
Num1 VatRate
VatRate StudentName
StudentName 01 StudentName PIC X(10) VALUE SPACES.
000
000 .18
.18
Dec 7, 2021 53
PICTURES for Group Items
Dec 7, 2021 54
More on PIC
PIC Size Remarks
A 1 byte Only a letter of Alphabet or space
B 1 byte one Space character is inserted
Z 1 byte Zero suppression in leading position
0 1 byte one zero character is inserted
/ 1 byte one “/” character is inserted
, 1 byte one comma character is inserted
. 1 byte Align decimal period & inserted “.”
+ 1 byte Editing sign control symbol
DB 2 bytes -do-
- 1 byte -do-
CR 2 bytes -do-
* 1 byte Cheque protection symbol in leading
Dec 7, 2021
positions 55
Editing Symbols
,, B
B 00 // Simple
Simple Insertion
Insertion
.. Special
Special Insertion
Insertion
++ -- CR
CR DB
DB $$ Fixed
Fixed Insertion
Insertion
++ -- SS Floating
Floating Insertion
Insertion
ZZ ** Suppression
Suppression and
and
Replacement
Replacement
Dec 7, 2021 56
Simple Insertion
Sending
Sending Receiving
Receiving
Picture
Picture Data
Data Picture
Picture Result
Result
PIC
PIC999999
999999 123456
123456 PIC
PIC999,999
999,999 123,456
PIC
PIC9(6)
9(6) 000078
000078 PIC
PIC9(3),9(3)
9(3),9(3) 000,078
PIC
PIC9(6)
9(6) 000078
000078 PIC
PICZZZ,ZZZ
ZZZ,ZZZ 78
PIC ****178
PIC9(6)
9(6) 000178
000178 PIC ***,***
PIC ***,***
**2,178
PIC
PIC9(6)
9(6) 002178
002178 PIC ***,***
PIC ***,***
PIC 120183
PIC9(6)
9(6) 120183
120183 PIC
PIC99B99B99
99B99B99
PIC 12/01/83
PIC9(6)
9(6) 120183
120183 PIC
PIC99/99/99
99/99/99
PIC 120045
PIC9(6)
9(6) 001245
001245 PIC
PIC990099
990099
Dec 7, 2021 57
Special Insertion
Sending
Sending Receiving
Receiving
Picture
Picture Data
Data Picture
Picture Result
Result
PIC
PIC999V99
999V99 12345
12345 PIC
PIC999.99
999.99 123.45
023.4
PIC
PIC999V99
999V99 02345
02345 PIC
PIC999.9
999.9
12.34
PIC
PIC999V99
999V99 51234
51234 PIC
PIC99.99
99.99 456.00
PIC
PIC999
999 456
456 PIC
PIC999.99
999.99
Dec 7, 2021 58
Fixed Insertion - Plus and Minus
Sending
Sending Receiving
Receiving
Picture
Picture Data
Data Picture
Picture Result
Result
PIC
PICS999
S999 -123
-123 PIC
PIC-999
-999 -123
PIC 123-
PICS999
S999 -123
-123 PIC
PIC 999-
999-
123
PIC
PICS999
S999 +123
+123 PIC
PIC-999
-999
+12345
PIC -123
PICS9(5)
S9(5) +12345
+12345 PIC
PIC+9(5)
+9(5)
123-
PIC
PICS9(3)
S9(3) -123
-123 PIC
PIC+9(3)
+9(3)
PIC
PICS9(3)
S9(3) -123
-123 PIC
PIC999+
999+
Dec 7, 2021 59
Fixed Insertion - Credit, Debit, $
Sending
Sending Receiving
Receiving
Picture
Picture Data
Data Picture
Picture Result
Result
PIC
PICS9(4)
S9(4) +1234
+1234 PIC
PIC9(4)CR
9(4)CR 1234
PIC
PICS9(4)
S9(4) -1234
-1234 PIC
PIC9(4)CR
9(4)CR 1234CR
PIC
PICS9(4)
S9(4) +1234
+1234 PIC
PIC9(4)DB
9(4)DB 1223
PIC
PICS9(4)
S9(4) -1234
-1234 PIC
PIC9(4)DB
9(4)DB 1234DB
PIC
PIC9(4)
9(4) 1234
1234 PIC
PIC$99999
$99999 $01234
PIC $
PIC9(4)
9(4) 0000
0000 PIC
PIC$ZZZZZ
$ZZZZZ
Dec 7, 2021 60
Floating Insertion
Sending
Sending Receiving
Receiving
Picture
Picture Data
Data Picture
Picture Result
Result
PIC
PIC9(4)
9(4) 0000
0000 PIC
PIC$$,$$9.99
$$,$$9.99 $0.00
PIC
PIC9(4)
9(4) 0080
0080 PIC
PIC$$,$$9.00
$$,$$9.00 $80.00
PIC
PIC9(4)
9(4) 0128
0128 PIC
PIC$$,$$9.99
$$,$$9.99 $128.00
PIC
PIC9(5)
9(5) 57397
57397 PIC
PIC$$,$$9
$$,$$9 $7,397
PIC
PICS9(4)
S9(4) --0005
0005 PIC
PIC++++9
++++9 -5
PIC
PICS9(4)
S9(4) +0080
+0080 PIC
PIC++++9
++++9 +80
PIC
PICS9(4)
S9(4) --0080
0080 PIC
PIC-- ------ 99 -80
PIC
PICS9(5)
S9(5) +71234
+71234 PIC
PIC-- ------ 99 ž1234
Dec 7, 2021 61
Suppression and Replacement
Sending
Sending Receiving
Receiving
Picture
Picture Data
Data Picture
Picture Result
Result
PIC 12,345
PIC9(5)
9(5) 12345
12345 PIC
PICZZ,999
ZZ,999
1,234
PIC
PIC9(5)
9(5) 01234
01234 PIC ZZ,999
PIC ZZ,999 123
PIC
PIC9(5)
9(5) 00123
00123 PIC
PICZZ,999
ZZ,999 012
PIC
PIC9(5)
9(5) 00012
00012 PIC
PICZZ,999
ZZ,999 *5,678
PIC ***567
PIC9(5)
9(5) 05678
05678 PIC
PIC**,**9
**,**9
******
PIC
PIC9(5)
9(5) 00567
00567 PIC **,**9
PIC **,**9
PIC
PIC9(5)
9(5) 00000
00000 PIC
PIC**,***
**,***
Dec 7, 2021 62
The USAGE clause – why?
Dec 7, 2021 63
USAGE Syntax
Dec 7, 2021 65
Arithmetic when USAGE IS DISPLAY
8 4 2 1 8 4 2 1 HEX CHAR
Num1 PIC 9 VALUE 4. 1 1 1 1 0 1 0 0 F4 “4”
Dec 7, 2021 66
Arithmetic when USAGE IS COMPUTATIONAL
8 4 2 1 8 4 2 1 HEX CHAR
Num1 PIC 9 VALUE 4. 0 0 0 0 0 1 0 0 04 ?
Dec 7, 2021 67
Arithmetic when USAGE IS COMPUTATIONAL
8 4 2 1 8 4 2 1 HEX CHAR
Num1 PIC 9 VALUE 4. 0 0 0 0 0 1 0 0 04 ?
Num3 PIC 9. 0 0 0 0 0 1 0 1 05 ?
Dec 7, 2021 68
USAGE IS COMP
• COMP items are held in memory as pure binary
two's complement numbers.
Dec 7, 2021 69
COMP - Storage Requirements
Dec 7, 2021 71
Storage Requirement
Dec 7, 2021 72
The SYNCHRONIZED Clause
• The SYNCHRONIZED clause explicitly aligns COMP
and INDEX data items along their natural WORD
boundaries.
• If there is no SYNCHRONIZED clause the data items
are aligned on byte boundaries.
• This clause is used to optimize speed of processing -
but it does so at the expense of storage.
D O G Number
D O G Number
Word1 Word2
Dec 7, 2021 74
The SYNCHRONIZED Clause
F R O G S NumberNumber
Word1 Word2 Word3
Dec 7, 2021 75
The SYNCHRONIZED Clause
FiveBytes FourBytes
F R O G S S S S NumberNumber
Word1 Word2 Word3
S : Slack Bytes (not available to the program)
unless
Dec 7, 2021
they are explicitly defined. 76
The SYNCHRONIZED Clause
Dec 7, 2021 77
REDEFINES Clause
• REDEFINES clause allows you to use different data
entries to describe the same storage area.
• 05 A PIC X(4).
05 B REDEFINES A PIC 9(4).
Dec 7, 2021 79
The Redefines Clause
Rates
1 2 .3 4 5
01 Rates.
02 10Rate PIC 99V999.
02 100Rate REDEFINES 10Rate PIC 999V99.
02 1000Rate REDEFINES 10Rate PIC 9999V9.
Dec 7, 2021 80
The Redefines Clause
Rates
1 2 3 .4 5
01 Rates.
02 10Rate PIC 99V999.
02 100Rate REDEFINES 10Rate PIC 999V99.
02 1000Rate REDEFINES 10Rate PIC 9999V9.
Dec 7, 2021 81
The Redefines Clause
Rates
1 2 3 4 .5
01 Rates.
02 10Rate PIC 99V999.
02 100Rate REDEFINES 10Rate PIC 999V99.
02 1000Rate REDEFINES 10Rate PIC 9999V9.
Dec 7, 2021 82
The Redefines Clause
EuroDate
EuroDay EuroMonth
EuroYear
HoldDate 11 06 1983
USDay USMonth USYear
USDate
01 HoldDate.
02 EuroDate.
03 EuroDay PIC 99.
03 EuroMonth PIC 99.
03 EuroYear PIC 9(4).
02 USDate REDEFINES EuroDate.
03 USMonth PIC 99.
03 USDay PIC 99.
03 USYear PIC 9(4).
Dec 7, 2021 83
The Redefines Clause
Dec 7, 2021 84
One Dimension Table
01 JeansTable.
Dec 7, 2021 85
One Dimension Table
1 2 3 4
01 JeansTable.
02 Province PIC X(8) OCCURS 4. or
12346.99
1 309 2 3 4
01 JeansTable.
02 Province OCCURS 4 TIMES.
03 SalesValue PIC 9(4)V99.
03 NumSold PIC 9(2).
Province
SalesValue NumSold
Dec 7, 2021 87
Table
• 02 PROVINCE PIC X(8) OCCURS 4.
• Total occurrence is 4.
1 2 3 4
01 JeansTable.
02 Province OCCURS 4 TIMES.
Dec 7, 2021 90
Two Dimension Table
Province(1) Province(2) Province(3) Province(4)
1 2 3 4
1 2 1 2 1 2 1 2
G(1 1) 6 2 G(2,1) G(4,2)
SV(1,2) NS(1,2)
01 JeansTable.
02 Province OCCURS 4 TIMES.
03 Gender OCCURS 2 TIMES.
04 SalesValue PIC 9(4)V99.
04 NumSold PIC 9(2).
1 2 3 4
01 JeansTable.
02 Province OCCURS 4 TIMES.
Dec 7, 2021 92
Three Dimension Table
1 2 3 4
1 2 1 2 1 2 1 2
01 JeansTable.
02 Province OCCURS 4 TIMES.
03 Gender OCCURS 2 TIMES.
Dec 7, 2021 93
Three Dimension Table
1 2 3 4
1 2 1 2 1 2 1 2
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
01 JeansTable.
02 Province OCCURS 4 TIMES.
03 Gender OCCURS 2 TIMES.
04 Colour OCCURS 3 TIMES.
Dec 7, 2021 94
Three Dimension Table
1 2 3 4
1 2 1 2 1 2 1 2
1 12346.99 309
2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
01 JeansTable.
02 Province OCCURS 4 TIMES.
03 Gender OCCURS 2 TIMES.
04 Colour OCCURS 3 TIMES.
05 SalesValue PIC 9(4)V99.
05 NumSold PIC 9(2).
1 2 3 4
* 1 2 * 1 2 * 1 2 * 1 2
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
01 JeansTable.
02 Province OCCURS 4 TIMES.
03 ProviceTotal PIC 9(8).
03 Gender OCCURS 2 TIMES.
04 Colour OCCURS 3 TIMES.
05 SalesValue PIC 9(8)V99.
Dec 7, 2021 05 NumSold PIC 9(7). 96
Tables
What is the difference between:
01 Students.
02 Subject OCCURS 4 TIMES.
03 Marks OCCURS 3 TIMES.
05 Tot PIC 9(4)V99.
05 Score PIC 9(2).
and
01 Students.
02 Subject OCCURS 4 TIMES.
05 Tot PIC 9(4)V99 OCCURS 3 TIMES.
05 Score PIC 9(2) OCCURS 3 TIMES.
Imperative statements
Arithmetic: ADD SUBTRACT MULTIPLY
DIVIDE COMPUTE
Decision: IF EVALUATE
ADD A1 TO B1.
ADD A2 A3 A4 TO B2 B3.
ADD A4 A5 A6 GIVING B4.
A1 A2 A3 A4 A5 A6 B1 B2 B3 B4
Before 1 2 3 4 5 6 7 8 9 2
After 1 2 3 4 5 6 8 17 18 15
Dec 7, 2021 106
The ON SIZE ERROR option
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Num1 PIC 9 VALUE ZEROS.
01 Num2 PIC 9 VALUE ZEROS.
01 Result PIC 99 VALUE ZEROS.
PROCEDURE DIVISION.
Calc-Result.
DISPLAY “First Operand, please”.
ACCEPT Num1.
DISPLAY “Second Operand, please”.
ACCEPT Num2.
ADD Num1 Num2 GIVING Result.
DISPLAY "Result is = ", Result.
Result
Dec 7, 2021 RUN.
STOP 108
PROCEDURE DIVISION
ADD [CORR, or CORRESPONDING] iden-1 TO iden-2.
"ADD" ( "CORRESPONDING" | "CORR" ) A TO B
[ "END-ADD" ]
05 A. 02 B.
10 F1 PIC 99. 05 XYZ PIC X(8).
10 F2 PIC 99. 05 F2 PIC 9.
10 F3 PIC 99. 05 F1 PIC 9(5)
A1 A2 A3 A4 A5 B1 B2 B3 B4
Before 1 3 3 4 15 7 8 9 2
After 1 3 3 4 15 3 4 8 8
05 A. 02 B.
10 F1 PIC 99. 05 XYZ PIC X(8).
10 F2 PIC 99. 05 F2 PIC 9.
10 F3 PIC 99. 05 F1 PIC 9(5)
MULTIPLY A1 BY A2 A3.
MULTIPLY A4 BY A5 GIVING A6.
A1 A2 A3 A4 A5 A6
Before 2 3 4 4 5 6
After 2 6 8 4 5 20
A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11
Before 12 2 4 14 7 6 5 15 4 0 1
After 12 6 3 14 7 2 2 15 4 4 4
A1 A2 A3 A4 A5 A6 A7 A8 A9 A10
Before 13 2 4 14 8 6 5 15 9 2
After 13 2 6 1 8 6 1 2 9 4
Arithmetic Operators:
Addition + Subtraction - Multiplication *
Division / Exponentiation **
Condition-Name condition:
05 AGE PIC 99
88 TEEN-AGER VALUE 13 THRU 19.
…. Dec
IF7,TEEN-AGER
2021 imperative statement 116
PROCEDURE DIVISION
Conditional Expressions
Relation condition:
operand-1 [IS] [NOT] Relation condition operand-2
GREATER THAN or >
LESS THAN or <
EQUAL TO or =
GREATER THAN OR EQUAL TO or >=
LESS THAN OR EQUAL TO or <=
Sign condition:
operand-1 [IS] [NOT] Sign condition imperative statement
POSITIVE
NEGATIVE
ZERO
Dec 7, 2021 117
PROCEDURE DIVISION
Logical Operators
OR : The truth value is true when either
or both conditions are true
AND : The truth value is true when both
conditions are true
Combined condition
condition1 AND [OR] condition2
Complex condition
Dec 7, 2021 118
PROCEDURE DIVISION
IF cond-1 [THEN] statements
ELSE statements
END-IF
Nested If:
IF cond-1 [THEN] statements
ELSE IF cond-2 [THEN] statements
ELSE statements
END-IF
END-IF
GO [ TO ] procedure-name
GO [ TO ] proc1, proc2….DEPENDING [ON]
identifier1
EVALUATE MARKS
WHEN 80 THRU 100 DISPLAY “GRADE=A”
WHEN 60 THRU 79 DISPLAY “GRADE=B”
WHEN 50 THRU 59 DISPLAY “GRADE=C”
WHEN OTHER DISPLAY “GRADE=F”
Dec 7, 2021 121
END-EVALUATE
PROCEDURE DIVISION
MOVE verb for data movement
MOVE iden-1 TO iden-2 [iden-3……..].
MOVE CORR (or, CORRESPONDING) iden-1 TO iden-2.
Sending
Sending Receiving
Receiving
Picture
Picture Data
Data Picture
Picture Result
Result
PIC
PIC999999
999999 123456
123456 PIC
PIC999,999
999,999 123,456
PIC
PIC9(6)
9(6) 000078
000078 PIC
PIC9(3),9(3)
9(3),9(3) 000,078
PIC
PIC9(6)
9(6) 000078
000078 PIC
PICZZZ,ZZZ
ZZZ,ZZZ 78
PIC
PIC9(6)
9(6) 000178
000178 PIC
PIC***,***
***,*** ****178
PIC **2,178
PIC9(6)
9(6) 002178
002178 PIC ***,***
PIC ***,***
PIC
PIC9(6)
9(6) 120183
120183 PIC
PIC99B99B99
99B99B99 120183
PIC
PIC9(6)
9(6) 120183
120183 PIC
PIC99/99/99
99/99/99
12/01/83
PIC 120045
PIC9(6)
9(6) 001245
001245 PIC
PIC990099
990099
Dec 7, 2021 125
Procedure Division – MOVE: Special Insertion
Sending
Sending Receiving
Receiving
Picture
Picture Data
Data Picture
Picture Result
Result
PIC
PIC999V99
999V99 12345
12345 PIC
PIC999.99
999.99 123.45
023.4
PIC
PIC999V99
999V99 02345
02345 PIC
PIC999.9
999.9
12.34
PIC
PIC999V99
999V99 51234
51234 PIC
PIC99.99
99.99 456.00
PIC
PIC999
999 456
456 PIC
PIC999.99
999.99
Sending
Sending Receiving
Receiving
Picture
Picture Data
Data Picture
Picture Result
Result
PIC
PICS999
S999 -123
-123 PIC
PIC-999
-999 -123
PIC 123-
PICS999
S999 -123
-123 PIC
PIC 999-
999- 123
PIC
PICS999
S999 +123
+123 PIC
PIC-999
-999
+12345
PIC -123
PICS9(5)
S9(5) +12345 PIC +9(5)
+12345 PIC +9(5)
123-
PIC
PICS9(3)
S9(3) -123
-123 PIC
PIC+9(3)
+9(3)
PIC
PICS9(3)
S9(3) -123
-123 PIC
PIC999+
999+
Sending
Sending Receiving
Receiving
Picture
Picture Data
Data Picture
Picture Result
Result
PIC
PICS9(4)
S9(4) +1234
+1234 PIC
PIC9(4)CR
9(4)CR 1234
PIC
PICS9(4)
S9(4) -1234
-1234 PIC
PIC9(4)CR
9(4)CR 1234CR
PIC
PICS9(4)
S9(4) +1234
+1234 PIC
PIC9(4)DB
9(4)DB 1223
PIC
PICS9(4)
S9(4) -1234
-1234 PIC
PIC9(4)DB
9(4)DB 1234DB
PIC
PIC9(4)
9(4) 1234
1234 PIC
PIC$99999
$99999
$01234
PIC
PIC9(4)
9(4) 0000
0000 PIC
PIC$ZZZZZ
$ZZZZZ $
Sending
Sending Receiving
Receiving
Picture
Picture Data
Data Picture
Picture Result
Result
PIC
PIC9(4)
9(4) 0000
0000 PIC
PIC$$,$$9.99
$$,$$9.99 $0.00
PIC $80.00
PIC9(4)
9(4) 0080
0080 PIC $$,$$9.00
PIC $$,$$9.00 $128.00
PIC
PIC9(4)
9(4) 0128
0128 PIC
PIC$$,$$9.99
$$,$$9.99 $7,397
PIC
PIC9(5)
9(5) 57397
57397 PIC
PIC$$,$$9
$$,$$9
-5
+80
PIC
PICS9(4)
S9(4) --0005
0005 PIC
PIC++++9
++++9 -80
PIC
PICS9(4)
S9(4) +0080
+0080 PIC
PIC++++9
++++9 ž1234
PIC
PICS9(4)
S9(4) --0080
0080 PIC
PIC-- ------ 99
PIC
PICS9(5)
S9(5) +71234
+71234 PIC
PIC-- ------ 99
Dec 7, 2021 129
MOVE: Suppression and Replacement
Sending
Sending Receiving
Receiving
Picture
Picture Data
Data Picture
Picture Result
Result
PIC
PIC9(5)
9(5) 12345
12345 PIC
PICZZ,999
ZZ,999 12,345
1,234
PIC
PIC9(5)
9(5) 01234
01234 PIC
PICZZ,999
ZZ,999 123
PIC
PIC9(5)
9(5) 00123
00123 PIC
PICZZ,999
ZZ,999 012
PIC *5,678
PIC9(5)
9(5) 00012
00012 PIC ZZ,999
PIC ZZ,999
PIC
PIC9(5)
9(5) 05678
05678 PIC
PIC**,**9
**,**9 ***567
******
PIC
PIC9(5)
9(5) 00567
00567 PIC **,**9
PIC **,**9
PIC
PIC9(5)
9(5) 00000
00000 PIC
PIC**,***
**,***
Note: STOP RUN will close all open files, release the
acquired resources and terminate the execution.
STOP “OK?” – program will stop with a message OK?
and halt. It has to be manually restarted – by console
operator or display/accept statement of the program.
Dec 7, 2021 134
PROCEDURE DIVISION
SET statement is used to perform:
– Index manipulation (table/occurs)
– Incrementing/decrementing an occurrence no.
– Moving data to condition name
01 TAX-CODE PIC 9.
88 EXEMPT VALUE 0.
Format:
CONTINUE.
Paragraph-name.
EXIT.
PERFORM para1.
PERFORM para1 THRU para5.
PERFORM proc1 [THRU proc2] int-1 (or, lit-1) TIMES.
PERFORM para1 THRU para5 10 TIMES.
Dec 7, 2021 139
PROCEDURE DIVISION
In-line PERFORM
PERFORM
ADD……..
……….
END-PERFORM
enter enter
True
condition--Exit execute range
False
execute range condition
Dec 7, 2021 141
PROCEDURE DIVISION
Program
IDENTIFICATION DIVISION.
etc.
ENVIRONMENT DIVISION.
etc.
Record Instance DATA DIVISION.
DISK FILE SECTION.
STUDENTS RecordBuffer
Declaration
MOVE 50 TO REL-KEY.
READ rel-file INTO WS-REL-REC
INVALID KEY GO TO PARA-INVALID.
EXIT PROGRAM.
specifies the end of a called program and returns
control to the calling program.
Input/output procedure:
para-name1 THRU para-name2 or
section-name1 THRU section-name2
PROCEDURE DIVISION.
Begin.
SORT SDFILE ON ASCENDING KEY ProvinceCode
DESCENDING KEY SalesmanCode
USING INPFILE
GIVING SORTEDFILE.
. 7,. 2021
Dec . . . . 167
PROCEDURE DIVISION
IN-FILE /IN-REC OUT-FILE /OUT-REC
WORK-FILE (DEFINED IN SD) /SORT-REC
ACCOUNT-STATUS AMOUNT ACCOUNT-NO
PROCEDURE DIVISION.
SORTING SECTION.
PARA-BEGIN.
SORT WORK-FILE ON ASCENDING KEY ACCOUNT-NO
USING IN-FILE INPUT PROCEDURE IS CHECK-STATUS
GIVING OUT-FILE OUTPUT PROCEDURE IS CHECK-AMOUNT.
-INPSTOP RUN.
CHECK-STATUS SECTION. PARA-
OPEN-INPUT. OPEN INPUT IN-FILE.
PARA-READ.
READ IN-FILE AT END GO TO PARA-END-INPUT.
IF ACCOUNT-STATUS = “9” MOVE “A” TO ACCOUNT-STATUS.
Dec GO TO CHECK-AND-RETURN.
7, 2021 168
P-END. CLOSE OUT-FILE.
Searching Tables
A B C D E F G H I J K ...
01
01 LetterTable.
LetterTable.
02
02 TableValues.
TableValues.
03
03 FILLER
FILLER PIC
PIC X(13)
X(13)
VALUE
VALUE ""ABCDEFGHIJKLM".
ABCDEFGHIJKLM
ABCDEFGHIJKLM".
ABCDEFGHIJKLM
03
03 FILLER
FILLER PIC
PIC X(13)
X(13)
VALUE
VALUE ""NOPQRSTUVWXYZ".
NOPQRSTUVWXYZ
NOPQRSTUVWXYZ".
NOPQRSTUVWXYZ
02
02 FILLER
FILLER REDEFINES
REDEFINES TableValues.
TableValues.
03
03 Letter
Letter PIC
PIC XX OCCURS
OCCURS 26
26 TIMES.
TIMES.
Dec 7, 2021 170
Searching a Table
A B C D E F G H I J K L M
1 2 3 4 5 6 7 8 9 10 11 12 13
01
01 LetterTable.
LetterTable.
02
02 TableValues.
TableValues.
03
03 FILLER
FILLER PIC
PIC X(13)
X(13)
VALUE
VALUE "ABCDEFGHIJKLM".
"ABCDEFGHIJKLM".
03
03 FILLER
FILLER PIC
PIC X(13)
X(13)
VALUE
VALUE "NOPQRSTUVWXYZ".
"NOPQRSTUVWXYZ".
02
02 FILLER
FILLER REDEFINES
REDEFINES TableValues.
TableValues.
03
03 Letter
Letter PIC
PIC XX OCCURS
OCCURS 2626 TIMES.
TIMES.
ACCEPT
ACCEPT LetterIn.
LetterIn.
PERFORM
PERFORM VARYING
VARYING Idx
Idx FROM
FROM 11 BY
BY 11 UNTIL
UNTIL
LetterIn
LetterIn EQUAL
EQUAL TO
TO Letter(Idx)
Letter(Idx)
END-PERFORM.
END-PERFORM.
DISPLAY
DISPLAY LetterIn,
LetterIn, "is
"is in
in position
position ", ", Idx.
Idx.
Dec 7, 2021 171
Search Syntax
1 2 3 4
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4
01 TimeTable.
02 Day OCCURS 5 TIMES INDEXED BY DayIdx.
03 Hours OCCURS 8 TIMES INDEXED BY HourIdx.
04 Item PIC X(10).
04 Location PIC X(10).
SET DayIdx TO 0.
PERFORM UNTIL MeetingFound OR DayIdx > 5
SET DayIdx UP BY 1
SET HourIdx TO 1
SEARCH Hours WHEN MeetingType = Item(DayIdx, HourIdx)
SET MeetingFound TO TRUE
DISPLAY MeetingType " on " DayIdx " at " HourIdx
END-SEARCH
END-PERFORM.
Dec 7, 2021 173
Search All Syntax
A B C D E F G H I J K L M
1 2 3 4 5 6 7 8 9 10 11 12 13
01
01 LetterTable.
LetterTable.
02
02 TableValues.
TableValues.
03
03 FILLER
FILLER PIC
PIC X(13)
X(13)
VALUE
VALUE "ABCDEFGHIJKLM".
"ABCDEFGHIJKLM".
03
03 FILLER
FILLER PIC
PIC X(13)
X(13)
VALUE
VALUE "NOPQRSTUVWXYZ".
"NOPQRSTUVWXYZ".
02
02 FILLER
FILLER REDEFINES
REDEFINES TableValues.
TableValues.
03
03 Letter
Letter PIC
PIC XX OCCURS
OCCURS 26
26 TIMES
TIMES
ASCENDING
ASCENDING KEY
KEY IS
IS Letter
Letter
INDEXED
INDEXED BY
BY LetterIdx.
LetterIdx.
SEARCH
SEARCH ALL
ALL Letter
Letter
WHEN
WHEN Letter(LetterIdx)
Letter(LetterIdx) == LetterIn
LetterIn
DISPLAY
DISPLAY LetterIn,
LetterIn, "is
"is in
in position
position ",
", Idx
Idx
END-SEARCH.
END-SEARCH.
Dec 7, 2021 175
How the Search All works
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
1 26 13 = M
ALGORITHM – Searching for ‘Q’ (say)
Middle = (Lower + Upper) / 2
CASE TRUE
WHEN Letter(Middle) < "Q" THEN Lower = Middle + 1
WHEN Letter(Middle) > "Q" THEN Upper = Middle - 1
WHEN Letter(Middle) = "Q" THEN SET ItemFound TO
TRUE
WHEN Lower > Upper THEN SET ItemNotInTable TO TRUE
Dec 7, 2021 176
Search All Example
01 StateTable.
01 StateTable.
02
02 StateValues.
StateValues.
03
03 FILLER
FILLER PIC
PIC X(20)
X(20) VALUE
VALUE ??????????????
??????????????
PIN
PIN Codes
Codes and
and Names
Names
03
03 FILLER
FILLER PIC
PIC X(20)
X(20) VALUE
VALUE ??????????????
??????????????
02
02 FILLER
FILLER REDEFINES
REDEFINES StateValues.
StateValues.
03
03 States
States OCCURS
OCCURS 50
50 TIMES
TIMES
ASCENDING
ASCENDING KEY
KEY IS
IS StateName
StateName
INDEXED
INDEXED BY
BY StateIdx.
StateIdx.
04
04 PinCode
PinCode PIC
PIC X(6).
X(6).
04
04 StateName
StateName PIC
PIC X(14).
X(14).
SEARCH
SEARCH ALL
ALL States
States
AT
AT END
END DISPLAY
DISPLAY "State
"State not
not found"
found"
WHEN
WHEN StateName(StateIdx)
StateName(StateIdx) == InputName
InputName
MOVE
MOVE PinCode(StateIdx)
PinCode(StateIdx) TO
TO PrintPinCode
PrintPinCode
END-SEARCH.
END-SEARCH.
Dec 7, 2021 177
INSPECT
Before
F F F F A F F F X F Q F F F Z
After
F F F F A G G G X G Q F F F Z
StringData
F F F F A F F F F F Q F F F Z
initial A range of replacing initial Z
Leading F leading-effect-terminates
After
F F F F A G G G G G Q F F F Z
StringData
F F F F A F F F F F Q F F F Z
F F F F A G G G G G Q G G G Z
After
StringData
F F F F A F F F F F Q F F F Z
After
F F F F A G F F F F Q F F F Z
StringData
F F F F A F F F F F Q F F F Z
After
F F F F A F R O G F Q F F F Z
Dec 7, 2021 186
STRING
- - - - - - - - - - - - - - -
5 - - - - - - - - - - - - - -
5 , - - - - - - - - - - - - -
5 , J U N E - - - - - - - - -
5 , J U N E , - - - - - - - -
5 , J U N E , 1 9 9 4 - - - -
MOVE 6 TO StrPtr.
STRING Field1, Field3 DELIMITED BY SPACE
INTO Field2 WITH POINTER StrPtr
ON OVERFLOW DISPLAY "String Error"
NOT ON OVERFLOW DISPLAY Field2
END-STRING.
MOVE 6 TO StrPtr.
STRING Field1, Field3 DELIMITED BY SPACE
INTO Field2 WITH POINTER StrPtr
ON OVERFLOW DISPLAY "String Error"
OR
• DELIMITED BY
Specifies delimiters within the data that control the
data transfer. If “DELIMITED BY” is not used, then
“DELIMITER IN” and “COUNT IN” must not be used.
• INTO
Specifies the fields where the data is to be moved
(destination or receiving field).
• DELIMITER IN
A DELIMITER IN clause is associated with a
particular Destination String. iden-5 represents the
delimiter receiving field.
Dec 7, 2021 204
UNSTRING clauses
• ON OVERFLOW.
The ON OVERFLOW is activated if :-
• The Unstring pointer (Pointer#i) is not pointing
to a character position within the Source-String
when the UNSTRING executes.
• All the Destination Strings have been processed
but there are still valid unexamined characters
in the Source String.
• COUNT IN
The COUNT IN clause is associated with a
particular Destination String and holds a count of the
number of characters passed to the Destination
String.
• TALLYING IN
Only one TALLYING clause can be used with each
UNSTRING. It holds a count of the number of
Destination Strings affected by the UNSTRING
operation.
Dec 7, 2021 205
The UNSTRING clauses
• WITH POINTER
The value of the pointer field behaves as if it
were increased by 1 for each examined character
in the sending field. After the execution, the
pointer contains a value equal to its initial value
plus the number of characters examined in the
sending field.
• ALL
When the ALL phrase is used, contiguous
delimiters are treated as if only one delimiter had
been encountered.
ACCEPT DateStr.
UNSTRING DateStr
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
display device
ACCEPT DateStr.
UNSTRING DateStr
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
ACCEPT DateStr.
UNSTRING DateStr
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
ACCEPT DateStr.
UNSTRING DateStr
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
ACCEPT DateStr.
UNSTRING DateStr
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
Chars
Chars Left
Left
1 9 s t o p 0 5 s t o p 8 0
No overflow
ACCEPT DateStr.
UNSTRING DateStr DELIMITED BY "stop"
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY "Chars Left"
END-UNSTRING.
Note:
ADD is a reserved word. But ADD1 or ADD-1 is not.
Source
Program List
Copy
Compiler
Libraries
Object Module
LE Output
Object
Link editor
Libraries
Load Module
Appln. O/P
Appln.
Dec 7, 2021
Data Application Program 224
The End
Thank You
and
Best of Luck