COBOL
COBOL – Common Business Oriented Language
COBOL is an application programming language
COBOL implemented (introduced) by CODASYL in 1950’s
CODASYL – Conference On Data Systems Languages
COBOL was standardized by ANSI in 1960’s
ANSI – American National Standards Institute
COBOL is structured programming language
COBOL does not contain any graphics
COBOL is an English like language which is easily readable
COBOL is easy to understand, easy to code, easy to write also because 90%
of COBOL code runs on character based environment
COBOL is commercial programming language mainly implemented to solve
business problems
COBOL is very famous in file handling
COBOL is an 80 byte record structure divided into different areas as
follows…..
COBOL CODING SHEET
1-6 7 8-11 12-72 73-80
COBOL / Area-A or Area-B or Ignored or
Standard * Margin-A Margin-B Comment
Numbers - 1. Divisions 1. field
D 2. Sections Statements
3. 2. Sentences
Paragraphs 3. Level
4. Level No# No#(02-49,
(01,77) 66, 88)
Column 1-6
In COBOL column 1-6 defined for COBOL STANDARD NUMBERS.
These are auto generated, no need to declare
COBOL STANDARD NUMBERS will always appear in yellow color
Column 7
Column 7 in COBOL is used as continuation column
The possible values in Column 7 are
1. / - Specifies new line (also used for commenting COBOL lines)
2. * - it is used for commenting COBOL lines
3. –(HIPHEN) – Continuation Character
4. D – Debugging
Column 8-11
It is called as Area-A also called as Margin-A
In Area-A we can write
1. Divisions
2. Sections
3. Paragraphs
4. Level No# (01,77)
Level No’s we have start from column 8 only
Column 12-72
It is called as Area-B also called as Margin-B
In Area-B we can write
1. Statements
2. Sentences
3. Level No# (02-49, 66, 88)
Level No’s should start from column 12(May or MayNot)
Column 73-80
It is an ignored field also called as commented field
COBOL RESERVED WORDS
In COBOL programming we are having some predefined words, they are
also called as COBOL RESERVED WORDS
All COBOL RESERVED WORDS are also called as COBOL VERBS
In COBOL we are having mainly 3 types of COBOL RESERVED
WORDS. They are………….
1. Keywords
2. Optional Words
3. Figurative Constants
1. KEYWORDS
All Keywords are also called as COBOL VERBS. The list of Keywords
available in COBOL are as follows…………
1. ADD
2. SUBTRACT
3. DIVIDE etc..
2. OPTIONAL WORDS
Optional Words does not affect the application programming
EX. GIVING, AFTER, BEFORE, ROUNDED etc…..
ADD A TO B GIVING C. C = A + B.
ADD A TO B. A+B
3. FIGURATIVE CONSTANTS
All Figurative Constants will contain a predefined value in it
The list of Figurative Constants is
EX
HIGH-VALUE ZERO SPACE LOW-VALUE
HIGH-VALUES ZEROES SPACES LOWVALUES
ZEROS
ZERO, ZEROS are used to initialize Numeric Data types
ZEROES is used to initialize Alphanumeric Data types
SPACE, SPACES is used to initialize Alphabetic and Alphanumeric Data
types
LOWVALUE, LOWVALUES and HIGHVALUE, HIGHVALUES are
mainly used to set lowest and highest ordinal values. We use
LOWVALUE(S) in CICS to initialize the MAPS
USER DEFINED WORDS
In COBOL we can also use USER DEFINED WORDS
While writing USER DEFINED WORDS we are having some rules as
follows..
1. USER DEFINED WORDS should not be COBOL RESERVED WORDS
2. The max length of USER DEFINED WORD is 35 characters
3. USER DEFINED WORD should not start or end with ‘ – ‘
4. Possible values are a-z, 0-9, and’ – ‘
5. The names must be Alphabetic only
6. Spaces between the characters not allowed
7. Special characters not allowed in the name field
We have several user defined words as follows………
1. Statements – Statement is a collection of COBOL word ending with a period
(.)
Statements are two types. They are
a. Imperative Statement
b. Conditional Statement
a. IMPERATIVE STATEMENT – it starts with a verb and no conditional
action is to be taken out
EX. MOVE, DISPLAY etc
b. CONDITIONAL STATEMENT – it starts with a COBOL VERB and
specifies a conditional action to be taken
EX. IF and ELSE, EVALUATE etc..
2. Sentence
Sentence is a collection of Statements
3. Paragraphs
It is a collection of Sentences
PARAGRAPH must start from Area-A
PARAGRAPH name may contain a-z, 0-9 and ‘ – ‘ combinations
The max length of PARAGRAPH name is 35 characters
Spaces between the PARAGRAPH name not allowed
4. Sections
It is a collection of PARAGRAPHS
SECTION is a second highest unit in COBOL
SECTION always contain the PARAGRAPHS
5. Divisions
DIVISION is a collection of SECTIONS
Every SECTION must be derived from DIVISION only
DIVISIONS in COBOL
In COBOL we are having 4 divisions. The DIVISION is largest unit in
COBOL
The 4 DIVISIONS in COBOL are
1. Identification Division
2. Environment Division
3. Data Division
4. Procedure Division
1. IDENTIFICATION DIVISION
This is the 1st division in COBOL and also it is mandatory for every COBOL
program
Identification division does not contain any Sections
The list of Paragraphs in Identification division are as follows……
PROGRAM-ID.<NAME>.
AUTHOR.<NAME>
DATE-WRITTEN.<DATE>
DATE-COMPILED.<DATE>
In all the above PARAGRAPH PROGRAM-ID.<NAME>. is mandatory
because PROGRAM-ID.<NAME>. specifies name of a program written by the
user
2. ENVIRONMENT DIVISION
It specifies external resources using in a program like files and printer
etc(data base access)
This is optional. When we use files in a COBOL program then only it is
mandatory
In environment division we are having mainly 2 sections
1. Configuration Section
2. Input – output Section
1. CONFIGURATION SECTION
It is optional. It contain 3 paragraphs
a. Source-Computer
b. Object-Computer
c. Special-names
These 3 paragraphs are not required for every program
2. INPUT – OUTPUT SECTION
It is useful for including the files into a COBOL program. In this we have
an important paragraph FILE-CONTROL
FILE-CONTROL
It is mainly used to declare the files in program (logical file
declaration)
3. DATA DIVISION
It specifies the data names using in a program (variable names). In this
we have 3 sections. They are……
A. Linkage Section
B. File Section
C. Working-storage Section
A. LINKAGE SECTION
It is also called as GLOBAL VARIABLE DECLARATION SECTION
Mainly used for accepting the data from outside the program into currently
running application program (sub routines)
B. FILE SECTION
It is required only when FILE-CONTROL in ENVIRONMENT
DIVISION is declared because FILE SECTION specifies the attributes of a file
C. WORKING-STORAGE SECTION
It is also called as LOCAL VARIABLES DECLARATION SECTION
In general this is alone required for individual COBOL programs to declare a
variable
4. PROCEDURE DIVISION
It does not contain SECTIONS
In procedure division user can write logic
Procedure division is also required for every program
NOTE
In COBOL continuation character is’ – ‘
In COBOL each and every statement must end with period (.)
It is called as SCOPE TERMINATOR
In COBOL there are 2 types of SCOPE TERMINATORS. They are…..
1. IMPLICIT SCOPE TERMINATOR – PERIOD (.)
2. EXPLICIT SCOPE TERMINATOR
EXPLICIT SCOPE TERMINATOR always start with the prefix “END-“
EX END-IF
END-EVALUATE
END-PERFORM etc
In COBOL whenever an application programmer want to print a data in
output area. We use “DISPLAY” statement. “DISPLAY” will send the data
or data of a variable on to the spool (output section)
In COBOL we can also receive the data into a program dynamically at run-
time by using ACCEPT statement
DATA NAMES AND ITS RULES
Data Name gives the reference in the storage
Data Names in general called as Variable Names
Data Name should not start or end with ‘ - ‘
EX -AB
AB-
Spaces in the data name not allowed
EX AB is right A B is wrong
Data name should start with alphabet only
Possible values are a-z, 0-9, and ‘ – ‘
DATA TYPES IN COBOL
In COBOL application programming language we are having mainly 3
data types
1. Numeric
2. Alphabetic
3. AlphaNumeric
NUMERIC data types are represented with ‘9’
ALPHABETIC data types are represented with ‘A’
ALPHANUMERIC data types are represented with ‘X’
1. Numeric Data Types (9)
The possible values for Numeric are 0-9
In numeric the default values 0’s
In numeric the allocation will be done from right to left
The maximum range is 18 digits
9(02) = 2 bytes of numeric data type
00
00 = 05 = 5?
9(03) 000 12
012 =12
9(06) 12345
012345
9(01) = 10101010
2. Alphabetic (A)
Possible values are a-z
The default values are Spaces
Allocation will be done from left to right
A(04) = Balaji
Bala
3. Alphanumeric (X)
Possible values are a-z, 0-9
Default values are Spaces
Allocation is from left to right
X(04) Balaji
Bala
ZEROES = X(04) 1234
01 A PIC X(04) VALUE ZEROES.
1234
ALLOCATION OF DATA ITEMS
To declare any data item in COBOL program we have to follow the
structure as follows…..
PICTURE DATA
LEVEL NO# DATA NAME VALUE CLAUSE
CLAUSE TYPE(LENGTH)
01 a-z PIC 9(n) VALUE..........
02-49 0-9 A(n) VALUE '..........'
66 - X(n) VALUE '...........'
77
88
LEVEL NO#
In COBOL level no# specifies hierarchy of the COBOL program
In COBOL all level no’s must be written in ascending order only
The available level no# are
01
Used as group item as well as elementary data item
01 should start from column 8
02-49 – it is used as sub-groups should start in Area-B only
66 – specifies RENAMES should start in Area-B
77 – elementary dataitem
Pure elementary data item must begin from column 8
It does not contain sub-groups
88 – used for condition checking begin in Area-B
PICTURE CLAUSE (PIC)
Picture Clause specifies the attributes of a data item
It also specifies the data types and its properties
In picture clause we are having 2 types
a. Edited Picture Clause
b. Non-Edited Picture Clause
VALUE CLAUSE
By using value clause we can assign a data for the data item
Depends on data type we have to write a value
Value clause should not exceed the length given for the data item
FILLER CLAUSE
Filler is a predefined word in COBOL mainly used for to allocate spaces
between the data items in group
Filler also used in files to nullify the unused fields with spaces
NOTE
In COBOL only Identification division is mandatory and sometimes
Procedure division also
PICTURE CLAUSE AND ITS TYPES
Picture Clause always specifies the attributes of a data item
Picture Clause mainly divided into 2 types
a. Edited Picture Clause
b. Non-Edited Picture Clause
a. Edited Picture Clause
Edited picture clause mainly used for display purpose
We cant implemented in arithmetic operations
Edited picture clause divided into 2 types
A. Insertion
B. Suppression
A. INSERTION
In Insertion we are having mainly 4 types
1. Simple Insertion
2. Special Insertion
3. Fixed Insertion
4. Float Insertion
1. SIMPLE INSERTION
In simple insertion we are having some possible values
B – Blank
/ - Inserting ‘ / ‘
0 (zero) – Inserting ‘ 0 ‘
, - Insering ‘ , ‘
According to the picture clause the values will be allocated
EX.1 SAMPLE COBOL PROGRAM
ID DIVISION.
PROGRAM-ID. PGM1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC A(3) VALUE 'ABC'.
01 B PIC 9(3) VALUE 456.
01 C PIC X(3) VALUE 'A3C'.
PROCEDURE DIVISION. OUTPUT
DISPLAY ‘WELCOME TO COBOL’. WELCOME TO
COBOL
DISPLAY ‘A VALUE ::’ A. A VALUE:: ABC
DISPLAY ‘B VALUE ::’ B. B VALUE:: 456
DISPLAY ‘C VALUE ::’ C. C VALUE:: A3C
STOP RUN.
EX.2
ID DIVISION.
PROGRAM-ID. PGM1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC A(3).
01 B PIC 9(3).
01 C PIC X(3).
PROCEDURE DIVISION.
DISPLAY ‘WELCOME TO COBOL’.
ACCEPT A.
ACCEPT B.
ACCEPT C.
DISPLAY A.
DISPLAY B.
DISPLAY C.
STOP RUN.
OUTPUT
WELCOME TO COBOL
ABC
456
A3C
NOTE
By using the ‘VALUE CLAUSE’ we cant assign a value more the given
‘PICTURE CLAUSE’
But in case of ‘ACCEPT’ even though the input value is greater it takes only
the character depends on ‘PICTURE CLAUSE’ length (In ACCEPT the
value will be assigned to the data item position by position)
When you assign a value using VALUE CLAUSE 1st allocation will be done
depending on PICTURE CLAUSE
PROGRAM TERMINATOR STATEMENTS
Mainly used to stop the flow of an application program
We have 3 program terminator statements. They are
1. STOP RUN – After successful execution of the program the control
will be transferred to Operating System
2. GO BACK – After execution of called cursor will be sent to calling
program. It also acts as ‘STOP RUN’ when coded in calling program
3. EXIT PROGRAM – After successful execution of called program
control will sent to calling program
EX. SIMPLE INSERTION
ID DIVISION.
PROGRAM-ID. PGM1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 VAL1 PIC 9(4) VALUE 1234.
01 A PIC 9(3)B9(1).
01 B PIC 9(2)/9(2).
01 C PIC 9(2),9(2).
01 D PIC 9(2)09(1)09.
PROCEDURE DIVISION.
MOVE VAL1 TO A,B,C,D.
DISPLAY VAL1.
DISPLAY A.
DISPLAY B.
DISPLAY C.
DISPLAY D.
STOP RUN.
OUTPUT
A – 123 4
B – 12/34
C – 12,34
D – 120304
NOTE
In insertion according to the given picture clause the value will be allocated
In general
9 = 9(1) A = A(1) X = X(1)
99 = 9(2) AA = A(2) XX = X(2)
999 = 9(3) AAA = A(3) XXX = X(3)
2. SPECIAL INSERTION
In ‘special insertion’ we are having only one value i.e., Decimal Point (.)
In Decimal always allocation will be done before Decimal Point only
We cant use special insertion in arithmetic operations
01 A PIC 9(5).9(5).
MOVE 12.34 TO A
00012.34000
EX
ID DIVISION.
PROGRAM-ID. PGM1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(3).9(2).
01 B PIC 9(2).9(2).
01 C PIC 9.9.
01 D PIC 9(3).(5) VALUE ZEROS.
01 E PIC 9(3).
PROCEDURE DIVISION.
MOVE 1234 TO A.
DISPLAY A.
MOVE 121.1173 TO A.
DISPLAY A.
MOVE 4567.6674 TO B.
DISPLAY B.
MOVE 1.11 TO C.
DISPLAY C.
MOVE 131415.0130 TO D.
DISPLAY D.
MOVE 4532.113 TO E.
DISPLAY E.
STOP RUN.
3. FLOAT INSERTION
In float insertion we are having mainly 3 possible values
1. S – Sign
2. + - Positive
3. - - negative
1. S (Sign Clause)
In general, in COBOL we cant write ‘+‘ or ‘–‘ values for the data item
but by using sign clause we can write the values because in COBOL each and
every ‘+’ and ‘–‘ values are having their own combinations in sign clause
+0 = { -0 = }
+1 = A -1 = J
+2 = B -2 = K
+3 = C -3 = L
+4 = D -4 = M
+5 = E -5 = N
+6 = F -6 = O
+7 = G -7 = P
+8 = H -8 = Q
+9 = I -9 = R
In sign clause we are having mainly 4 types. They are……..
a. SIGN LEADING
b. SIGN LEADING SEPERATE
c. SIGN TRAILING
d. SIGN TRAILING SEPARATE
a. SIGN LEADING
LEADING means left most position
In SIGN LEADING the sign will be applied to the left most position i.e., 1st
character
At a time only one value will be converted
EX -123 +123
J23 A23
Because -1 = J and +1 = A
b. SIGN LEADING SEPARATE
In SIGN LEADING SEPARATE the sign will be displayed separately
from the data in leading position
EX +123 -123
+ 123 - 123
c. SIGN TRAILING
Trailing means right most position
In SIGN TRAILING the sign will be applied right most positioned 1st
character
It is default in sign clause
EX +123 -123
123+ 123-
12+3 12-3
d. SIGN TRAILING SEPARATE
In SIGN TRAILING SEPARATE the sign will be displayed separately
from the data in trailing position
EX +123 -123
123+ 123-
123 + 123 –
EX
ID DIVISION.
PROGRAM-ID. PGM1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC S9(3)VALUE +345.
01 B PIC S9(3) SIGN LEADING.
01 C PIC S9(3) SIGN LEADING SEPERATE.
01 D PIC S9(3) SIGN TRAILING.
01 E PIC S9(3) SIGN TRAILING SEPERATE.
PROCEDURE DIVISION.
MOVE A TO B,C,D,E.
DISPLAY A.
DISPLAY B.
DISPLAY C.
DISPLAY D.
DISPLAY E.
STOP RUN.
OUTPUT
A – 34E
B – C45
C - + 345
D – 34E
E – 345 +
+, - Insertion
In sign clause we are having predefined rules while moving the data from
one sign to another sign
+ - NO SIGN
+ + +
- + -
- - -
EX
ID DIVISION.
PROGRAM-ID. PGM1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC S9(3)VALUE +345.
01 B PIC +9(3).
01 C PIC -9(3).
01 D PIC S9(3) VALUE -345.
01 E PIC +9(3).
01 F PIC -9(3).
PROCEDURE DIVISION.
MOVE A TO B,C.
MOVE D TO E,F.
DISPLAY A. 34E
DISPLAY B. +345
DISPLAY C. 345
DISPLAY D. 34N
DISPLAY E. -345
DISPLAY F. -345
STOP RUN.
4. FIXED INSERTION
In fixed insertion we are having mainly 3 values. They are……
1. CR – Credit
2. DB – Debit
3. $ - Inserting $ symbol
CR,DB will appear only for negative values
EX
ID DIVISION.
PROGRAM-ID. PGM1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC S9(3)VALUE -123.
01 B PIC 9(3)CR.
01 C PIC 9(3)DB.
01 D PIC S9(3) VALUE +123.
01 E PIC 9(3)CR.
01 F PIC 9(3)DB.
01 G PIC $ 9(4) VALUE ZEROS.
PROCEDURE DIVISION.
MOVE A TO B,C.
MOVE D TO E,F.
MOVE 1234 TO G.
DISPLAY A. 123- = 12L
DISPLAY B. 123CR
DISPLAY C. 123DB
DISPLAY D. 123+ = 12C
DISPLAY E. 123
DISPLAY F. 123
DISPLAY G. $1234
STOP RUN.
In the above example CR,DB signs will be displayed
only for negative values
B. SUPPRESSION
In suppression we are having mainly two values. They are…
1. Z – leading Zeros suppression
2. * - leading Zeros replacement by’*’
According to the given picture clause the zeros will be suppressed
EX
ID DIVISION.
PROGRAM-ID. PGM1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(7)VALUE 0001234.
01 B PIC Z9(6).
01 C PIC ZZ9(5).
01 D PIC ZZZ9(4).
01 E PIC *9(6).
01 F PIC **9(5).
01 G PIC ***9(4).
PROCEDURE DIVISION.
MOVE A TO B,C,D,E,F,G. OUTPUT
DISPLAY A. 0001234
DISPLAY B. 001234
DISPLAY C. 01234
DISPLAY D. 1234
DISPLAY E. *001234
DISPLAY F. **01234
DISPLAY G. ***1234
STOP RUN.
b. NON-EDITED PICTURE CLAUSE
Non-Edited picture clause may be included in arithmetic operations
In non-editing picture clause we are having a value i.e.,
V – Imaginary decimal point
EX
1. 9(2).9(2) VALUE 12.34 – wrong
2. 9(2)V9(2) VALUE 12.34 – right
1.34 internally is 1234
EX
ID DIVISION.
PROGRAM-ID. PGM1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(3)V9(3) VALUE 123.456.
01 B PIC 9(3)V9(3) VALUE 111.123.
01 C PIC 9(3).9(3) VALUE ZEROS.
PROCEDURE DIVISION.
ADD ATO B GIVING C. 123456+111123
DISPLAY ‘C VALUE IS::’ C. C VALUE IS:: 234.579
STOP RUN.
RENAMES CLAUSE
In COBOL level no# 66 is used as RENAMES
66 level no# must start in Area-B
Using 66 level no# we can re-group the data items only within a group
We can’t use renames between two groups
SYNTAX
66 DATANAME RENAMES <D.NAME1> THRU <D.NAMEn>
EX
ID DIVISION.
PROGRAM-ID. PGM10.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 GRP1.
02 ENO PIC 9(4) VALUE 1001.
02 ENAME PIC A(15) VALUE 'SRI'.
02 ESAL PIC 9(5) VALUE 3000.
02 EADDR PIC X(10) VALUE 'HYD1'.
02 RES1 PIC X(55).
66 A RENAMES ENO THRU ESAL.
66 B RENAMES ENAME THRU EADDR.
66 C RENAMES ENO THRU EADDR.
66 D RENAMES ESAL THRU EADDR.
PROCEDURE DIVISION.
DISPLAY A.
DISPLAY B.
DISPLAY C.
DISPLAY D.
MOVE C TO RES1.
DISPLAY RES1.
STOP RUN.
DECLERATION OF GROUP ITEMS
In COBOL 01 level no# is used for group item
Group item does not contain picture clause because……
Sub group elements size = group size
We can use 02 – 49 level no# for sub-groups
Sub-group 02 – 49 must start from Area-B (12th Column)
EX
ID DIVISION.
PROGRAM-ID. PGM11.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 GRP1.
02 ENO PIC 9(4) VALUE 1001.
02 ENAME PIC A(15) VALUE 'RAJU'.
02 ESAL PIC 9(5) VALUE 25000.
02 EADDR PIC X(10) VALUE 'HYD'.
PROCEDURE DIVISION.
DISPLAY 'GROUP ITEMS'.
DISPLAY GRP.
DISPLAY LENGTH OF GRP1.
STOP RUN.
USING FILLER CLAUSE
By using filler clause we can also allocate no of required
spaces between the sub-group elements existed in a group
EX
ID DIVISION.
PROGRAM-ID. PGM12.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 GRP1.
02 ENO PIC 9(4) VALUE 1001.
02 FILLER PIC X(2).
02 ENAME PIC A(4) VALUE 'RAJU'.
02 FILLER PIC X(2).
02 ESAL PIC 9(5) VALUE 25000.
02 FILLER PIC X(2).
02 EADDR PIC X(3) VALUE 'HYD'.
PROCEDURE DIVISION.
DISPLAY GRP1.
DISPLAY LENGTH OF GRP1.
STOP RUN.
LEVEL NO# 77
In COBOL level no# 77 is Pure elementary data item
Under 77 we cant write sub-group elements because in COBOL all level
no’s should come in ascending order
EX
ID DIVISION.
PROGRAM-ID. PGM13.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 X PIC 9(5) VALUE 17745.
77 Y PIC A(5) VALUE ‘SRINU’.
77 Z PIC X(3) VALUE ‘A2B’.
PROCEDURE DIVISION.
DISPLAY X.
DISPLAY Y.
DISPLAY Z.
STOP RUN.
INITIALIZE VERB
Initialize is mainly used for initializing the data item with their default values
Using ‘initialize’ we can effectively ‘REPLACE’ ‘FIGURATIVE
CONSTANTS’
SYNTAX
INITIALIZE DATANAME
EX
ID DIVISION.
PROGRAM-ID. PGM13.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 X PIC 9(5) VALUE 17745.
77 Y PIC A(5) VALUE ‘SRINU’.
77 Z PIC X(3) VALUE ‘A2B’.
PROCEDURE DIVISION.
INITIALIZE X.
MOVE 456 TO X.
DISPLAY X.
INITIALIZE Y.
DISPLAY Y.
INITIALIZE Z.
MOVE X2Y TO Z.
INITIALIZE Z.
DISPLAY Z.
MOVE ‘ABC4’ TO Z.
DISPLAY Z.
STOP RUN.
MOVE STATEMENT
By using MOVE we can move the data to any no of data items and also we
can move the data from one data item to any no of data items
SYNTAX
MOVE D.NAME1 TO D.NAME2,D.NAME3,………..,D.NAMEn
MOVE VALUE TO D.NAME1,D.NAME2,………..,D.NAMEn
In MOVE we are having two types. They are ……..
1. Group MOVE
2. Elementary MOVE
We can move the data from one group to another group
and also from elementary data item to another elementary data item
EX
ID DIVISION.
PROGRAM-ID. PGM13.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 GRP1.
02 A1 PIC A(5) VALUE 'COBOL'.
02 A2 PIC 9(3) VALUE 123.
01 GRP2.
02 B1 PIC A(5) VALUE 'HELLO'.
02 B2 PIC 9(5) VALUE 45675.
77 X PIC 9(3) VALUE 177.
77 Y PIC 9(5).
77 Z PIC 9.
77 C PIC 9(2).
PROCEDURE DIVISION.
MOVE GRP1 TO GRP2.
MOVE X TO Y,Z,C.
DISPLAY GRP1.
DISPLAY GRP2.
DISPLAY X.
DISPLAY Y.
DISPLAY Z.
DISPLAY C.
STOP RUN.
While moving data from one group to another group irrespective of sub
group elements data it transpose the data from group to group
In MOVE 1st allocation will be done and then the value will be counted
REDEFINES CLAUSE
By using REDEFINES we can use any existing group memory(size) in any
sub-sequent group
We can also REDEFINE multiple items
Using REDEFINES we can request the total memory of one group into the
next group
For non 01 level redefinition the redefining item size must be lesser or equal
to item being redefined
For 01 level redefinition the redefining item size must be greater or lesser or
equal to item being redefined
EX
ID DIVISION.
PROGRAM-ID. PGM14.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 GRP1. 20bytes
02 A. 10 bytes
03 A1 PIC A(5) VALUE 'COBOL'.
03 A2 PIC 9(5) VALUE 12345.
02 B REDEFINES A. =<
03 B1 PIC A(2).
03 B2 PIC 9(3).
03 B3 PIC X(5).
01 GRP2 REDEFINES GRP1. =<>
02 C PIC A(20).
PROCEDURE DIVISION.
DISPLAY GRP1.
DISPLAY A.
DISPLAY B.
DISPLAY B1.
DISPLAY GRP2.
STOP RUN.
NOTE
In REDEFINES while redefining from one group to
another group the data of redefining group will also appear in redefining group
CONDITIONAL NAMES
In COBOL level no# 88 is used for condition checking
Level no# 88 must start from 12th column only
It does not contain any picture clause
EX
ID DIVISION.
PROGRAM-ID. PGM15.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SEL PIC X(2).
88 VOWELS VALUES 'A','E','I','O','U'.
88 EVEN VALUES '2','4','6','8','10','12','14','16'.
88 ODD VALUES '1','3','5','7','9','11','13','15'.
PROCEDURE DIVISION.
ACCEPT SEL.
IF(VOWELS)
DISPLAY 'ENTERED VOWELS'
ELSE IF EVEN
DISPLAY 'ENTERED EVEN'
ELSE IF ODD
DISPLAY 'ENTERED ODD'
END-IF.
DISPLAY SEL.
STOP RUN.
USAGE CLAUSE
Usage is mainly used for reducing the memory by usage also specifies the
internal allocation
By having USAGE we can store large amount of data in small amount of
storage
In USAGE we have mainly 4 types. They are…..
1. USAGE COMP
2. USAGE COMP-1
3. USAGE COMP-2
4. USAGE COMP-3
1. USAGE COMP
COMP means Computational
COMP is binary format having specific ranges
In COMP we have fixed range values as follows
9(1) – 9(4) 2 Bytes
9(5) – 9(9) 4 Bytes
9(10) – 9(18) 8 Bytes
EX
01 A PIC 9(3) USAGE COMP.
PROCEDURE DIVISION.
MOVE 123 TO A.
DISPLAY A.
STOP RUN.
2. USAGE COMP-1
COMP-1 is single precision floating point number
COMP-1 does not contain picture clause because the value of COMP-1 is
fixed for 4 Bytes
COMP-1 accepts only 8 to 9 characters of data
EX
01 A USAGE COMP-1
3. USAGE COMP-2
COMP-2 is a double precision floating point number
COMP-2 does not contain picture clause because the value is fixed for 8
Bytes
COMP-2 accepts 17 to 18 characters of data
EX
01 A USAGE COMP-2
4. USAGE COMP-3
COMP-3 is a packed decimal format
Packed decimal means “it stores two characters of data within 1 Byte of
storage”
In COMP-3 depending on picture clause it allocates the memory for the data
item
If picture clause is even. Then……..
N/2+1, where n = length of picture clause
EX
01 A PIC 9(6) USAGE COMP-3
N/2+1 = 6/2+1 = 4 Bytes
If picture clause is odd. Then……….
N+1/2
EX
01 PIC 9(7) USAGE COMP-3
7+1/2 = 4 Bytes
EX
ID DIVISION.
PROGRAM-ID. PGM16.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(3) USAGE COMP.
01 B PIC 9(5) USAGE COMP.
01 C PIC 9(17) USAGE COMP.
01 D USAGE COMP-1.
01 E USAGE COMP-2.
01 F PIC 9(3) USAGE COMP-3.
01 G PIC 9(5) USAGE COMP-3.
01 H PIC 9(6) USAGE COMP-3.
01 I PIC 9(16) USAGE COMP-3.
PROCEDURE DIVISION.
DISPLAY LENGTH OF A.
DISPLAY LENGTH OF B.
DISPLAY LENGTH OF C.
DISPLAY LENGTH OF D.
DISPLAY LENGTH OF E.
DISPLAY LENGTH OF F.
DISPLAY LENGTH OF G.
DISPLAY LENGTH OF H.
DISPLAY LENGTH OF I.
STOP RUN.
ARITHMETIC VERBS
In COBOL we have mainly 4 Arithmetic Verbs. They are…
1. ADD
2. SUBTRACT
3. MULTIPLY
4. DIVIDE
1. ADD – Used to perform Addition Operation
SYNTAX
1. ADD D.NAME1 TO D.NAME2
2. ADD D.NAME1,D.NAME2………..TO D.NAMEN
3. ADD D.NAME1 TO D.NAME2 GIVING D.NAME3
4. ADD D.NAME1,D.NAME2……….. TO D.NAMEN GIVING
D.NAMEX
2. SUBTRACT –
SYNTAX
1. SUBTRACT D.NAME1 FROM D.NAME2
2. SUBTRACT D.NAME1,D.NAME2………..FROM D.NAMEN
3. SUBTRACT D.NAME1 FROM D.NAME2 GIVING D.NAME3
3. MULTIPLY –
SYNTAX
1. MULTIPLY D.NAME1 BY D.NAME2
2. MULTIPLY D.NAME1 BY D.NAME2 GIVING D.NAME3
4. DIVIDE –
While performing Division Operation we have 2 ways
CASE1: USING ‘BY’
SYNTAX
1. DIVIDE D.NAME1 BY D.NAME2
2. DIVIDE D.NAME1 BY D.NAME2 GIVING D.NAME3
3. DIVIDE D.NAME1 BY D.NAME2 GIVING D.NAME3
REMAINDER D.NAME4
EX
DIVIDE A BY B GIVING C i.e., C = A/B
CASE2: USING ‘INTO’
SYNTAX
1. DIVIDE D.NAME1 INTO D.NAME2
2. DIVIDE D.NAME1 INTO D.NAME2 GIVING D.NAME3
3. DIVIDE D.NAME1 INTO D.NAME2 GIVING D.NAME3
REMAINDER D.NAME
EX
DIVIDE A INTO B GIVING C i.e., C =B/A
EX1
ID DIVISION.
PROGRAM-ID. PGM17.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(2).
01 B PIC 9(2).
01 C PIC 9(4).
PROCEDURE DIVISION.
ACCEPT A.
ACCEPT B.
ADD A TO B GIVING C.
DISPLAY C.
INITIALIZE A.
INITIALIZE B.
ADD A,B TO B GIVING C.
DISPLAY C.
INITIALIZE A.
INITIALIZE B.
ADD A TO B.
DISPLAY B.
STOP RUN.
EX2
ID DIVISION.
PROGRAM-ID. PGM19.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(2).
01 B PIC 9(2).
01 C PIC 9(4).
PROCEDURE DIVISION.
ACCEPT A.
ACCEPT B.
SUBTRACT A FROM B GIVING C.
DISPLAY C.
SUBTRACT A,B FROM B GIVING C.
DISPLAY C.
SUBTRACT A,B FROM B.
DISPLAY B.
STOP RUN.
While doing SUBTRACT in the above example B value must be greater
than A
EX3
ID DIVISION.
PROGRAM-ID. PGM20.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(2).
01 B PIC 9(2).
01 C PIC 9(4).
PROCEDURE DIVISION.
ACCEPT A.
ACCEPT B.
MULTIPLY A BY B GIVING C.
DISPLAY C.
MULTIPLY A BY B.
DISPLAY B.
STOP RUN.
EX4
ID DIVISION.
PROGRAM-ID. PGM21.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(2).
01 B PIC 9(2).
01 C PIC 9(4).
01 D PIC 9(2).
PROCEDURE DIVISION.
ACCEPT A.
ACCEPT B.
DIVIDE A BY B.
DISPLAY B.
DIVIDE A BY B GIVING C REMAINDER D.
DISPLAY C.
DISPLAY D.
DIVIDE A BY B GIVING C.
DIVIDE C.
STOP RUN.
EX5
ID DIVISION.
PROGRAM-ID. PGM21.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(2).
01 B PIC 9(2).
01 C PIC 9(4).
01 D PIC 9(2).
PROCEDURE DIVISION.
ACCEPT A.
ACCEPT B.
DIVIDE A INTO B.
DISPLAY B.
DIVIDE A INTO B GIVING C REMAINDER D.
DISPLAY C.
DISPLAY D.
DIVIDE A INTO B GIVING C.
DIVIDE C.
STOP RUN.
COMPUTE VERB
By using ‘COMPUTE’ we can effectively replace COBOL Arithmetic Verbs
Using ‘COMPUTE’ we can also perform Arithmetic Operations as follows
ADD with +
SUBTACT with –
MULTIPLY with *
DIVIDE with /
COMPUTE C = A + B
COMPUTE C = A – B
COMPUTE C = A * B
COMPUTE C = A / B
EX
ID DIVISION.
PROGRAM-ID. PGM21.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(2).
01 B PIC 9(2).
01 C PIC 9(2).
01 D PIC 9(2).
PROCEDURE DIVISION.
ACCEPT A.
ACCEPT B.
COMPUTE C = A + B.
DISPLAY C.
COMPUTE D = A - B.
DISPLAY D.
INITIALIZE C.
INITIALIZE D.
COMPUTE C = A * B.
DISPLAY C.
COMPUTE D = A / B.
DISPLAY D.
STOP RUN.
ON SIZE ERROR
It is a COBOL verb mainly used in arithmetic operations
While performing an arithmetic operation if the resultant data item is unable
to store the actual result of an arithmetic operation it will print an user
defined message to the output
SYNTAX
ADD D.NAME1 TO D.NAME2 GIVING D.NAME3 ON SIZE
ERROR
DISPLAY “IMPERATIVE STATEMENT”
EX
ID DIVISION.
PROGRAM-ID. PGM1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(2) VALUE ZEROS.
01 B PIC 9(2) VALUE ZEROS.
01 C PIC 9(2) VALUE ZEROS.
PROCEDURE DIVISION.
ACCEPT A.
ACCEPT B.
ADD A TO B GIVING C ON SIZE ERROR
DISPLAY "SIZE ERROR OCCURED".
DISPLAY C.
STOP RUN.
INPUT OUTPUT
10 , 90 ON SIZE
ERROR
ROUNDED
By using ROUNDED we can round the value after decimal point according
to the given picture clause
It is a COBOL VERB and only applicable for arithmetic expressions
EX
ADD A TO B GIVING C ROUNDED
A = 12.3793
B = 13.1073
C = 25.4866
For >5 NEXT
For <=5 Same Value
EX
ID DIVISION.
PROGRAM-ID. PGM24.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(3)V9(4) VALUE 117.3237.
01 B PIC 9(3)V9(4) VALUE 101.3563.
01 C PIC 9(3).9(3).
01 D PIC 9(2).9(2).
01 E PIC 9(2).9(1).
PROCEDURE DIVISION.
ADD A TO B GIVING C ROUNDED.
DISPLAY C.
ADD A TO B GIVING D ROUNDED.
DISPLAY D.
ADD A TO B GIVING E ROUNDED.
DISPLAY E.
ADD A TO B GIVING C.
DISPLAY C.
STOP RUN.
COPY STATEMENT
By using COPY STATEMENT we can invoke the repeated lines of code
kept in a member of PDS file for code reusability
By using COPY STATEMENT we can invoke the members into currently
running COBOL program
The included PDS is called COPY LIBRARY and the members are called
COPY BOOKS
We can invoke any no of COPY BOOKS into currently running program
According to the 1st statement column no in COPY BOOK, we have to write
the COPY STATEMENT in the program
To include the COPY BOOKS into the program we have to write the
following statement while compiling
//COBOL.SYSLIB DD DSN=COPYLIBRARY (COPYBOOK)
The code is written in the members of PDS (MATE02.GRP.PDS)
IN COPY1 (STARTED FROM COLUMN 8)
ID DIVISION.
PROGRAM-ID. PGM24.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(3).
01 B PIC 9(3).
01 C PIC 9(6).
IN COPY2 (STARTED FROM COLUMN12)
COMPUTE C = A + B.
DISPLAY C.
IN COPY3
COMPUTE C = A - B.
DISPLAY C.
IN COPY4
COMPUTE C = A * B.
DISPLAY C.
IN COPY5
COMPUTE C = A / B.
DISPLAY C.
COPY COPY1.
PROCEDURE DIVISION.
ACCEPT A.
ACCEPT B.
COPY COPY4.
STOP RUN.
PARM PARAMETER
By using PARM we can pass MAX 100 characters of data into the COBOL
program
PARM is a variable character which contain both length and text fields
SYNTAX
01 PARM.
02 LENGTH-FIELD PIC S9(VALUE) USAGE COMP.
02 TEXT-FIELD PIC X(VALUE).
EX
ID DIVISION.
PROGRAM-ID. PGM26.
DATA DIVISION.
LINKAGE SECTION.
01 PARM-VAL.
05 P-LEN PIC S9(4) USAGE COMP.
05 P-TXT PIC X(40).
PROCEDURE DIVISION USING PARM-VAL.
DISPLAY P-TXT.
DISPLAY P-LEN.
STOP RUN.
After successful compilation, at run-time give the input
value for PARM as follows
PARM = ‘WELCOME TO PARM’
CORRESPONDING VERB
It is a COBOL reserved word
In CORRESPONDING it moves the data only to the corresponding data
names
In CORRESPONDING we have various types as follows………
1. MOVE CORRESPONDING
2. ADD CORRESPONDING
3. SUBTRACT CORRESPONDING
EX: MOVE CORRESPONDING
ID DIVISION.
PROGRAM-ID. PGM1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 GRP1.
02 A2 PIC A(5) VALUE 'HAI'.
02 A4 PIC 9(3) VALUE 178.
01 GRP2.
02 A1 PIC A(4) VALUE 'BYE'.
02 A2 PIC A(3) VALUE 'HI'.
02 A3 PIC X(3) VALUE 'X24'.
02 A4 PIC 9(5) VALUE 1257.
PROCEDURE DIVISION.
MOVE CORRESPONDING GRP1 TO GRP2.
DISPLAY GRP1.
DISPLAY GRP2.
STOP RUN.
EX: ADD CORRESPONDING
ID DIVISION.
PROGRAM-ID. PGM1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A.
02 X1 PIC 9(3) VALUE 175.
02 X2 PIC 9(3) VALUE 5.
01 B.
02 X3 PIC A(7) VALUE 'WELCOME'.
02 X4 PIC 9(5) VALUE 17573.
02 X1 PIC 9(2) VALUE 17.
PROCEDURE DIVISION.
ADD CORRESPONDING A TO B.
DISPLAY A.
DISPLAY B.
STOP RUN.
EX: SUBTRACT CORRESPONDING
ID DIVISION.
PROGRAM-ID. PGM1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A.
02 X1 PIC 9(3) VALUE 175.
02 X2 PIC 9(3) VALUE 5.
01 B.
02 X3 PIC A(15) VALUE 'WELCOME TO SUBTRACT'.
02 X1 PIC 9(5) VALUE 18175.
02 X2 PIC 9(2) VALUE 3.
PROCEDURE DIVISION.
SUBTRACT CORRESPONDING A FROM B.
DISPLAY A.
DISPLAY B.
STOP RUN.
SEQUENCE CONTROL VERB
By using ‘SEQUENCE CONTROL VERB’ we can overcome the sequential
process of COBOL program
In COBOL we have various ‘SEQUENCE CONTROL VERB’ as
follows…..
1. PERFORM
2. GO TO
3. EVALUATE
4. IF & NESTED IF
5. CONTINUE
6. NEXT SENTENCE
1. PERFORM STATEMENT
Using ‘PERFORM’ statement we can execute any required paragraph
existed in the program
In ‘PERFORM’ after successful execution of the paragraph the control will
be return back to the next statement after ‘PERFORM’
In PERFORM, we have various types as follows……….
1. PERFORM PARNAME
2. PERFORM PARANAME N TIMES
3. PERFORM PARANAME THRU PARANAME
4. PERFORM PARANAME THRU PARANAME N TIMES
5. PERFORM PARANAME UNTIL CONDITION
6. PERFORM PARANAME VARYING UNTIL CONDITION
7. INLINE PERFORM
1. PERFORM PARANAME
Using this possibility we can evaluate any required
paragraph among the paragraph
EX
ID DIVISION.
PROGRAM-ID. PGM30.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(2).
01 B PIC 9(2).
01 C PIC 9(4) VALUE ZEROS.
PROCEDURE DIVISION.
ACCEPT A.
ACCEPT B.
PERFORM ADD-PARA.
PERFORM SUB-PARA.
STOP RUN.
MUL-PARA.
COMPUTE C = A * B.
DISPLAY C.
ADD-PARA.
COMPUTE C = A + B.
DISPLAY C.
DIV-PARA.
COMPUTE C = A / B.
DISPLAY C.
SUB-PARA.
COMPUTE C = A - B.
DISPLAY C.
2. PERFORM PARANAME N TIMES
Based on requirement we can repeat any paragraph for N no of times
EX
ID DIVISION.
PROGRAM-ID. PGM30.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(2).
01 B PIC 9(2).
01 C PIC 9(4) VALUE ZEROS.
PROCEDURE DIVISION.
ACCEPT A.
ACCEPT B.
PERFORM DIV-PARA 2 TIMES.
PERFORM MUL-PARA 3 TIMES.
STOP RUN.
MUL-PARA.
COMPUTE C = A * B.
DISPLAY C.
ADD-PARA.
COMPUTE C = A + B.
DISPLAY C.
DIV-PARA.
COMPUTE C = A / B.
DISPLAY C.
SUB-PARA.
COMPUTE C = A - B.
DISPLAY C.
3. PERFORM PARANAME THRU PARANAME
We can process the group of paragraphs within 1 perform statement as
follows
EX
ID DIVISION.
PROGRAM-ID. PGM30.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(2).
01 B PIC 9(2).
01 C PIC 9(4) VALUE ZEROS.
PROCEDURE DIVISION.
ACCEPT A.
ACCEPT B.
PERFORM ADD-PARA THRU DIV-PARA.
PERFORM MUL-PARA THRU SUB-PARA 2 TIMES.
STOP RUN.
MUL-PARA.
COMPUTE C = A * B.
DISPLAY C.
ADD-PARA.
COMPUTE C = A + B.
DISPLAY C.
DIV-PARA.
COMPUTE C = A / B.
DISPLAY C.
SUB-PARA.
COMPUTE C = A - B.
DISPLAY C.
4. PERFORM PARANAME UNTIL CONDITION
In this based on given condition we can execute the required paragraph
EX
ID DIVISION.
PROGRAM-ID. PGM30.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(1) VALUE 1.
PROCEDURE DIVISION.
PERFORM UNTIL-PARA UNTIL A>3.
STOP RUN.
UNTIL-PARA.
DISPLAY ‘WELCOME TO PARA’.
COMPUTE A = A+1.
5. PERFORM VARYING CONDITION
Using varying we can repeat the para for n = no of times sequentially
EX1
ID DIVISION.
PROGRAM-ID. PGM30.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 I PIC 9(1).
PROCEDURE DIVISION.
PERFORM VARY-PARA VARYING I FROM 1 BY 1 UNTIL I>3.
DISPLAY ‘VARYING SEQUENTIALLY’
STOP RUN.
VARY-PARA.
DISPLAY ‘WELCOME TO VARYING’.
EX2
ID DIVISION.
PROGRAM-ID. PGM30.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 I PIC 9(1).
PROCEDURE DIVISION.
PERFORM VARY-PARA VARYING I FROM 3 BY -1 UNTIL I>3.
DISPLAY ‘VARYING SEQUENTIALLY’
STOP RUN.
VARY-PARA.
DISPLAY ‘WELCOME TO VARYING’.
CASE STUDY
GENERATING THE TABLES USING COBOL
By using PERFORM STATEMENT in COBOL we can generate the
tables as follows
EX
ID DIVISION.
PROGRAM-ID. PGM30.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(2).
01 B PIC 9(2).
01 C PIC 9(4) VALUE ZEROS.
PROCEDURE DIVISION.
ACCEPT A.
ACCEPT B.
PERFORM TAB-PARA VARYING A FROM 1 BY 1 UNTIL A>20
AFTER B FROM 1 BY 1 UNTIL B>20.
DISPLAY ’20 * 20 TABLES GENERATED’
STOP RUN.
TAB-PARA.
COMPUTE C = A * B.
DISPLAY A ‘*’ B ‘=’ C
6. INLINE PERFORM
By using INLINE PERFORM we can process the list of statements as
follows
For INLINE PERFORM ‘END-PERFORM’ is mandatory
EX
ID DIVISION.
PROGRAM-ID. PGM1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(2) VALUE 08.
01 B PIC 9(2) VALUE 09.
01 C PIC 9(2).
PROCEDURE DIVISION.
PERFORM
ADD A TO B GIVING C
DISPLAY C
SUBTRACT B FROM A GIVING C
DISPLAY C
MULTIPLY A BY B GIVING C
DISPLAY C
END-PERFORM.
STOP RUN.
2. GO TO
By using ‘GO TO’ we can transfer the control permanently from one place
to another place within a program
‘GO TO’ permanently transfers the control, we cant expect cursor back
Compare to PERFORM, GO TO is having more logical errors
EX
ID DIVISION.
PROGRAM-ID. PGM36.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(4) VALUE 0072.
01 B PIC 9(4) VALUE 0082.
01 C PIC 9(4).
PROCEDURE DIVISION.
PERFORM ADD-PARA THRU DIV-PARA.
GO TO SUB-PARA.
PERFORM MUL-PARA.
STOP RUN.
MUL-PARA.
COMPUTE C = A * B.
DISPLAY C.
ADD-PARA.
COMPUTE C = A + B.
DISPLAY C.
DIV-PARA.
COMPUTE C = A / B.
DISPLAY C.
SUB-PARA.
COMPUTE C = A - B.
DISPLAY C.
EX
ID DIVISION.
PROGRAM-ID. PGM36.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(4) VALUE 0072.
01 B PIC 9(4) VALUE 0082.
01 C PIC 9(4).
PROCEDURE DIVISION.
PERFORM ADD-PARA THRU DIV-PARA.
GO TO SUB-PARA.
PERFORM MUL-PARA.
MUL-PARA.
COMPUTE C = A * B.
DISPLAY C.
ADD-PARA.
COMPUTE C = A + B.
DISPLAY C.
DIV-PARA.
COMPUTE C = A / B.
DISPLAY C.
SUB-PARA.
COMPUTE C = A - B.
DISPLAY C.
STOP RUN.
While using GO TO we always place STOP RUN at-last
only
IF & NESTED IF
Using IF we can also PERFORM condition checking
‘IF AND ELSE’ are used for EVALUATING the given data
SYNTAX
IF <COND>
DISPLAY ‘IMPERATIVE STATEMENT’
ELSE
DISPLAY ‘IMPERATIVE STATEMENT’
END-IF.
EX
ID DIVISION.
PROGRAM-ID. PGM38.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(3) VALUE 127.
01 B PIC 9(3) VALUE 137.
PROCEDURE DIVISION.
IF A>B
DISPLAY 'A IS BIGGER'.
ELSE
DISPLAY 'B IS BIGGER'.
END-IF.
STOP RUN.
EVALUATE
Using EVALUATE we can evaluate the case structure
Using EVALUATE we can effectively replace ‘NESTED-IF’ statement
SYNTAX
EVALUATE TRUE/FALSE
WHEN COND1
DISPLAY ‘IMPERATIVE STATEMENT’
WHEN COND2
DISPLAY ‘IMPERATIVE STATEMENT’
………
………
………
………
……….
WHEN CONDn
DISPLAY ‘IMPERATIVE STATEMENT’
WHEN OTHER
DISPLAY ‘IMPERATIVE STATEMENT’
END-EVALUATE.
EX
ID DIVISION.
PROGRAM-ID. PGM39.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 MONTH PIC 99.
01 MONTHNAME PIC A(15).
01 DAYS PIC 99.
PROCEDURE DIVISION.
DISPLAY “ENTER THE MONTH NUMBER”.
ACCEPT MONTH.
DISPLAY MONTH.
DISPLAY MONTHNAME.
MONTH-PARA
EVALUATE MONTH
WHEN 1 MOVE 'JANUARY' TO MONTHNAME
WHEN 2 MOVE 'FEBRUARY' TO MONTHNAME
WHEN 3 MOVE 'MARCH' TO MONTHNAME
WHEN 4 MOVE 'APRIL' TO MONTHNAME
WHEN 5 MOVE 'MAY' TO MONTHNAME
WHEN 6 MOVE 'JUNE' TO MONTHNAME
WHEN 7 MOVE 'JULY' TO MONTHNAME
WHEN 8 MOVE 'AUGUST' TO MONTHNAME
WHEN 9 MOVE 'SEPTEMBER' TO MONTHNAME
WHEN 10 MOVE 'OCTOBER' TO MONTHNAME
WHEN 11 MOVE 'NOVEMBER’ TO MONTHNAME
WHEN 12 MOVE 'DECEMBER' TO MONTHNAME
END-EVALUATE.
DAY-PARA
MOVE ‘DDDDDDDD’ TO MONTHNAME.
DISPLAY MONTHNAME.
EVALUATE TRUE
WHEN MONTH = 4 OR 6 OR 9 OR 11
MOVE 30 TO DAYS
WHEN MONTH = 2
MOVE 28 TO DAYS
WHEN OTHER MOVE 31 TO DAYS
END-EVALUATE.
STOP RUN.
CONTINUE AND NEXT SENTENCE
CONTINUE will transfer the control to the next statement after an explicit
scope terminator whereas NEXT SENTENCE will transfer control to the
next sentence after next period
These two are mainly used for navigating within a program
IF A>B
CONTINUE/NEXT SENTENCE
IF B>C
DISPLAY ‘B IS BIGGER’
ELSE
DISPLAY ‘C IS BIG’
END-IF
DISPLAY “HELLO”. Continue
DISPLAY “HI”. Next sentence
CONTINUE is like a NULL STATEMENT which does not do anything
where as NEXT SENTENCE will bypass the statements from execution
STRING HANDLING
String is a collection of one or more characters
Using String Handling we can combine one or more strings into one string
We can divide one string into no of strings
We can replace the existing characters with new characters
We can count the occurrences of the characters in the given string
We can convert the data into new data
In string we have different functions as follows. They are………
1. String
2. Unstring
3. Inspect
4. Tallying
5. Replacing
6. Converting
1. STRING
Using string we can combine one or more strings into one string
SYNTAX
STRING ID1 DELIMITED BY ‘COMMA/SPACE/SIZE’,
ID2 DELIMITED BY ‘COMMA/SPACE/SIZE’,
ID3 DELIMITED BY ‘COMMA/SPACE/SIZE’,
………………..
………………..
………………..
………………..
IDn DELIMITED BY ‘COMMA/SPACE/SIZE’, INTO
IDENTIFIER
END-STRING.
EX
ID DIVISION.
PROGRAM-ID. PGM40.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC A(10) VALUE 'HELLO HAI'.
01 B PIC X(13) VALUE 'WELCOME#BYE'.
01 C PIC A(20) VALUE SPACES.
PROCEDURE DIVISION.
STRING A DELIMITED BY ' ',
B DELIMITED BY '#' INTO C.
DISPLAY C.
STOP RUN.
2. UNSTRING
Using UNSTRING we can divide one string into no of strings as
follows
SYNTAX
UNSTRING ID1 INTO ID2,ID3,ID4………IDn
END-STRING.
EX
ID DIVISION.
PROGRAM-ID. PGM41.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC A(30) VALUE 'HELLO HAI WELCOME TO STRING'.
01 B PIC A(5).
01 C PIC A(3).
01 D PIC A(10).
01 E PIC A(6).
PROCEDURE DIVISION.
UNSTRING A INTO B,C,D,E.
DISPLAY A.
DISPLAY B.
DISPLAY C.
DISPLAY D.
DISPLAY E.
STOP RUN.
INSPECT, TALLYING
Using ‘INSPECT’ we can examine the string
Using ‘TALLYING’ we can update the count and also used for counting the
occurrences
Using ‘INSPECT’ we can count the occurrence of a character in the given
string as follows
EX
ID DIVISION.
PROGRAM-ID. PGM42.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC A(30) VALUE 'HELLO HAI WELCOME TO INSEPECT'.
01 B PIC 9(4).
PROCEDURE DIVISION.
INSPECT A TALLYING B FOR ALL 'E'.
DISPLAY A.
DISPLAY 'COUNT IS::' B.
STOP RUN.
REPLACING
Using REPLACING we can change character by character or word by word
While REPLACING, the old and new characters length must be same
Using REPLACE we can also NULLIFY the data
EX
ID DIVISION.
PROGRAM-ID. PGM43.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC A(30) VALUE 'HELLO HI HAI HI'.
PROCEDURE DIVISION.
INSPECT A REPLACING ALL 'H' BY 'A'.
DISPLAY A.
INSPECT A REPLACING ALL 'HI' BY 'H!'
DISPLAY A.
STOP RUN.
CONVERTING
Using CONVERTING we can convert character by character and also
we can convert multiple characters with new characters at a time
EX
ID DIVISION.
PROGRAM-ID. PGM44.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC A(20) VALUE 'HELLO HI HAI HI'.
PROCEDURE DIVISION.
INSPECT A CONVERTING 'H' TO 'A'.
DISPLAY A.
INSPECT A CONVERTING 'HI' TO 'X!'.
DISPLAY A.
INSPECT A CONVERTING 'HIL' TO '---'.
DISPLAY A.
STOP RUN.
REFERENCE MODIFICATION
In String Handling by using REFERENCE MODIFICATION we can
retrieve required string from the given string
Using REFERENCE MODIFICATION random selection of data is possible
SYNTAX
DATANAME(LOCATIONINPUT:LENGTH)
EX
ID DIVISION.
PROGRAM-ID. PGM45.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC A(30) VALUE 'HELLO HOW ARE YOU'.
01 B PIC A(3).
01 C PIC A(3).
01 D PIC A(5).
01 E PIC A(3).
PROCEDURE DIVISION.
MOVE A(11:3) TO B.
MOVE A(7:3) TO C.
MOVE A(1:5) TO D.
MOVE A(15:3) TO E.
DISPLAY A.
DISPLAY B.
DISPLAY C.
DISPLAY D.
DISPLAY E.
STOP RUN.
TABLE HANDLING
TABLE is a linear data structure
TABLE stores the homogenous data items
Using TABLE we can write or invoke collection of linear data structures
(data items) with one name called TABLE
TABLE is a collection of ROWS and COLUMNS
Using TABLE HANDLING we can repeat one data item for ‘n’ no of times
In TABLE we can repeat the data names for ‘n’ no of times using OCCURS
CLAUSE
By using OCCURS we can repeat the structure for ‘n’ times and it allocates
the memory for the data names
01 GRP
02 EMP OCCURS 2 TIMES
03 ENO 9(4). 20*2 = 40 Bytes
03 ENAME A(16).
RULES IN OCCURS CLAUSE
We cant use OCCURS CLAUSE for group item (01)
Only positive occurrences are allowed
OCCURS is a WORKING-STORAGE SECTION item
We also cant write OCCURS for ‘66’,’77’ &’88’ level no#
By using SUBSCRIPT and INDEX only we can change the value of an array
element
SUBSCRIPT means occurrences whereas INDEX means displacement
The value of INDEX will be set by using a SET statement
DIFFERENCES BETWEEN SUBSCRIPT AND INDEX
SUBSCRIPT INDEX
SUBSCRIPT is a working- INDEX is an internal item
storage section item INDEX means
SUBSCRIPT means displacement
occurrences INDEX is faster than
SUBSCRIPT is slower than SUBSCRIPT
INDEX INDEX can be
SUBSCRIPT can be implemented in arithmetic
implemented in arithmetic operations
operations
In general COBOL we can write max ‘7’ dimensions
DIMENSION = No of Occurrences
We can also write multiple dimensions
The declaration of array elements and the values allocation is as follows….
EX1. ONE DIMENSIONAL ARRAY
ID DIVISION.
PROGRAM-ID. PGM1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 EMP-TAB.
02 EMP OCCURS 3 TIMES.
03 ENO PIC 9(4).
03 ENAME PIC A(15).
77 I PIC 9(1).
PROCEDURE DIVISION.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 3
ACCEPT ENO(I)
ACCEPT ENAME(I)
DISPLAY ENO(I)
DISPLAY ENAME(I)
END-PERFORM.
DISPLAY EMP-TAB.
STOP RUN.
EX1. TWO DIMENSIONAL ARRAY
ID DIVISION.
PROGRAM-ID. PGM2.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CLASS-TAB.
02 SEC-TAB OCCURS 2 TIMES.
03 SEC PIC X(5)
03 NAME-TAB OCCURS 5 TIMES.
04 NAME PIC A(20).
77 I PIC 9(1).
77 J PIC 9(1).
PROCEDURE DIVISION.
PERFORM VARYING I FROM 1 BY 1 UNTIL I>2
ACCEPT SEC(I)
DISPLAY SEC(I)
PERFORM VARYING J FROM 1 BY 1 UNTIL J>5
ACCEPT NAME(I,J)
DISPLAY NAME(I,J)
END-PERFORM.
END-PERFORM.
DISPLAY CLASS-TAB.
STOP RUN.
EX1. TWO DIMENSIONAL ARRAY
ID DIVISION.
PROGRAM-ID. PGM3.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CLASS-TAB.
02 SECTION-TAB OCCURS 2 TIMES.
03 SEC PIC X(5).
03 NAME-TAB OCCURS 3 TIMES.
04 NAME-TAB PIC A(20).
04 MARK-TAB OCCURS 6 TIMES.
05 MARK PIC 9(3).
77 I PIC 9(1).
77 J PIC 9(1).
77 K PIC 9(1).
PROCEDURE DIVISION.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 2
ACCEPT SEC(I) 00160004
DISPLAY SEC(I) 00170004
PERFORM VARYING J FROM 1 BY 1 UNTIL J > 5
ACCEPT SEC(I,J)
DISPLAY SEC(I,J)
PERFORM VARYING K FROM 1 BY 1 UNTIL K > 6
ACCEPT SEC(I,J,K)
DISPLAY SEC(I,J,K)
END-PERFORM.
END-PERFORM.
END-PERFORM.
DISPLAY CLASS-TAB.
STOP RUN.
SEARCH AND SEARCHALL
We can search any element of an array by using SEARCH and
SEARCHALL
SEARCH is a sequential search whereas SEARCHALL is a binary search
SEARCH is preferable for small tables whereas SEARCHALL is preferable
for large tables
In SEARCH and SEARCHALL, INDEX is required
In SEARCH any logical comparison is possible
SEARCHALL will work faster than SEARCH because of their order or
sequence
SYNTAX1. SEARCH
SEARCH IDENTIFIER AT END DISPLAY ‘IMPERATIVE
STATEMENT’
WHEN DATANAME = SEARCH ELEMENT
DISPLAY ‘IMPERATIVE STATEMENT’
END-SEARCH
SYNTAX2. SEARCHALL
SEARCHALL IDENTIFIER AT END DISPLAY ‘IMPERATIVE
STATEMENT’
WHEN DATANAME = SEARCH ELEMENT
DISPLAY ‘IMPERATIVE STATEMENT’
END-SEARCHALL
EX1. SEARCH
ID DIVISION.
PROGRAM-ID. PGM1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 EMP-TAB.
02 EMP OCCURS 3 TIMES INDEXED BY I.
03 ENO PIC 9(4).
03 ENAME PIC A(15).
77 SNUM PIC 9(4).
PROCEDURE DIVISION.
PERFORM PARA-1 3 TIMES.
ACCEPT SNUM.
PERFORM SER-PARA.
STOP RUN.
PARA-1
ACCEPT ENO(I).
ACCEPT ENAME(I).
SER-PARA
SEARCH EMP AT END MOVE “ELEMENT NOT FOUND”
WHEN ENO(I)=SNUM
DISPLAY ENO(I)
DISPLAY ENAME(I)
END-SEARCH.
In SEARCH we can keep the input data in any order
EX2. SEARCHALL
ID DIVISION.
PROGRAM-ID. PGM1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 EMP-TAB.
02 EMP OCCURS 3 TIMES INDEXED BY I ASCENDING KEY ENO.
03 ENO PIC 9(4).
03 ENAME PIC A(15).
77 SNUM PIC 9(4).
PROCEDURE DIVISION.
PERFORM PARA-1 3 TIMES.
ACCEPT SNUM.
PERFORM SER-PARA.
STOP RUN.
PARA-1
ACCEPT ENO(I).
ACCEPT ENAME(I).
SER-PARA
SEARCHALL EMP AT END MOVE “ELEMENT NOT FOUND”
WHEN ENO(I)=SNUM
DISPLAY ENO(I)
DISPLAY ENAME(I)
END-SEARCH.
FILE HANDLING
FILE is a collection of records
In COBOL we use both VSAM and NON-VSAM files for storing data
DATA FILE is a collection of physical and logical files
Physical file is identified by system whereas the Logical file will be
identified by the program
In COBOL we use physical sequential (PS) and VSAM CLUSTERS for
loading data
In COBOL to use a file we need to follow the structure as follows
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT <LOGICAL FILE> ASSIGN TO <DD NAME>.
ORGANIZATION <SEQUENTIAL> OR <INDEXED> OR
<RELATIVE>.
ACCESS MODE <SEQUENTIAL> OR <RANDOM> OR
<DYNAMIC>.
RECORD KEY <KEY NAME>.
RELATIVE KEY <KEY NAME>.
ALTERNATE RECORD KEY <KEY NAME>.
FILE STATUS <FS NAME>.
DESCRIPTION
Using SELECT we can assign a logical file for the DD NAME
LOGICAL FILE is a user defined name
SELECT will access the physical file through DD NAME
1. ORGANIZATION
Specifies the behavior of and structure of the file depends on file type we
can assign organization for the file
1. SEQUENTIAL is used for PS and ESDS also
2. INDEXED is used for KSDS
3. RELATIVE is used for RRDS
NOTE
ORGANIZATION, SEQUENTIAL is default
2. ACCESS MODE
Specifies how the file is going to be accessed
a. SEQUENTIAL
Data can be retrieved in entry sequence
b. RANDOM
we can retrieve the data randomly from the specific location in the file
c. DYNAMIC
It is combination of both SEQUENTIAL and RANDOM
By using START and READ NEXT we can access the records from the file
DYNAMIC is mainly used for KSDS cluster
3. RECORD KEY
Specifies primary key of KSDS cluster
4. RELATIVE KEY
Is used for RRDS
5. ALTERNATE RECORD KEY
Using ALTERNATE INDEX also we can retrieve data from clusters
(KSDS & ESDS)
6. FILE STATUS
It is a two digit no mainly used to know the status of a file
It is not mandatory for every file
NOTE
‘ACCESS MODE’ SEQUENTIAL is default
File is an 80 byte record structure
While declaring file we need to use FILLER to initialize the unused space
with ‘SPACES’
FILLER is also used for flexible insertion and updating of data
USAGE OF FILES
Declare a logical file in COBOL
Declare file attributes (usage of 80 bytes)
Opening the file
Performing required operation
Closing the file
FILE OPEN MODES
We can open file according to the user requirement
In file we have 4 modes. They are
1. Input
2. Output
3. Extend
4. Input-output
1. INPUT
Used for the existing files
Only READ operation is allowed
2. OUTPUT
Used to insert records
If a SEQUENTIAL file already holding record is used then the existing
records will be deleted 1st and then the new records are inserted into the file
It will not happen in case of INDEXED or RELATIVE file
3. EXTEND
Used to append records in a SEQUENTIAL file
The records are inserted at the end
If the ACCESS MODE is RANDOM or DYNAMIC then EXTEND mode
cannot be used
4. INPUT-OUTPUT
Used to read and rewrite the records of a file
On files we can perform many operations like.……..
1. OPEN
2. READ
3. WRITE
4. REWRITE
5. DELETE
6. START
7. CLOSE
1. OPEN
1st file operation that must be performed
If open is successful then only further operations are possible on a file
Only after opening a file, the variables in the file structure are available for
processing
FILE STATUS is updated after each and every file operation
SYNTAX
OPEN “MODE” FILENAME
Open input bi001-infile.
2. READ
Used to fetch the records from a file
To perform READ operation the file must be opened in INPUT or INPUT-
OUTPUT mode
SYNTAX
READ FILENAME NEXT RECORD INTO WS-FILE-
STRUCTURE
AT END DISPLAY ‘END OF FILE’
NOT END DISPLAY ‘RECORD DETAILS::’ WS-FILE-
STRUCTURE
END-READ.
3. WRITE
Used to insert the records into the file
Once the record is written it is no longer available in record buffer
Before inserting the records in the file, move values to the record buffer and
then perform WRITE verb
Open mode should be EXTEND or OUTPUT, if ACCESS MODE is
RANDOM or DYNAMIC then Open mode should be OUTPUT or INPUT-
OUTPUT
SYNTAX
WRITE RECORD BUFFER (FROM WS-FILE-STRUCTURE)
INVALID KEY DISPLAY ‘INVALID KEY’
NOT INVALID KEY DISPLAY ‘RECORD INSERTED’
END-WRITE.
4. REWRITE
Open mode should be INPUT-OUTPUT
Used to update records
Last record is overrided
SYNTAX
REWRITE RECORD BUFFER (FROM WS-FILE-STRUCTURE)
INVALID KEY DISPLAY ‘INVALID KEY’
NOT INVALID KEY DISPLAY ‘RECORD INSERTED’
END-REWRITE.
5. DELETE
Open mode should be INPUT-OUTPUT
If the ACCESS MODE is SEQUENTIAL, then the last record read by
READ statement is deleted. If the ACCESS MODE is RANDOM then
specify the RECORD KEY and then perform delete operation
SYNTAX
DELETE FILENAME RECORD
INVALID KEY DISPLAY ‘INVALID KEY’
NOT INVALID KEY DISPLAY ‘RECORD DELETED’
END-DELETE.
6. START
Open mode should be INPUT-OUTPUT
START is performed on files those ACCESS MODES are SEQUENTIAL
and RANDOM
Used to place the file pointer on specific record
SYNTAX
START FILENAME KEY IS (=, <, >, !=, >=, <=) REC-KEY
INVALID KEY DISPLAY ‘INVALID KEY’
NOT INVALID KEY DISPLAY ‘RECORD INSERTED’
END-START.
7. CLOSE
Used to close the opened file
SYNTAX
CLOSE FILENAME
EX1: WRITING DATA INTO FILE
ID DIVISION.
PROGRAM-ID. PGM4.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE1 ASSIGN TO DD1
ORGANIZATION SEQUENTIAL
ACCESS MODE SEQUENTIAL
FILE STATUS FS1.
DATA DIVISION.
FILE SECTION.
FD FILE1.
01 FILE-REC.
49 ENO PIC 9(4).
49 ENAME PIC A(16).
49 ESAL PIC 9(5).
49 FILLER PIC X(55).
WORKING-STORAGE SECTION.
77 FS1 PIC 9(2).
PROCEDURE DIVISION.
PERFORM OPEN-PARA.
PERFORM WRITE-PARA.
PERFORM CLOSE-PARA.
STOP RUN.
OPEN-PARA.
OPEN OUTPUT FILE1.
DISPLAY FS1.
WRITE-PARA.
ACCEPT ENO.
ACCEPT ENAME.
ACCEPT ESAL.
WRITE FILE-REC.
DISPLAY FS1.
CLOSE-PARA.
CLOSE FILE1.
DISPLAY FS1.
FD – specifies the File Descriptions used to declare file attributes
SD(Sort Descriptor) – it is used for doing SORT and MERGE operations on
files
EX2: WRITING DATA INTO FILE USING FILLER
ID DIVISION.
PROGRAM-ID. PGM4.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE1 ASSIGN TO DD1
ORGANIZATION SEQUENTIAL
ACCESS MODE SEQUENTIAL
FILE STATUS FS1.
DATA DIVISION.
FILE SECTION.
FD FILE1.
01 FILE-REC.
49 ENO PIC 9(4).
49 FILLER X(2).
49 ENAME PIC A(16).
49 FILLER X(3).
49 ESAL PIC 9(5).
49 FILLER PIC X(50).
WORKING-STORAGE SECTION.
77 FS1 PIC 9(2).
PROCEDURE DIVISION.
PERFORM OPEN-PARA.
PERFORM WRITE-PARA.
PERFORM CLOSE-PARA.
STOP RUN.
OPEN-PARA.
OPEN OUTPUT FILE1.
DISPLAY FS1.
WRITE-PARA.
ACCEPT ENO.
ACCEPT ENAME.
ACCEPT ESAL.
WRITE FILE-REC.
DISPLAY FS1.
CLOSE-PARA.
CLOSE FILE1.
DISPLAY FS1.
Using FILE STATUS we can know the file status of the file. If operation
is successful it gives ‘00’ otherwise more than ‘00’
EX3: WRITING NO OF RECORDS INTO FILE
ID DIVISION.
PROGRAM-ID. PGM4.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE1 ASSIGN TO DD1
ORGANIZATION SEQUENTIAL
ACCESS MODE SEQUENTIAL
FILE STATUS FS1.
DATA DIVISION.
FILE SECTION.
FD FILE1.
01 FILE-REC.
49 ENO PIC 9(4).
49 ENAME PIC A(16).
49 ESAL PIC 9(5).
49 FILLER PIC X(55).
WORKING-STORAGE SECTION.
77 FS1 PIC 9(2).
PROCEDURE DIVISION.
PERFORM OPEN-PARA.
PERFORM WRITE-PARA 4 TIMES.
PERFORM CLOSE-PARA.
STOP RUN.
OPEN-PARA.
OPEN OUTPUT FILE1.
DISPLAY FS1.
WRITE-PARA.
ACCEPT ENO.
ACCEPT ENAME.
ACCEPT ESAL.
WRITE FILE-REC.
DISPLAY FS1.
CLOSE-PARA.
CLOSE FILE1.
DISPLAY FS1.
EX4. READING DATA FROM THE FILE
ID DIVISION.
PROGRAM-ID. PGM4.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE4 ASSIGN TO DD1
ORGANIZATION SEQUENTIAL
ACCESS MODE SEQUENTIAL
FILE STATUS FS1.
DATA DIVISION.
FILE SECTION.
FD FILE4.
01 FILE-REC.
49 ENO PIC 9(4).
49 ENAME PIC A(16).
49 ESAL PIC 9(5).
49 FILLER PIC X(55).
WORKING-STORAGE SECTION.
77 FS1 PIC 9(2).
PROCEDURE DIVISION.
PERFORM OPEN-PARA.
PERFORM READ-PARA.
PERFORM CLOSE-PARA.
STOP RUN.
OPEN-PARA.
OPEN OUTPUT FILE1.
DISPLAY FS1.
READ-PARA.
READ FILE4.
DISPLAY FS1.
CLOSE-PARA.
CLOSE FILE1.
DISPLAY FS1.
CASE1.
If suppose file having 100 records we can fetch 100 at a time as
PERFORM READ-PARA 100 TIMES
CASE2.
If suppose file having many records we can fetch all at a time by using
following example
ID DIVISION.
PROGRAM-ID. PGM4.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE5 ASSIGN TO DD1
ORGANIZATION SEQUENTIAL
ACCESS MODE SEQUENTIAL
FILE STATUS FS1.
SELECT FILE6 ASSIGN TO DD2
ORGANIZATION SEQUENTIAL
ACCESS MODE SEQUENTIAL
FILE STATUS FS2.
DATA DIVISION.
FILE SECTION.
FD FILE5.
01 FILE-REC.
49 ENO PIC 9(4).
49 ENAME PIC A(16).
49 ESAL PIC 9(5).
49 FILLER PIC X(55).
FD FILE6.
01 FILE-REC1.
49 ENO PIC 9(4).
49 ENAME PIC A(16).
49 ESAL PIC 9(5).
49 FILLER PIC X(55).
WORKING-STORAGE SECTION.
77 FS1 PIC 9(2).
77 FS2 PIC 9(2).
77 VAL PIC X(1).
PROCEDURE DIVISION.
PERFORM OPEN-PARA.
PERFORM READ-PARA UNTIL VAL=’N’.
PERFORM CLOSE-PARA.
STOP RUN.
OPEN-PARA.
OPEN INPUT FILE5.
DISPLAY FS1.
OPEN INPUT FILE6.
DISPLAY FS2.
READ-PARA.
READ FILE5 AT END MOVE ‘N’ TO VAL.
NOT AT END PERFORM WRITE-PARA.
DISPLAY FS1.
WRITE-PARA.
MOVE FILE-REC TO FILE-REC1.
WRITE FILE-REC1.
DISPLAY FS2.
CLOSE-PARA.
CLOSE FILE5.
DISPLAY FS1.
CLOSE FILE6.
DISPLAY FS2.