Documente Academic
Documente Profesional
Documente Cultură
Table of Contents
UNIT 1. Introduction to Language Features
COMMON BUSINESS ORIENTED LANGUAGE
COBOL PROGRAM ORGANIZATION
COBOL LANGUAGE STRUCTURE
STRUCTURE OF COBOL PROGRAM
CHARACTER SET OF COBOL
SAMPLE COBOL PROGRAM
CODING FORMAT
USER-DEFINED WORDS
UNIT 2. The Organization of a COBOL Program
IDENTIFICATION DIVISION
ENVIRONMENT DIVISION
DATA DIVISION
DATA-ITEMS
LEVEL NUMBERS
SPECIAL LEVEL NUMBERS
W-S DECLARATIONS
FILLER
PICTURE CLAUSE
USAGE CLAUSE
VALUE CLAUSE
REDEFINES CLAUSE
DUPLICATE DATA NAMES
RENAMES CLAUSE
FIGURATIVE CONSTANTS
EDITED FIELDS
MORE EDITING CHARACTER
EXAMPLES
UNIT 3. PROCEDURE DIVISION
PROCEDURE DIVISION
COBOL VERBS
PARAGRAPHS
TERMINATOR STATEMENTS
SCOPE TERMINATORS
DISPLAY VERB
ACCEPT VERB
MOVE VERB
ELEMENTARY & GROUP MOVES
CORRESPONDING PHASE
REFERENCE MODIFICATION
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
ADD VERB
ADD CORRESPONDING STATEMENT
ON SIZE ERROR PHRASE
NUMERIC DATA
SUBTRACT VERB
SUBTRACT CORRESPONDING STATEMENT
MULTIPLY VERB
DIVIDE VERB
COMPUTE STATEMENT
PERFORM STATEMENT
PERFORM THROUGH
PERFORMN TIMES
PERFORMVARYING
IN-LINE PERFORM
RELATIONAL EXPRESSIONS
IF STATEMENT
COMPOUND CONDITIONALS
CLASS CONDITION
CONTINUE & NEXT SENTENCE STATEMENT
EVALUATE STATEMENT
SET TO TRUE
INITIALIZE
UNIT 4. FILE HANDLING IN COBOL
FILES
FIXED VS VARIABLE LENGTH RECORDS
FILE-CONTROL PARAGRAPH
ACCESS MODE
FILE STATUS CLAUSE
I-O CONTROL PARAGRAPH
FILE SECTION
FILE OPERATIONS
OPEN MODES
READ-SEQUENTIAL ACCESS
END OF FILE PROCESSING
READ RANDOM ACCESS
READ DYNAMIC ACCESS
START STATEMENT
WRITE STATEMENT
WRITEFROM
READINTO
REWRITE & DELETE
APPENDING TO SEQUENTIAL FILES
FILE COMPARISON
CLOSE STATEMENT
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
SEQUENTIAL FILES
INDEXED FILES
INVALID KEY
ACCESS MODE: SEQUENTIAL & RANDOM
ACCESS MODE: DYNAMIC
RELATIVE FILES
UNIT 5. TABLE HANDLING
INTRODUCTION: TABLE HANDLING
OCCURS CLAUSE
SUBSCRIPT
INDEXING
ONE DIMENSIONAL TABLE
TWO DIMENSIONAL TABLE
MULTIDIMENTIONAL TABLE
TABLE-SORTING
SET
SEARCH
BINARY SEARCH
UNIT 6. Library Services
COPY STATEMENT
NESTED COPY
COPY REPLACING
COPY PSEUDO-TEST
REPLACE PSEUDO-TEST
UNIT 7. CHARACTER HANDLING
STRING
UNSTRING STATEMENT
EXAMINE STATEMENT
INSPECT TALLYING STATEMENT
INSPECT REPLACING STATEMENT
UNIT 8. SORT / MERGE
SORT/MERGE
SORT STATEMENT
MERGE STATEMENT
SORT PROCEDURES
RELEASE STATEMENT
RETURN STATEMENT
Cobol Lab
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
UNIT 1
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Notes:
To meet the increasing demands for a high level language suitable for business
data processing, the United States Department of Defense Convened a
Conference on 28th and 29th of May 1958.
Three committee were formed for the actual design of the language.
In September 1959 the short term committee submitted a report to the Defense
Directorate thus COBOL came into existence.
COBOL is known as a structured programming language because it allows
programmers to segregate the modules and put them into different paragraphs in
a more efficient way.
Some of the features of COBOL are
It is English-like and more easily readable
Efficient file handling capabilities.
More than 70% of business applications are running on COBOL
Reduces the efforts required for documentation of the program.
The following features are available with VS COBOL II:
2
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
IDENTIFICATION DIVISION
ENVIRONMENT DIVISION
DATA DIVISION
PROCEDURE DIVISION
Every cobol program must have these divisions.
Notes:
The four divisions of a COBOL source program are :
IDENTIFICATION DIVISION
The primary purpose of these program is to name the program.
ENVIRONMENT DIVISION
This division is primarily used to tell the computer about the input and
output devices such as files or printers.
DATA DIVISION
4
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
The division is used to define and describe the data items and being used
in the program. Data items and data names refer to some storage space in
memory to store data. Here you would distinguish between data, which
will be used for a scratch pad area called WORKING-STORAGE and
the holding area for data that will be used by the files.
PROCEDURE DIVISION
The PROCEDURE DIVISION is the section of our program where the
logic or commands reside. This is also the place logic or rules we will use
to manipulate the data defined in the DATA DIVISION to solve a
business problem.
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Notes:
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
DATA DIVISION
Sections or Paragraphs
PROGRAM-ID
FILE SECTION, 100-PARA
Statements
MOVE A TO B
Sentences
Notes:
All COBOL programs should follow the structure. Rules of coding varies,
depending on the compiler versions but the structure remains same. A period (.)
is a must at the end of each sentence and indicates the end of the sentence.
A typical program could contain divisions, sections or paragraphs within
divisions, and statements within sections or paragraphs. There are both system
and user defined sections and paragraphs.
Eg:
PROCEDURE DIVISION.
ADD-PARA.
ADD A,B GIVING C.
SUB-PARA.
Where A,B and C are dada items defined in the data divisions.
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Alphabets
Spaces or blanks
Arithmetic operators
ex: **, *, +, -, /
Special characters
ex: - \ / , ;
Notes:
The character 0-9 are called numeric characters or digits.
The characters A-Z are called letters and remaining are called special characters.
The COBOL dictionary words used for coding are called COBOL reserved words
and they should not be used as user-defined words.
Lower case alphabets can be used for coding depending on the compiler version.
Comma (,) or space is used as separators for user-defined words.
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Columns
1
67 8
11 12
72 73
80
IDENTIFICATION DIVISION.
PROGRAM-ID. SAMPLE.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 9(2) VALUE 20.
01 B PIC 9(2) VALUE 3O.
01 C PIC 9(3) VALUE ZEROS.
PROCEDURE DIVISION.
DISPLAY THE SUM IS.
ADD A ,B GIVING C.
DISPLAY C.
STOP RUN.
Notes:
1-6
7
8-11
12-72
73-80
This foil shows a sample COBOL program to ADD two numbers and
DISPLAY the sum. SAMPLE is the program name.
SAMPLE, A, B AND C are called user-defined words.
A, B,C are called variables or data-items.
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Coding Format
01 06
Sequence
07
Indicator
08 11
Area A
12 72
Area B
73 80
Description
Notes:
COBOL coding should follow the standard format.
The Screen is divided into different areas for the purposes explained above.
All statements indicating action are called COBOL verbs and should begin from
12th column or after.
-E.g
10
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
User-defined Words
Valid
Invalid
Reason
TOTAL-OF-FIGURES
DATA
34B100-PARA1
-48B
Hyphen in beginning
GROSS-PAY
GROSS PAY
Literals
Examples
Numeric constants
35, -345.67
Alphanumeric constants
Leo talstoy
ka01-h215
Notes:
All user-defined words should conform to following rules:
11
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
UNIT 2
The Organization of a COBOL Program
12
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
IDENTIFICATION DIVISION
IDENTIFICATION DIVISION.
PROGRAM-ID. <Pgm-name>
AUTHOR.
<Pgmr-name>
DATE WRITTEN. <Entry>
DATE-COMPILED. <Entry>
SECURITY. <Entry>
Required
Required
Optional
Optional
Optional
Optional
Notes:
The Identification Division must be the first division in every COBOL source
program. It must be coded as IDENTIFICATION DIVISION or ID DIVISION
followed by a separator period.
The Identification Division identifies the source program and the resultant output
listing. The user can include the date the program is written and other information as
desired under the paragraphs in the general format. This entire division (including
the division header) is optional.
13
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
ENVIRONMENT DIVISION
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. <Entry>.
OBJECT-COMPUTER. <Entry>.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
------------------------------------------------------I-O-CONTROL.
---------------------------------------------------------
Notes:
The Environment Division is divided into two sections:
The CONFIGURATION SECTION
The Configuration Section is an optional section for programs which describe the
computer environment on which the program is compiled and executed.
The Configuration Section can be specified only in the ENVIRONMENT
DIVISION of the outermost program of a COBOL source program.
The INPUT-OUTPUT SECTION
The Input-Output Section of the Environment Division contains two paragraphs:
o FILE-CONTROL paragraph
o I-O-CONTROL paragraph
FILE-CONTROL paragraph
The keyword FILE-CONTROL can appear only once, at the beginning of the
FILE-CONTROL paragraph. It must begin in Area A, and be followed by a
separator period. The FILE-CONTROL paragraph is optional.
The FILE-CONTROL paragraph associates each file in the COBOL program with
an external data set, and specifies file organization, access mode, and other
information.
14
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
15
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
DATA DIVISION
Data division is the third and most frequently used division in all
programs. Every data items or variable required by the program should
be declared in appropriate section of the data division, before using in
procedure division. The Data Division is divided into three sections:
File Section:
Defines the structure of data files (including sort-merge files). If the
program is accessing files.
Working-Storage Section:
Describes records and subordinate data items that are not part of
data files but are required by the program.
Linkage Section:
Describes data made available by another program. It usually
appears in the called program and describes data items that are referred
to by the calling and the called programs.
16
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
DATA DIVISION.
FILE SECTION.
FD . ----------------------------WORKING-STORAGE SECTION.
01 VAR-1
PIC A(5).
01 ID-1
PIC X(10)
01 DATA-NAME
PIC 9(5)
Level number
picture
Clause
DATA TYPES
-Alphabetic
-Alphanumeric
-Numeric
LINKAGE SECTION.
record-description-entry
data-item-description-entry
17
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
DATA-ITEMS
Explicitly identifies the data being described
A data-item
paragraph name
Notes:
Data item is a user-defined word which is associated with Level number.
COBOL Reserved words should not be Data items.
The data division of a COBOL source program describes, in a structured manner, all
the data to be processed by the program.
This division allocates memory locations for the data items that a program requires.
There are several types of storage locations in COBOL: file buffers, misc. scratch
data, communication buffers, screen paint data and report format data.
18
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Level Numbers
Range of level numbers available are 01 to 49 and
01 and 77 level entries must begin from area A and other level
entries can begin from any where in area A or area B
Notes:
Level represents the nature of a data item.
The level-number specifies the hierarchy of data within a record, and identifies
special-purpose data entries. A level-number begins a data description entry, a
renamed or redefined item, or a condition-name entry. A level-number has a value
taken from the set of integers between 01 and 49, or from one of the special levelnumbers, 66, 77, or 88.
Level-number 01 and 77 must begin in Area A and must be followed either by a
separator period; or by a space, followed by its associated data-name, FILLER, or
appropriate data description clause.
19
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
20
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
21
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Picture Clause
Describes the characteristics of the data
CODE
meaning
A
B
G or N
9
X
P
alphabetic or space
Blanks or spaces
Graphical data
Indicates a Numeric
Indicates an Alpha Numeric
Indicates the position of the assumed
decimal point when the point lies outside
the data item.
Indicates the position of assumed decimal
point of numeric field.
Indicates whether the data item signed.
V
S
Notes:
22
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
W-S Declarations
WOKING-STORAGE SECTION.
01 PAY.
05 GROSS-PAY.
10 BASIC PIC 9(4)V99.
10 DA
PIC 9(4)V99.
10 HRA
PIC 9(4)V99
05 DEDUCTIONS.
07 PF-DED
07 IT-DED
05 NET-PAY
PIC
05 NAME
PIC
05 E-CODE
PIC
PIC 9(3)V99.
PIC 9(3)V99.
9(4)V99.
A(5).
X(6).
Alternatively
9(4)V9(2)
9999V99
AAAAA
XXXXXX
Notes:
Use the WORKING-STORAGE SECTION in the DATA DIVISION of the
OBJECT paragraph to describe the instance data that a COBOL class needs, that is,
the data to be allocated for each instance of the class. The OBJECT keyword, which
you must immediately precede with an IDENTIFICATION DIVISION declaration,
indicates the beginning of the definitions of the instance data and instance methods
for the class.
Pay, gross-pay, deductions are called group items and they dont have PICTURE
clause. Other elements with picture clause are called elementary items, which cannot
be broken further.
Pay is a Group item is divided into Gross-pay, Deductions, net-pay, name, e-code
further Gross-pay sub-divided into Basic, DA, HRA and DEDUCTIONS subdivided into PF-DED and IT-DED.
23
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
FILLER
FILLER is a COBOL Reserved Word used to describe data fields that
will not be referenced in the PROCEDURE DIVISION.
EMPLOYEE-ADDRESS
FILLER
PIC
PIC
PIC
PIC
PIC
PIC
X.
X(6).
X(30).
X(2).
X(60).
X(34).
Notes:
FILLER is a data item that is not explicitly referred to in a program. The key word
FILLER is optional. If specified, FILLER must be the first word following the levelnumber.
IF data-name or FILLER clause is omitted, the data item being described is treated
as though FILLER had been specified.
The VALUE clause may be used on FILLER items, e.g. to assure BLANKS in
header lines between fields.
In a MOVE CORRESPONDING statement, or in an ADD CORRESPONDING or
SUBTRACT CORRESPONDING statement, FILLER items are ignored.
In an INITIALIZE statement, elementary FILLER items are ignored.
24
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
USAGE Clause
<level number> data-name
[PIC
X(n)]
[USAGE] COMP
COMP-1
COMP-2
COMP-3
COMP
- Binary Representation
COMP-1
COMP-2
COMP-3
Notes:
The usage description must match the data-field type described in the FD descriptor
of the COBOL program. If the COBOL program does not include a usage clause,
select the Chars (character) option for the usage.
The USAGE clause can be specified for a data description entry with a level-number
other than 66 or 88. However, if it is specified at the group level, it applies to each
Elementary item in the group. The usage of an elementary item must not contradict
the usage of a group to which the elementary item belongs.
The USAGE clause specifies the format in which data is represented in storage. The
format can be restricted if certain Procedure Division statements are used.
When the USAGE clause is not specified at either the group or elementary level, it
assumed that the usage is DISPLAY.
25
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
COMPUTATIONAL-2(COMP-2) Usage
This usage is same as COMP-1, except that the data is represented internally in two words.
The advantage is that this increases the precision of the data which means that more
significant digits can be available for the item. The PICTURE clause cannot be specified for
COMP-2 items. Specified for internal floating-point items (double precision). COMP-2
items are 8 bytes long.
COMPUTATIONAL-3(COMP-3) Usage
In this form of internal representation the numeric data is the decimal form, but one digit
takes half-a-byte. The sign is stored separately as the right most half a-byte regardless of
whether S is specified in the PICTURE or not. The hexa decimal number C or F denotes a
positive sign and the Hexa decimal number D denotes a negative sign. Inorder that data
fields can start and end on byte boundaries, numbers with an even number of digits are
stored with an extra half-byte of zeroes on the left hand side. Thus an item with
PICTURE
S9(5)V9(3)
USAGE IS COMP-3
will require 5 bytes to be stored internally. Only the characters 9, S, V and P can be used in
the PICTURE of a COMP-3 item. This is the equivalent of PACKED-DECIMAL.
26
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Value Clause
Value Clause defines the initial value of a data item must not be used for
items declared in FILE SECTION. Can also specify FIGURATIVE
CONSTANTS. If defined at the group level can be used for array
declaration also
EXAMPLES
01 NUM-1
01 E-CODE
PIC 9(3)
PIC X(6)
At group level
01 GROUP-ITEM
05
E-ITEM-1
05
E-ITEM-2
05 E-ITEM-3
VALUE 245.
VALUE E10K3.
contents
VALUE IS ER34155
PIC X(2).
ER
PIC XXX
341
PIC X(3)
55
27
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
REDEFINES Clause
Two or more data items can share the same working storage area by
REDEFINING a storage area.
Notes:
Two or more storage areas defined in the data sometimes may not be used
simultaneously, in such cases; only one storage area can serve the purpose of
two or more areas if the area is defined.
The REDEFINES clause used allows the said area to be referred to by more
than one data name with different sizes and pictures.
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
DATA DIVISION.
WORKING-STORAGE SECTION.
01
01
01
01
X1
02
02
X3
02
02
02
X4
02
02
X5
02
Y
Y1
PIC 99.
REDEFINES
PIC XX.
Z
PIC X
VALUE M.
ZZ
PIC X (25)
VALUE ALL *.
ZZZ
PIC X (45)
VALUE ALL - .
REDEFINES X3.
FILL1
PIC X.
FILL2
PIC X (70).
REDEFINES X4.
BUFFER
PIC X (71).
PROCEDURE DIVISION
PARA 1.
MOVE 20 TO Y.
DISPLAY X1.
MOVE A1 TO Y1.
DISPLAY X1
DISPLAY X3.
DISPLAY X4.
DISPLAY X5.
STOP RUN.
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
PIC 9(5).
PIC X (25).
PIC X (20).
PIC
PIC
PIC
PIC
PIC
X
X
X
X
X
(5).
(5)
(5).
(25).
(920).
RENAMES Clause
Syntax:
30
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Notes:
In order to re-group elementary data items in a record, so that they can belong to the
original as well as to the new group, the RENAMES clause is used.
The RENAMES clause specifies alternative and possibly overlapping groupings of
elementary data items.
LEVEL-66 regroups previously defined items.
A level-66 entry cannot rename another level-66 entry, nor can it rename a level-01,
level-77, or level-88 entry.
All level-66 entries associated with one record must immediately follow the last data
description entry in that record.
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
02
05
05
66
66
FIXED-PAY
10 E-BASIC
PIC 9(6). 99
10 E-DA
PIC 9(6). 99.
ADDL-PAY.
10 HRA
PIC 9(4). 99.
10 INCENTIVE
PIC 9(3). 99.
DEDUCTIONS.
10 E-PF
PIC 9(3). 99.
10 E-IT
PIC 9(4). 99.
10 OTHERS
PIC 9(3). 99.
PAY-LESS-BASIC RENAMES E-DA THRU INCENTIVE.
IT-AND-PF RENAMES E-PF THRU E-IT.
PROCEDURE DIVISION.
MAIN-PARA
MOVE-123456.78 TO E-BASIC.
MOVE 234567.89 TO E-DA.
MOVE 1234.56 TO HRA.
MOVE 123.45 TO INCENTIVE.
MOVE 123.45 TO E-PF.
MOVE 1234.56 TO E-IT.
MOVE 123.45 TO OTHERS.
DISPLAY PAY.
DISPLAY FIXED-PAY.
DISPLAY ADDL-PAY.
DISPLAY DEDUCTIONS.
DISPLAY PAY-LESS-BASIC.
DISPLAY IT-AND-PF.
STOP RUN.
Figurative Constants
Constants frequently used by most programs
32
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Figurative Constants
Meaning
HIGH-VALUE(S)
LOW-VALUES (S)
SPACE (S)
Example:
Notes:
Figurative constants are reserved words that name and refer to specific constant
values.
ZERO, ZEROS, ZEROES:
Represents the numeric value zero (0) or one or more occurrences of the character
zero, depending on context. When the figurative constant ZERO, ZEROS, or
ZEROES is used in a context that requires an alphanumeric character, an
alphanumeric character zero is used.
SPACE:
Represents one or more blanks or spaces. SPACE is treated as an alphanumeric
literal when used in a context that requires an alphanumeric character, as a DBCS
literal when used in a context that requires a DBCS character, and as a national
literal when used in a context that requires a national character.
HIGH-VALUE:
Represents one or more occurrences of the character that has the highest ordinal
position in the collating sequence used. HIGH-VALUE is treated as an alphanumeric
literal in a context that requires an alphanumeric character.
33
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
LOW-VALUE:
Represents one or more occurrences of the character that has the lowest ordinal
position in the collating sequence used. LOW-VALUE is treated as an alphanumeric
literal in a context that requires an alphanumeric character.
Edited Fields
Move 345.46 to a field of picture 9(3)v99 & display or print You
may see different number in result
34
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Effect
Editing Codes are specified in the picture clause for variables intended for
report purpose.
Meaning
35
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
CR or DB
Blank is appeared.
- (hyphen) /(slash)
EXAMPLES
DATA
02346
0005
PIC CLAUSE
UNEDITED
9(5)
9(4)
PIC CLAUSE
EDITED
ZZ999
ZZ99
EDITED
VALUE
2346
05
36
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
03.42
0.007
05634
00143
453
-0453
-0453
453
-453
70.46
156758
00
99V99
9V999
9(5)
9(5)
9(3)
9(4)
9(4)
9(3)
9(3)
99V99
9(6)
99V9
8654
24
9(4)
99
Z999
ZV999
**999
$9(5)
$**999
-ZZ9(2)
9999999999+
99.9999/99/99
99.9 Blank when
zero
99b9b9
9900
003
007
*5634
$00143
$**453
-453
0453453
45370.46
15/67/58
86b5b4
2400
Notes:
The above table shows contents of unedited fields in the first column. Contents of
edited fields after moving the data-1 shown in last column.
Edited fields (Fields with editing codes) cannot take part in arithmetic computations.
Moving of numeric edited fields to unedited fields is illegal.
37
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
UNIT 3
PROCEDURE DIVISION
38
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
PROCEDURE DIVISION
PROCEDURE DIVISION[USING <DATA-ITEM1>, <DATA-ITEM2>.
MAIN-PARA.
DISPLAY ENTER VALUE OF A:.
ACCEPT A.
DISPLAY ENTER VALUE OF B:.
ACCEPT A.
MOVE A TO B.
ADD A TO B.
DISPLAY A VALUE : A.
DISPLAY B VALUE : B.
---------------------------------------------------------------STOP RUN.
Notes:
Procedure Division can consists of
Sections (Optional)
Paragraphs (Optional)
Statements.
While coding, we must follow the following Hierarchy:
SECTION------- PARAGRAPHS ------ STATEMENTS
Or
PARAGRAPH------- STATEMENTS
Or
STATEMENTS
39
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
COBOL VERBS
All instructions are coded in Procedure division.
BASIC COBOL VERBS
MOVE
ACCEPT
DISPLAY
PERFORM
GO TO
STOP RUN
CALL
COPY
SORT
MERGE
FILE OPERATIONS
CHARACTER HANDLING
TABLE HANDLING
CONDITIONS
ARITHMETIC VERBS
Notes:
Arithmetic Verbs
Conditions
File handling
Character handling
Table handling
Paragraphs
Paragraphs are building blocks of the PROCEDURE DIVISION
40
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
PROCEDURE DIVISION.
MAIN-PARA.
STATEMENT1.
STATEMENT2.
------------------------------------------------------------PARA-100.
-----------------------------------------
Notes:
A paragraph-name must begin in Area A and must be followed by a separator
period.
A paragraph-name need not be unique because it can qualified by a SECTION
name.
Paragraph-names need NOT contain any alphabetic character (i.e. can be all
numeric).
A paragraph ends at:
The next paragraph-name or section header
The end of the PROCEDURE DIVISION
The Scope terminator END-PARAGRAPH
Terminator Statements
EXIT PROGRAM
41
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
STOP RUN
The STOP RUN statements halts the execution of the object program,
and returns control to the system
GOBACK
The GOBACK statement functions like the EXIT PROGRAM
statement. When it is coded as part of a called program and like the
STOP RUN when coded in a main program
Notes:
If these statements are not the last statements in a sequence, statements following
them will not be executed.
Scope Terminators
Explicit scope terminators mark the end of certain PROCEDURE
DIVISION statements.
Explicit scope terminators are COBOL Reserved Words.
42
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
END-ADD
END-SEARCH
END-CALL
END-MULTIPLY
END-START
END-COMPUTE
END-PERFORM
END-STRING
END-DELETE
END-READ
END-DIVIDE
END-UNSTRING
END-EVALUATE
END-REWRITE
END-WRITE
END-IF
An explicit Scope Terminator is paired with the unpaired occurrence of
the verb. An implicit Scope Terminator is a separator period.
Notes:
Example:
PERFORM PARA-1 UNTIL A > 10
STATEMENT1
STATEMENT2
-------------------------------------END-PERFORM.
Period (.) should not encounter in between PERFORM and END-PERFORM.
Since it indicates end of the PERFORM statement, then compiler error will raise.
DISPLAY Verb
The function of the DISPLAY statement is to display low-volume results
on the operators console or some other hardware device.
43
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Syntax:
>>____DISPLAY_____ __identifier-1___ __ |
_____________________________________________>
| _ literal-1______|
E.g:
PROCEDURE DIVISION.
DISP-PARA.
DISPLAY SRCH-ARG NOT IN TABLE..
---------------------------------------------------------------DISPLAY HELLO HOW ARE YOU.
Notes:
The DISPLAY statement transfers the contents of each operand to the output
device. The contents are displayed on the output device in the order, left to right,
in which the operands are listed.
WITH NO ADVANCING when specified, the positioning of the output device
will not be changed in any way following the display of the last operand.
ACCEPT Verb
Format 1 transfers data from an input/output device into identifier1.
When the FROM phrase is omitted, the system input device is
assumed.
44
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
MOVE Verb
MOVE verb is used to copy the contents of an identifier into another
identifier.
MOVE <identifier-1>
Or
<literal-1>
TO <identifier-2>[<identifier-3>,.].
45
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
E.g.:
MOVE
MOVE
MOVE
MOVE
MOVE
A TO B, C, D
dataname-1 to dataname-2
345 to num-1
345 TO K
XYZ TO data-name-1
If the length of the receiving field is less than the length of sending field
then truncation occurs.
Notes:
The MOVE statement transfers data from one area of storage to one or more other
areas.
An index data item cannot be specified in a MOVE statement.
If the sending field (identifier-1) is reference-modified, subscripted, or is an
alphanumeric or alphabetic function-identifier, the reference-modifier, subscript,
or function is evaluated only once, immediately before data is moved to the first of
the receiving operands.
PIC X (10).
46
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
01 DATA-FLD
PIC X (10).
01 OLD-ADDR.
05
NO
PIC X (5).
05
NAME
PIC X (15).
------------------------------------------------------------01 NEW-ADDR.
05
N-NO
PIC X (5).
05
N-NAME
PIC X (15).
------------------------------------------------------------MOVE OUT OF SEQUENCE TO MSG-FIELD
MOVE SPACES TO OLD-ADDR, NEW-ADDR
MOVE DATA-FLD TO MSG-FIELD.
MOVE NEW-ADDR TO OLD-ADDR.
Notes:
Elementary move
-
Group Move
-
CORRESPONDING Phrase
01 STRUCT-1.
03 FIELD-A
03 FIELD-B
03 FIELD-C
03 FIELD-D
01 STRUCT-2.
10 FIELD-C
10 FILLER
10 FIELD-B
PIC
PIC
PIC
PIC
PIC
PIC
PIC
9(9)
X(5)
9(4)V99
9(4)V99
VALUE 23456789.
VALUE abcde.
VALUE 1234.56.
VALUE 123456789.
Z(4).99.
XXX.
X(5).
47
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
10 FILLER
10 FIELD-A
10 FILLER
PIC
PIC
PIC
XXX.
Z(9)
XXX.
VALUE
VALUE
VALUE
VALUE
2345.
ALL N.
ALL D
1234.67.
48
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
05
05
05
05
05
05
05
E-ID
FILLER
E-NAME
FILLER
E-DEPT
FILLER
E-BASIC
PIC
PIC
PIC
PIC
PIC
PIC
PIC
9(5)
X(5)
X (25).
X(5).
X(20)
X(5)
9(4). 99
PROCEDURE DIVISION.
PARA 1.
MOVE E-ID OF DATA-1 TO E-ID OF DATA-2
MOVE E-NAME OF DATA-1 TO E-NAME OF DATA-2.
MOVE E-DEPT OF DATA-1 TO E-BASIC OF DATA-2.
DISPLAY DATA-1
DISPLAY DATA-2
MOVE SPACES TO DATA-2.
MOVE CORRESPONDING DATA-1 TO DATA-2.
DISPLAY DATA-1
DISPLAY DATA-2.
STOP RUN.
Reference Modification
Reference Modification defines a data item by specifying its leftmost
character and optionally, a length
49
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Notes:
Eg:
WORKING-STORAGE SECTION.
01
CAT-TYPE
PIC X(15) VALUE 'CALICO'.
01
DOG-TYPE
PIC X(15) VALUE 'SCHNAUZER'.
01
CAT-ABBREV PIC X(5).
01
DOG-END
PIC X(10).
PROCEDURE DIVISION.
*Reference Modification Example Number 1: (From position 1:For 5 positions.)
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
*This will move "AUZER" to DOG-END. (The letters from position 5 of DOGTYPE to the end of DOG-TYPE.)
DISPLAY DOG-END.
ADD Verb
All identifiers (or literals) preceding the word TO are added
together, and then this sum is added to, and replaces, each
identifier-2. The action is repeated in order left-to-right for each
identifier-2
Identifiers must be elementary numeric items
Format 1 :
51
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Example:
ADD A TO B.
ADD 112 TO B.
ADD A TO B ON SIZE ERROR GO TO ERR-PARA.
52
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Example:
ADD A TO B GIVING C
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Format:
>>___ADD_______ CORRESPONDING_ ___identifier-1___ TO___ identifier2____________________>
|_CORR__________|
>___ ______________ __ ______________________________________________
________________ >
|_ ROUNDED__| | _ ___ __SIZE ERROR____ imperative-statement-1_|
|_ ON_ |
>___ _____________________________________________________
___________________________ >
|_NOT___ ______ __SIZE ERROR__imperative statement_1_|
|_ON___|
>___ _____________________________________________________
___________________________ >
|_ NOT___ ______ _SIZE ERROR__imperative statement_2_|
|_ ON_|
>___ _______ _________________________________________________ >
|_ END-ADD_|
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
ADD
CORRESPONDING
or
SUBTRACT
Size error
Zero raised to zero
power
Zero raised to a
negative number
A negative number
raised to a fractional
power
57
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
The size error condition applies only to final results, not to any
intermediate results.
NUMERIC Data
Types of numeric items are:
Binary
Packed decimal. (Internal decimal)
Floating point representation.
58
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Notes:
A VALUE clause can specify a figurative constant ZERO.
SUBTRACT Verb
Format 1:
>>___SUBTRACT_______ identifier-1_ _|__
FROM_________________________________________>
|_literal-1___|
> ______identifier-2__ _______________
_|________________________________________________>
| _ ROUNDED ____|
59
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
>___ ____________________________________________________
___________________________>
|_ ____ __SIZE ERROR imperative-statement-1______|
|_ON _|
>___ _____________________________________________________
___________________________>
|_ NOT___ ______ ___SIZE ERROR__imperative statement_2_|
>___ _______ _________________________________________________>
|_ END-SUBTRACT_|
All identifiers or literals preceding the key word FROM are added
together and this sum is subtracted from and stored immediately in
identifier-2. This process is repeated for each successive occurrence of
identifier-2, in the left-to-right order in which identifier-2 is specified.
Notes:
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
>___ _____________________________________________________
____________________________ >
|_
_______ ___SIZE ERROR__imperative statement_1_|
|_ ON _|
>___ _____________________________________________________
____________________________ >
|_ NOT___ ______ _SIZE ERROR__imperative statement_2_|
|_ ON_|
>___ _______ __________________________________________________ >
|_ END-SUBTRACT_|
All identifier or literals preceding the key word FROM are added
together and this sum is subtracted from identifier-2 or literals-2. The
result of the subtraction is stored as the new value of each data item
referenced by identifier-3.
Notes:
Example:
1. SUBTRACT A FROM B.
The value of a subtracted from the value of B and then the resultant value will be
stored in B.
2. SUBTRACT 9 FROM C.
3. SUBTRACT C FROM 9. Is not valid because 9 is a Literal.
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
|_ON___|
>___ _____________________________________________________
____________________________ >
|_ NOT___ ______ _SIZE ERROR__imperative statement_2_|
|_ ON__|
>___ _______ __________________________________________________ >
|_ END-SUBTRACT_|
Elementary data items within identifier-1 are subtracted from, and the
results are stored in, the corresponding elementary data items within
identifier-2.
Notes:
MULTIPLY Verb
Format 1:
>>___MULTIPLY_______ identifier-1___ ___BY____identifier-2___
__________________| __________>
|_ literal-1________|
>___ _____________________________________________________
____________________________ >
|____ ______ __SIZE ERROR__imperative statement_1_|
|_ON___|
>___ _____________________________________________________
____________________________ >
|_ NOT___ ______ _SIZE ERROR__imperative statement_2_|
|_ ON__|
>___ _______ _________________________________________________ ><
62
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
|_ END-MULTIPLY_|
Notes:
The MULTIPLY statement multiplies numeric items and sets the values of data
items equal to the results.
63
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
>___ _____________________________________________________
____________________________>
|_ NOT___ ______ _SIZE ERROR__imperative statement_2_|
|_ ON_|
>___ _______ _________________________________________________ >
|_ END-MULTIPLY_|
Notes:
DIVIDE Verb
Format 1:
>>___DIVIDE_____ _____ identifier-1_ _|__ INTO__________identifier-2____
_________
__ |____>
|_literal-1___|
|_ROUNDED _|
>___ ____________________________________________________
____________________________>
|_ ____ __SIZE ERROR imperative-statement-1____________|
|_ON _|
>___ _____________________________________________________
____________________________>
|_ NOT___ ______ ___SIZE ERROR__imperative statement_2_|
|_ON __|
>___ _______ __________________________________________________>
|_ END-DIVIDE_|
64
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Notes:
The DIVIDE statement divides one numeric data item into or by other(s) and sets
the values of data items equal to the quotient and remainder
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Notes:
COMPUTE Verb
Format:
>>___COMPUTE_______ identifier-1_ ____________ _|____ _ =______
__________________________>
|_ ROUNDED _|
|_ EQUAL_|
>___ arithmetic
expression_______________________________________________________________
_>
____________________________ >
|_
_______ ___SIZE ERROR__imperative statement_1_|
|_ ON _|
>___ _____________________________________________________
____________________________ >
|_ NOT___ ______ _SIZE ERROR__imperative statement_2_|
|_ ON_|
>___ _______ __________________________________________________ >
|_ END-COMPUTE_|
66
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
The arithmetic expression is calculated and replaces the value for each
identifier-1 item. Valid operators allowed in the expression are:
+ addition
- subtraction
* multiplication
/ division
** exponentiation
Notes:
The COMPUTE statement assigns the value of an arithmetic expression to one or
more data items.
With the COMPUTE statement, arithmetic operations can be combined without
the restrictions on receiving data items imposed by the rules for the ADD,
SUBTRACT, MULTIPLY, and DIVIDE statements.
Identifier-1
Must name elementary numeric item(s) or elementary numeric-edited item(s). Can
name an elementary floating-point data item. The word EQUAL can be used in
place of =.
An arithmetic expression ca consists of any of the following:
1.
2.
3.
4.
When the COMPUTE statement is executed, the value of the arithmetic expression
is calculated, and this value is stored as the new value of each data item referenced
by identifier-1.
67
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
PERFORM Statement
PERFORM Paragraph-name/Section-header
Notes:
68
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
PERFORM types
PERFORM Para-name
PERFORM Para-name N TIMES
PERFORM Para-name VARYING K FROM M BY N UNTIL CONDITION
K>20
PERFORM Para-name VARYING K FROM M BY N UNTIL CONDITION
K>20 AFTER VARYING.
PERFORM THROUGH
PROCEDURE DIVISION.
100-MAIN-PARA.
PERFORM 200-PARA THRU 500-PARA.
STOP RUN.
200-PARA.
* Statements.
400-PARA.
* Statements
500-PARA.
* Statements
300-PARA.
Statement
- Not executed
Notes:
69
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
PERFORMN times
PERFORM PARA-NAME-1[THROUGH (or) THRU PARA-NAME-N]
N TIMES.
EX:
Notes:
70
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
PERFORMVARYING
PERFORM PARA-NAME-1 [THRU (or) THROUGH PARA-NAME-N]
VARYING { identifier- 1 }
{identifier-2 }
{Index-name-1} FROM
{index-name-2}
{ Literal-1
}
BY
{identifier-3 }
{Literal-2 }
UNTIL
Condition
EX:
PERFORM PARA-2000 THRU PARA-5000 VARYING A FROM
M BY N UNTIL A > Y
PERFORM para-1 Varying K FROM 10 BY 5 UNTIL K>100
Notes:
Example 2 says:
Sets the value of K to 10 initially
Execute para-1
71
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Enter
Set identifier 1
to initial value
True
Condition
Exit
False
False
Execute range
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
AFTER
BY
{identifier-3 }
{Literal-2
}
{ identifier- 4 }
{ Index-name-3}
{identifier-6}
{Literal-4
}
UNTIL
FROM
UNTIL
AFTER
{ identifier- 7 }
{Index-name-5} FROM
BY
{identifier-9 }
{Literal-6
}
UNTIL
Condition-1
{identifier-5 }
{index-name-4}
{ Literal-3
}
Condition-2
{identifier-8 }
{index-name-6}
{ Literal-5
}
Condition-3
This form is used when a nested repetition of the range is required while varying
more than one identifier.
73
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
For example:
PERFORM RANGE-TO-BE-EXECUTED
VARYING I FROM 1 BY 1 UNTIL I > 50
AFTER
J FROM 1 BY 1 UNTIL J > 10.
The range RANGE-TO-BE-EXECUTED will be performed 500 times,.
In-Line PERFORM
The in-line PERFORM will be coded using END-PERFORM.
Named Paragraph
PERFORM MOVEIT
VARYING X FROM 1 BY 1 UNTIL X = 5.
...
MOVEIT.
MOVE DATA-FLD (X) TO PRINT (X).
In-line PERFORM
PERFORM VARYING X FROM 1 BY 1 UNTIL X = 5.
MOVE DATA-FLD (X) TO PRINT (X).
END-PERFORM.
Notes:
An In-line PERFORM requires the END-PERFORM terminator. Conversely the
END-PERFORM phrase must not be specified when the statement is PERFORM
procedure name.
74
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
75
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Notes:
The IF statement evaluates a condition and provides for different sets of
statements to execute, depending on the evaluation of the IF.
Condition can be any simple or complex condition.
Statement-1, statement-2 Can be any one of the following:
An imperative statement
An conditional statement
An imperative statement followed by a conditional statement
NEXT SENTENCE If the NEXT SENTENCE phrase is specified, and then the
END-IF phrase must not be specified. NEXT SENTENCE passes control to the
statement after the closest following period. However, if the NEXT SENTENCE
phrase is executed, control will not pass to the statement after the closest following
period.
76
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Compound Conditionals
Conditional expressions can be compound using the AND and OR
logical operators
Conditional conditions can also use parentheses to group conditions.
IF
ITEM-1
=
DOMESTIC-ITEM-NO
AND ITEM-2
=
OVERSEAS-ITEM-NO
OR
ITEM-1
=
OVERSEAS-ITEM-NO
AND ITEM-2
=
DOMESTIC-ITEM-NO
SET MIXED-SHIPMENT-FLAG TO TRUE
END-IF
.
SEARCH TABLEPAIR VARYING NDX
WHEN ITEM-1(NDX) = FROM-CITY AND ITEM-2(NDX) = TO-CITY
MOVE
WHEN ITEM-2(NDX) = FROM-CITY AND ITEM-1(NDX) = TO-CITY
MOVE ..
END-SEARCH
Notes:
Relational Expressions
77
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
<
NOT <
>
NOT >
=
NOT =
>=
<=
Notes:
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
IF C = D
NEXT SENTENCE
ELSE
MOVE MESSAGE-1 TO RPT-MESSAGE-1
END-IF
ADD C TO TOTAL
DISPLAY TOTAL
IF E = F
MOVE MESSAGE-4 TO RPT-MESSAGE-2
END-IF
END-IF.
Example 2 - CONTINUE
IF A = B
IF C = D
CONTINUE
ELSE
MOVE MESSAGE-1 TO RPT-MESSAGE-1
END-IF
ADD C TO TOTAL
DISPLAY TOTAL
IF E = F
MOVE MESSAGE-4 TO RPT-MESSAGE-2
END-IF
END-IF.
Notes:
EVALUATE Statement
EVALUATE is a great way to implement the case programming
construct
EVALUATE dataname
WHENCourse
value-1
.
materials may not be produced in whole or in part
WHEN value-2
{THROUGH
without
the prior written| THRU}
permission. value-3 .
WHEN NOT value-4
Copyright
79
Perform add-trans
Perform delete-trans
Perform update-trans
Perform bad-trans
The scope of a WHEN clause is all statements UNTIL the next WHEN
clause, the END-EVALUATE, or a period
Notes:
The EVALUATE statement provides a shorthand notation for a series of nested IF
statements. It can evaluate multiple conditions. That is, the IF Statements can be
made up of compound conditions.
Examples:
Working-Storage for all Examples:
01 PLANET.
05 PLANET-NUMBER PIC 9.
05 PLANET-NAME PIC X(7).
Evaluate Example Number 1: (Evaluate a PIC 9 field)
80
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
EVALUATE PLANET-NUMBER
WHEN 1 MOVE "Mercury"
TO PLANET-NAME
WHEN 2 MOVE "Venus "
TO PLANET-NAME
WHEN 3 MOVE "Earth "
TO PLANET-NAME
WHEN 4 MOVE "Mars "
TO PLANET-NAME
WHEN 5 MOVE "Jupiter"
TO PLANET-NAME
WHEN 6 MOVE "Saturn "
TO PLANET-NAME
WHEN 7 MOVE "Uranus "
TO PLANET-NAME
WHEN 8 MOVE "Neptune" TO PLANET-NAME
WHEN 9 MOVE "Pluto "
TO PLANET-NAME
WHEN OTHER MOVE "
" TO PLANET-NAME
END-EVALUATE.
Evaluate Example Number 2: (Evaluate a PIC X field)
EVALUATE PLANET-NAME
WHEN "Mercury" MOVE 1
WHEN "Venus " MOVE 2
WHEN "Earth " MOVE 3
WHEN "Mars " MOVE 4
WHEN "Jupiter"
MOVE 5
WHEN "Saturn " MOVE 6
WHEN "Uranus " MOVE 7
WHEN "Neptune" MOVE 8
WHEN "Pluto " MOVE 9
WHEN OTHER
MOVE 0
END-EVALUATE.
TO
TO
TO
TO
TO
TO
TO
TO
TO
TO
PLANET-NUMBER
PLANET-NUMBER
PLANET-NUMBER
PLANET-NUMBER
PLANET-NUMBER
PLANET-NUMBER
PLANET-NUMBER
PLANET-NUMBER
PLANET-NUMBER
PLANET-NUMBER
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
MARKS
GRADE
80 100
A
60 - 79
B
45 - 59
C
30 - 44
D
0 - 29
E
The EVALUATE statement for the purpose is shown below.
EVALUATE MARKS
WHEN 80
THRU 100 MOVE A TO GRADE
WHEN 60
THRU 79 MOVE B TO GRADE
WHEN 45
THRU 59 MOVE C TO GRADE
WHEN 30 THRU 44 MOVE D TO GRADE
WHEN ZERO THRU 29 MOVE E TO GRADE
WHEN OTHER MOVE W TO GRADE
END-EVALUATE.
The literal W is moved to GRADE in the case of wrong marks.
MARTIAL-STATUS
SINGLE
MARRIED
WIDOWED
DIVORCED
ONCE-MARRIED
PIC 9.
VALUE 0.
VALUE 1.
VALUE 2.
VALUE 3.
VALUES ARE 1, 2, 3.
82
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
88
77
VALID-STATUS
AMOUNT
PIC 9 (4)
PROCEDURE DIVISION.
MAIN-PARA.
DISPLAY Martial Status:
DISPLAY 0- Single / 1- Married / 2- Widowed / 3- Divorced.
ACCEPT MARTIAL-STATUS.
IF NOT VALI-STATUS DISPLAY Error in Entry.
IF SINGLE SUBTRACT 100 TO AMOUNT.
IF MARRIED ADD 100 TO AMOUNT.
IF WIDOWED ADD 200 TO AMOUNT.
IF DIVORCED SUBTRACT 200 FROM AMOUNT.
IF ONCE-MARRIED ADD 250 TO AMOUNT
DISPLAY AMOUNT.
STOP RUN.
INITIALIZE Statement
The INITIALIZE statement sets selected categories of data fields to
predetermined values. It is functionally equivalent to one or more MOVE
statements.
When the REPLACING phrase is not used:
SPACE is the implied sending field for alphabetic alphanumeric,
alphanumeric-edited, and DBCS items. ZERO is the implied sending field
for numeric and numeric-edited items.
>>___INITIALIZE____identifier1_________________________________________________________________>
_______><
|
< _____________________________________________ __________________
Copyright
83
Course materials may not be produced in whole or in part
without the prior written permission.
Notes:
The INITIALIZE statement sets selected categories of data fields to predetermined
values. It is functionally equivalent to one or more MOVE statements.
A subscripted item can be specified for identifier-1. A complete table can be
initialized only by specifying identifier-1 as a group that contains the complete
table.
The data description entry for identifier-1 must not contain a RENAMES clause.
An index data item cannot be an operand of INITIALIZE.
Special registers can be specified for identifier-1 and identifier-2 only if they are
valid receiving fields or sending fields, respectively, for the implied MOVE
statement(s).
84
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
SET INACTIVE TO TRUE
SET SPEC-ACCTS TO TRUE
Notes:
Class Condition
NUMERIC
The item entirely contains characters 0 through 9 (with or without a sign
determined by its PICTURE clause). It may be USAGE DISPLAY or
PACKED DECIMAL.
ALPHABETIC
The entire item contains only A through Z, a through z, or spaces
ALPHABETIC-UPPER
The entire item contains only A through Z (exclusively upper-case) or
spaces.
ALPHABETIC-LOWER
The entire item contains only a through z (exclusively lower-case) or spaces.
Notes:
86
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Ex:
1. IF A IS NUMERIC
-------------------------------------------2. IF C IS ALPHABETIC
---------------------------------------------Where A and C are Data items.
87
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
UNIT 4
89
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
FILES
A record is a group of logically or functionally related fields.
A File is a group of Records.
A group of records, which can be created, copied, modified, retrieved and
deleted.
E.g.:
Details of an employee
-Name, Adds, Phone no., Dept no etc Forms a record
Notes:
Files can be broadly categorized into Program files and Data files. In COBOL the
term Files is used to indicate data files. Data files are normally created on a tape
or disk and subsequently program can refer them.
90
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Notes:
The size of a record is the cumulative size of all the fields in it.
If all the records of a file have the same structure then they are called Fixed
length-records.
For convenience, records of different lengths can be placed together in one file.
Then they are known as variable-length-records.
91
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
FILE-CONTROL Paragraph
Format:
SELECT [OPTIONAL] File-name-1 ASSIGN TO Assignment-name-1
[ RESERVE <INTEGER> AREA ]
SEQUENTIAL
[ ORGANIZATION IS
INDEXED
]
RELATIVE
[ACCESS MODE IS
SEQUENTIAL
RANDOM
DYNAMIC
92
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
ORGANIZATION IS SEQUENTIAL
The Records are stored in contigious allocation. To access the record in Sequential
mode only(I,e to read the last record, it reads all the records until last record
found.)
Deletion of record is not possible.Updation is possible but record length should
not changed.
ORGANIZATION IS INDEXED
Each record in the file has one or more embedded keys; each key is associated
with an index. An index provides a logical path to the data records, according to
the contents of the associated embedded record key data items. Indexed files must
be direct-access storage files. Records can be fixed-length or variable-length.
Each record in an indexed file must have an embedded prime key data item. When
records are inserted, updated, or deleted, they are identified solely by the values of
their prime keys. Thus, the value in each prime key data item must be unique and
must not be changed when the record is updated.
In addition, each record in an indexed file can contain one or more embedded
alternated key data items. Each alternated key provides another means of
identifying which record to retrieve.
The RECORD KEY clause specifies the data item within the record that is the
prime RECORD KEY for an indexed file. The values contained in the prime
RECORD KEY data item must be unique among records in the file.
The ALTERNATRE RECORD KEY clause specifies a data item within the record
that provides an alternated path to the data in an indexed file. Used like the
RECORD KEY but for an alternate index.
ORGANIZATION IS RELATIVE
The INPUT-OUTPUT FILE-CONTROL for Relative record files is very similar to
that of indexed files except you use the RELATIVE KEY clause of the ACCESS
MODE phrase and each record identified by the Relative Record Number instead
of Recoed Key.
93
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
ACCESS Mode
Modes
Meaning
SEQUENTIAL
RANDOM
DYNAMIC
directly without
Notes:
The record of a file stored on a magnetic tape can be accessed in sequential mode
only. But the records of file stored on magnetic disk can be accessed in all the
modes.
94
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Value
00
10
30
34
Status
Successful Completion
At end condition
Permanent error
Boundary violation
Notes:
Input-Output operations may not be successful thus resulting in termination of the
program.
The data-name specified in the file-status clause contains the status code and
can be referred by the programmer. Depending on the code programmer can take
specific actions by transferring the control to error-routine paragraphs.
The data name should be declared in working-storage section with alphanumeric
data type of two characters.
95
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
I-O-CONTROL Paragraph
The Optional I-O-CONTROL paragraph of the Input-Output Section
specifies when checkpoints are to be taken and the storage areas to be
shared by different files.
Specifies information needed for efficient transmission of data
between the external data set and the COBOL program.
Notes:
The I-O-CONTROL paragraph is optional.
The key word I-O-CONTROL can appear only once, at the beginning of the
paragraph. The word I-O-CONTROL must begin in Area A, and must be
followed by a separator period.
Each clause within the paragraph can be separated from the next by a separator
comma or a separator semicolon. The order in which I-O-CONTROL paragraph
clauses are written is not significant. The I-O-CONTROL paragraph ends with a
separator method.
96
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
FILE SECTION
FILE SECTION.
FD File-Name
BLOCK CONTAINS m RECORDS
RECORD CONTAINS n CHRACTERS
LABEL RECORDS ARE STANDARD/ OMMITED
01
File-record-structure.
Notes:
Each file used in the program should have an FD entry (File Description) in FILE
SECTION.
BLOCK CONTAINS clause specifies number of records in the block.
RECORD CONTAINS clause specifies total number of characters in each
record.
LABEL RECORDS clause indicates
Disk files if STANDARD option is specified
Print files if OMITTED option is specified
Value clause specifies the name of the physical file and the path
01 level entry should follow immediately after FD paragraph.
Blocking
Input-Output operations are slower compared to CPU processing speed. To reduce
the CPU waiting time, block of records from the disk can be moved to the memory
space called buffer thus reducing number of I-O operations.
The Programmer can specify the number of records contained in a block. Suitable
block size is to be selected by the programmer.
97
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
File Operations
Cobol Verbs
Meaning
WRITE
REWRITE
READ
DELETE
Notes:
This foil lists the possible operations that can be performed over files.
Before doing any operation, files should be opened and they must be closed before
exiting the program OPEN and CLOSE verbs are provided by COBOL.
98
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
OPEN modes
Mode
INPUT
OUTPUT
I-O
EXTEND
Notes:
SYNTAX
OPEN Mode File-name1, File-name2.
CLOSE File-name1, File-name2
While opening the file the mode must be specified depending on the operation to
perform.
More than one file can be opened and closed. Further, files can be opened and
closed more than once in a program.
99
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Notes:
For sequential access, the READ statement makes the next logical record from a
file available to the object program. For random access, the READ statement
makes a specified record from a direct-access file available to the object program.
When the READ statement is executed, the associated file must be open in INPUT
or I-O mode.
NEXT RECORD Reads the next record in the logical sequence of records. NEXT
is optional when ACCESS MODE IS SEQUENTIAL;
PREVIOUS RECORD Reads the previous record in the logical sequence of
records.
100
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Notes:
101
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
For VSAM INDEXED files, the KEY field contains a data value that
will be matched against the key filed in the file records until the first
record having an equal value is found.
For VSAM RELATIVE files, the KEY phrase must not be specified.
Notes:
Format 2 must be specified for indexed and relative files in random access mode,
and also for files in the dynamic access mode when record retrieval is random.
Execution of the READ statement depends on the file organization.
Indexed Files
Execution of a Format 2 READ statement causes the value of the key of reference
to be compared with the value of the corresponding key data item in the file
records, until the first record having an equal value is found. The file position
indicator is positioned to this record, which is then made available. If no record
can be so identified, an INVALID KEY condition exists, and READ statement
execution is unsuccessful.
If the KEY phrase is not specified, the prime RECORD KEY becomes the key of
reference for this request. When dynamic access is specified, the prime RECORD
KEY is also used as the key of reference for subsequent executions of sequential
READ statements, until a different key of reference is established.
102
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Relative Files
Execution of a Format 2 READ statement sets the file position indicator pointer to
the record whose relative record number is contained in the RELATIVE KEY data
item, and makes that record available.
The KEY phrase must not be specified for relative files.
103
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
The NEXT phrase must be specified for sequential access with dynamic
mode. In order to READ NEXT, position must have been established
in the file by a successful OPEN, START or READ statement
Notes:
104
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
START Statement
Format :
>>___START___file-name-1___________________________________________________________>
>__ _____________________________________________________________________ ________>
|_KEY___ ______ ____ __EQUAL___ ___ ________________ _data-name-1____|
|__TO _|
|
|_ TO_|
|
|_ = ______________________________|
|_LESS__ _______ ________________|
|
|_THAN_|
|
|_GREATER__ ____
_____________|
|
|_THAN_|
|
|_NOT LESS___ _______ ___________|
|
|_THAN _|
|
|_NOT < ___________________________|
|_NOT GREATER__ _______ ________|
|
|_THAN_|
|
|_NOT > ___________________________|
|_LESS_ ____ _ OR EQUAL_ __ _____|
|
|THAN|
|_TO_|
|
|_GREATER__ ____ _OR EQUAL_ __ _|
|
|_THAN_|
\ TO|
|_>+_______________________________|
>__ _______________________________________ _____________________________________>
|_INVALID___ _____ _imperative-statement-1_|
|_KEY_|
>__ _______________________________________ ___________ ____________ _____________>
|_NOT INVALID___ _____ imperative-statement-1_|
|_END-START_|
|_KEY_|
Notes:
The START statement provides a means of positioning within an indexed or
relative file for subsequent sequential record retrieval. When the START
statement is executed, the associated indexed or relative file must be open in either
INPUT or I-O mode.
file-name-1
Must name a file with sequential or dynamic access. File-name-1 must be defined
in an FD entry in the Data Division, and must not name a sort file.
END-START Phrase
This explicit scope terminator delimits the scope of the START statement. ENDSTART converts a conditional START statement to an imperative statement so
that it can be nested in another conditional statement. END-START can also be
used with an imperative START statement.
105
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
WRITE Statement
The WRITE statement releases a logical record for an output or
input/output file.
When the WRITE statement is executed:
- The associated sequential file must be open in OUTPUT or
EXTEND mode.
- The associated indexed or relative file must be open in OUTPUT, IO, or EXTEND mode.
Record-name must be defined in a Data Division FD entry. Recordname can be qualified. It must not be associated with a sort or merge
file.
Notes:
106
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
WRITE.FROM
PROCEDURE DIVISION.
WRITE File-rec FROM Identifier.
File-rec is record-name declared in FILE-SECTION.
Identifier is a working-storage section variable
The length of the identifier should be equal to the length of the
record.
Notes:
To Create a file, program can accept the data from the terminal into file record and
write it.
If the data need to be processed, it can be accepted in a W-S identifier. After
processing the data the above WRITE..FROM statement can be issued.
Each WRITE statement writes one record at a time.
107
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
READ.INTO
PROCEDURE DIVISION.
READ FILE-name (INTO W-S-Rec) | (AT END Statement)
Notes:
READ statement on sequential files reads one record at a time and makes it
available to program.
Reading begins from first record and if the READ statement is put in a loop. That
is executing the statement repeatedly, then it is possible to read consecutive
records.
Loop can be terminated before AT END condition is reached if required so by the
program.
If the file is left open next time when the read statement executes, reading
continuous from where it was stopped before the termination of loop.
If the file is closed then it is to be opened again before reading it.
108
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
-----------------not allowed
Notes:
It is often required to change the existing data and the process is called
UPDATING.
COBOL provides REWRITES verb to modify an existing record.
For example, changing the address field of an employee requires reading of
employee number. Every record to be updated needs to be read first. To search
the record of an employee, whose employee number is known, the process is as
follows
Store the employee number in a variable
Open the file
Read first record
Compare the variable with Emp-No field of the file
If it matches update his address by REWRITE
Else read next record his address by REWRITE
Repeat the process until the require record is read.
109
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
When new records to be added to file open the file in EXTEND mode
EXTEND mode causes the pointer to move to the end of the file.
Notes:
110
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
CLOSE Statement
Format :
CLOSE File-name-1, [File-name-2 .]
CLOSE Statement Releases the Resourcces which are assigned to that file.
Cannot Close the file which is not opened.
After performing the operations on the file (I,e no longer used in a program) needs to be
closed but not necessary.
If the FILE STATUS clause is specified in the FILE-CONTROL entry, the associated
status key is updated when the CLOSE statement is executed.
If the file is in an open status and the execution of a CLOSE statement is unsuccessful,
the EXCEPTION/ERROR procedure (if specified) for this file is executed.
Notes:
111
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Sequential Files
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL
SELECT file-name ASSIGN TO DEVICE-NAME
ORGANIZATION IS SEQUENTIAL.
ACCESS MODE IS SEQUENTIAL.
FILE STATUS IS data-name.
Area
B
Notes:
All the files used in the program should have an entry in FILE CONTROL
paragraph.
For each file used, there should be one SELECT..ASSIGN clause.
The file-name is select clause is user defined word and can be used throughout the
program wherever required.
ASSIGN clause specifies the device on which file stored.
112
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
113
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Indexed Files
Index component consists of a index structure with a record key values and
addresses of corresponding records.
RECORD KEY is one or more fields of the records.
Suitable record key is to be chosen by the programmer depending on the
functionality of the fields.
E.g :
Employee-code, Job-number.
Employee-name, Job-name.
Notes:
When an indexed file is created
An index component is also created containing some index tables based on
record keys.
A data component is created containing the actual records.
Record keys identify every record in the file.
The process of accessing a record involves searching for the record key with
matching index value. Then locate the record from the corresponding address.
This is done by the system itself.
114
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
INVALID KEY
(READ | WRITE | REWRITE | DELETE) File-name
(INVALID KEY Statement)
(AT END Statement).
Records and indexes of an indexed file are stored in key sequence order to
facilitate faster access.
Invalid key clause checks whether any input-output operation is violating the
Uniqueness of primary keys
E.g. add a record with duplicate value.
Proper read
E.g. try to read a non-exist record
Notes:
115
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Notes:
When READ NEXT statement is to be executed each time the records are read
consecutively.
IF the access mode is RANDOM a record is read from corresponding key value.
116
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Notes:
In a situation demanding the access of more than one consecutive records from
the middle of the file then dynamic access is used.
The START verb places the read pointer to the record whose key value is
compared with an identifier. Record is accessed randomly.
READNEXT can be put into loop for sequential reading.
For the Rewrite/ Delete operations the records must be read at first.
117
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
118
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Relative Files
FILE CONTROL
SELECT file-name ASSIGN TO Disk
ORGANIZATION IS RELATIVE
RELATIVE KEY data-name-1
RRN indicates the offset of a record from the first record of the file.
Notes:
In relative file Relative Record Number identifies the records of the file. Select
clause should specify RELATIVE KEY.
Value of data-name-1 indicates RRN.
Usage of READ/WRITE/ REWRITE/ DELETE statements, ACCESS modes,
OPEN modes and START verb, are exactly similar to that for sequential files.
119
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Open modes
File Organization
Sequential
A
C
C
E
S
S
S
E
Q
U
E
N
T
I
A
L
R
A
N
D
O
M
M
O
D
E
D
Y
N
A
M
I
C
OPERATIONS I
READ
X
WRITE
REWRITE
START
DELETE
READ
WRITE
REWRITE
I-O
X
Relative
E I
X
X
DELETE
I
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
X
I-O
X
X
X
X
X
START
DELETE
READ
WRITE
REWRITE
START
I-O
X
Indexed
X
X
X
X
X
X
X
X
X
X
X
X
X
120
Copyright
X
X
X
Student Notebook
UNIT 5
TABLE HANDLING
121
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Types of tables
One dimensional table
Two dimensional table
Multidimensional table
Notes:
Obviously declaring 100 data items in W-S section and sorting them becomes
practically impossible.
Tables or Arrays provide the solution to handle situations discussed above.
If volume of data to be processed is large and if they are not stored in files, then
tables are used.
122
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
OCCURS Clause
Specifies number of occurrences or elements of the table.
WORKING-STORAGE SECTION
01 Marks.
02 Mark Table1 OCCURS 10 TIMES PIC 9(2). Valid
02 Mark Table2 PIC 9(2) OCCURS 10 TIMES Valid
02 Mark Table3 PIC 9(2) OCCURS 10TIMES VALUE Invalid
Notes:
OCCURS clause causes setting up of area for holding the table elements.
Following rules must be followed with the usage of OCCURS clause.
1. The Integer must be positive.
2. Clause cannot be specified for an item whose level is 01, 66, 77, 88.
3. Value clause should not be specified with occurs clause.
4. OCCURS clause can be specified for file-section entries for both group
items as well as elementary items.
123
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Subscript
Indicates the position of an element in the table.
PROCEDURE DIVISION
Marks Table (Subscript)
Marks Table (I)
Marks Table (5)
Marks Table (12)
Parentheses required
Notes:
Subscript can be a COBOL variable or a literal. Value of subscript must not
exceed the range of no. of occurrences specified by OCCURS clause.
If OCCURS clause is specified for a group items subscript should be
specified for all elementary items of that group.
Subscript should be specified for only data items defined with OCCURS
clause, whenever used in procedure division.
124
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
INDEXING
An index-name is an identifier that becomes associated with a
particular table. The value in an index is the displacement from
the beginning of the table based upon the length of the table
element.
An index-name may appear on an OCCURS clause, e.g.
01
TABLE-OF-MONTHS.
02
MONTHS
PIC
OCCURS
X(10)
12 TIMES.
INDEXED BY NDX.
125
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
XYZ(1)
Notes:
126
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Student (3 5)
Marks (3 5)
Notes:
Two dimension tables are used most frequently in applications. Consider for
examples, 10 Students of a class appeared for 8 subjects in their annual exams and
you need to code a program to store and retrieve the data.
Data includes names of all the students, marks and names of corresponding
subjects.
To store the marks of n subjects of one student, one dimension table serves the
purpose. If number of students is more than one than for each student there
OCCURS n subjects and marks. Next foil shows the W-S declarations for this
example.
127
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Multidimensional Table
Each OCCURS clause adds a dimension in nested occurs.
Ex:
01
Multidimensional.
02 First-dim
02 Second-dim
05 Second
05 Third-dim
10 Third
OCCURS 10 TIMES
OCCURS 5 TIMES.
PIC A.
OCCURS 10 TIMES
PIC 5.
PIC X.
Notes:
COBOL supports multidimensional tables up to 7 levels.
128
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Table-Sorting
Use Sorting techniques to sort a table.
E.g Bubble sort
PERFORM
VARYING I FROM 1 BY 1 UNTIL I = N
PERFORM J FROM I BY 1 UNTIL J > N
IF A[I] > A[J]
MOVE A[I] TO TEMP
MOVE A[J] TO A[I]
MOVE TEMP TO A[J]
END-IF
END-PERFORM
END-PERFORM.
I and J are used as subscripts for comparing elements of the table.
Notes:
Sorting is the process of arranging the elements of table in order. Searching for a
particular element of the sorted table, requires less time when compared to
searching from an unsorted table.
SORT verb available in COBOL is limited to File sorting.
129
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
SET Verb
SET verb initializes and / or changes the value of index.
E.g. :
SET K To 1
SET K UP BY 2
SET K DOWN BY 2
K is initialized to 1
Value increment by step of 2.
Decrements by step of 2
Notes:
Even though indexes assume the displacement values for table elements internally,
programmer sets the value of an index by specifying the position of an element.
This means an index indicates the position of an element in the table similar to
subscript, but internally it is processed in a different manner, but more efficient.
130
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
SEARCH Verb
Searches for a particular value in the table, which has an index.
SEARCH Table name AT END statement
WHEN condition statement
Ex : SET K TO 1
SEARCH table-name AT END DISPLAY not found.
WHEN field-1 = element (K) DISPLAY element (K).
Notes:
In the above example, field-1 contains the required value to be searched for in the
table, More than one condition can be checked, with more than one WHEN
clause.
All valid arithmetic operators can be used.
This form of search statement is called serial search.
131
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Binary Search
Searches the table previously sorted, by splitting the table. Faster than
serial search
Only one WHEN clause is allowed.
SEARCH ALL Table-Name
.
WHEN .
other clauses
remain same.
Notes:
Before applying SEARCH ALL clause the table must be sorted.
SEARCH ALL causes the table to split into two halves. Then it determines which
half of the table contains the required value by comparing it to the last element of
the first half and first element of the second half.
Again the selected half-table splits and continues and so on until the value is
located.
132
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
133
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
DATA DIVISION
WORKING-STORAGE SECTION.
01
WORK-AREA
05
MORE DATA
PIC A VALUE Y.
88
NO-MORE-DATA VALUE N.
01
TEMPERATURE-ARRAY.
05
DAY-IN-THE-WEEK
OCCURS
24 TIMES.
10
HOURS-IN-THE-DAY OCCURS
24 TIMES.
15
DEGREE-TEMP
PIC S9(3).
77
DAY-OF-THE-WEEK
PIC 9.
77
TIME-OF-THE-DAY
PIC 99.
77
HOUR-COUNT
PIC 99.
77
DAY-COUNT
PIC 9.
77
TOT-TEMP
PIC S999.
77
AVERAGE-TEMP PIC S999.
PROCEDURE DIVISION.
100-MAIN-PARA.
PERFORM 200-DATA-ACCP-RTN UNTIL NO-MORE-DATA.
PERFORM 300-DATA-DISP-RTN.
STOP RUN.
200-DATA-ACCP-RTN.
DISPLAY Day of the Week : 1-Sunday.7-Saturday :
ACCEPT DAY-OF-THE-WEEK.
DISPLAY Time of the Day during Data Collection :
ACCEPT TIME-OF-THE- WEEK.
DISPLAY Temperature
ACCEPT DEGREE-TEMP (DAY-OF-THE-WEEK, TIME-OF-THEDAY).
DISPLAY Anymore (Y/N):
ACCEPT MORE-DATA.
300-DATA-DISP-RTN.
PERFORM VARYING
DAY-COUNT FROM 1 BY 1
UNTIL DAY-COUNT > 7.
PERFORM VARYING
DAY-COUNT FROM 1 BY 1
UNTIL HOUR-COUNT > 24.
DISPLAY DAY : , DAY-COUNT, HOUR: ,
HOUR-COUNT, TEMP : ,
DEGREE-TEMP ( DAY-COUNT, HOUR-COUNT )
ADD DEGREE-TEMP ( DAY-COUNT, HOUR-COUNT )
TO TOT-TEMP.
COMPUTE AVERAGE-TEMP = TOT-TEMP / 168.
DISPLAY Weeks Average Temperature Is : ,
AVERAGE-TEMP.
135
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Student Notebook
SET D1 UP BY 1
END-IF
ELSE
SET Y1 UP BY 1.
DISPLAY ANYMORE.
ACCEPT ANYMORE
137
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
UNIT 6
Library Services
138
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
COPY Statement
The COPY statement is a library statement that places prewritten text
in a COBOL program
Each COPY must be terminated by a period
If library-name is omitted, then SYSLIB is assumed
Format:
>>____COPY____
____________________>
|_
_____text-name___
literal
__|
_____
_________________________
Notes:
SUPRRESS means that the imbedded text will not be printed in the source
program listing
COPY requires the LIB compiler option to be in effect
The COPY statement is a library statement that places prewritten text in a COBOL
program.
Prewritten source program entries can be included in a source program at compile
time. Thus, an installation can use standard file descriptions, record descriptions,
or procedures without recording them. These entries and procedures can then be
saved in user-created libraries; they can then be included in the source program by
means of the COPY statement.
139
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
140
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Nested COPY
COPY FILEA.
FILEA contains:
01 FILEA.
05 NAME PIC X(40).
COPY ADDRESS.
05 DATA
PIC X(100).
ADDRESS contains:
05 STREET PIC X(40).
05 CITY
PIC X(20).
05 STATE PIC X(02).
05 ZIP
PIC X(09).
Compile produces:
01 FILEA.
05
NAME
PIC X(40).
05
STREET PIC X(40).
05
CITY
PIC X(20).
05
STATE
PIC X(02).
05
ZIP
PIC X(09).
05
DATA
PIC X(100).
Notes:
COBOL allows nested COPY statements.
Nested COPY statements cannot contain the REPLACING phrase.
A COPY statement can appear in the source program anywhere a character string
or a separator can appear. As an IBM extension, COPY statements can be nested.
However, nested COPY statements cannot contain the REPLACING phrase, and a
COPY statement with the REPLACING phrase cannot contain nested COPY
statements.
A COPY statement cannot cause recursion. That is, a COPY member can be
named only once in a set of nested COPY statements until the end-of-file for that
COPY member is reached.
141
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
COPY REPLACING
To change some, or all, of the names in the library (COPYed) text, the
programmer can use the REPLACING option.
The text in the library is unchanged.
COPY PAYLIB REPLACING
FLDA BY PAY-RECORD
FLDA BY HRLY-RATE
FLDA BY HRS-WORKD.
LIBRARY TEXT
01 FLDA.
02 FLDB PIC 999V99.
02 FLDC PIC 999V99.
SOURCE PROGRAM
01 PAY-RECORD.
02 HRLY-RATE PIC 999V99.
02 HRS-WORKD PIC 999V99.
In the discussion that follows, each operand can consist of one of the following:
Pseudo-text
An identifier
A literal
A COBOL word
Function identifier
142
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
COPY Pseudo-Text
To change only part of the data-name(s) in the library text, the
programmer can use the REPLACING option with the standard
pseudo-text delimiters (==)
COPY PAYLIB REPLACING
= = : PFFX: = =
BY
= = PAY = =.
LIBRAR TEXT
SOURCE PROGRAM
01 :PFFX:.
02 :PFFX:-RTE
PIC 999V99.
02 :PFFX: - HRS
PIC 999V99.
01 PAY.
02 PAY-RTE
PIC 999V99.
02 PAY-HRS
PIC 999V99.
Notes :
Pseudo-text A sequence of character-strings and/or separators bounded by, but not
including, pseudo-text-1 delimiters (= =). Both characters of each pseudo-text-1
delimiter must appear on one line; however, character-strings within pseudo-text-1
can be continued.
Any individual character-string within pseudo-text-1 can be up to 322 characters
long.
Pseudo-text-1 cannot be null, nor can it consist solely of the space character,
separator comma, separator semicolon, and/or of comment lines. Beginning and
ending blanks are not included in the text comparison process. Embedded blanks
are used in the text comparison process to indicate multiple text words.
Pseudo-text must not contain the word COPY.
143
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
REPLACE Pseudo-test
Replace can be applied to the entire program, including text
introduced through COPY members
Replace action starts at a the REPLACE statement and continues
until:
- Another REPLACE statement
- REPLACE OFF statement
- End of source program
REPLACE statements are processed by the compiler after any COPY
statements are processed
144
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
UNIT 7
CHARACTER HANDLING
145
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
STRING
Two or more fields can be concatenated or string in to single-field.
STRING
DELIMITED BY
DELIMITED BY
INTO id 7
Notes:
Delimited by clause specifies how the fields are concatenated, Its usage is described in
following foils.
With pointer option if used gives the total no. of characters in the concatenated field (id7)
If the length of id-7 is not enough to hold the transferred characters, the statement after
on overflow option is executed.
One STRING statement can be written instead of a series of MOVE statements.
The following rules should be followed when this verb is used.
(i). This statement is used to concatenate one or more strings into one by placing them
side by side.
(ii). Sending strings may be alphanumeric literals, figurative constants or identifiers
with usage DISPLAY.
(iii). The receiving string, i.e., identifier- 7 must also be with usage DISPLAY.
146
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
STRING example :
77
77
77
77
77
Counter.
RESULT
Content if id-4: CONCATENATED.
The following example shows usage of size option.
77 F 1
PIC X(7) VALUE HIGHTEC
77 F 2
PIC X(7) VALUE SPACES.
STRING MAIN DELIMITED BY SIZE F 1 INTO F 2.
Guess the result.
147
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
VALUE SPACES.
VALUE SPACES.
VALUE SPACES.
PROCEDURE DIVISION.
100-MAIN-PARA.
MOVE RAJA TO FIRST-NAME.
MOVE ROMOHAN TO MIDDLE-NAME.
MOVE ROY TO LAST-NAME.
STRING FIRST-NAME,MIDDLE-NAME,LAST-NAME
DELIMITTED BY SPACE INTO FULL-NAME.
148
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
UNSTRING Statement
The UNSTRING statement is used to split a single data item into several
data items
Format:
>>__UNSTRING___identifier1___________________________________________________________>
>__ _____________________________________________________________________________ ___>
|_DELIMITED___ _____ ___ _____ ___ __identifier-2__ ____ __________________________|
|_BY_|
|_ALL_| |_literal-1______|
| <_____________________ |
|__OR_ ____ __ identifier-3_ _| |
|_ALL_| |_literal-2__|
>___INTO____________________________________________________________________________>
<____________________________________________________________________________
>____identifier-4__ _____________________________ ____ ___________________________ __| __>
|_DELIMITER__ ___ identifier-5 _|
|_COUNT__ ____ identifier-6___|
|_IN_|
|_IN__|
>___ ___________________________ ____ _______________________________ ______________>
|_ ____ _POINTER__identifier-7_|
|_TALLYING___ ____ __identifier-8_|
|_WITH_|
|_IN_|
>__ _____________________________________________ _________________________________>
|_ _____ ___OVERFLOW__imperative-statement-1___|
|_WITH_|
>___ __________________________________________________ ___ _________________ _______>
|_ NOT__ _____ __OVERFLOW__imperative-statement_2_|
|_END-UNSTRING_|
|_ON_|
Notes:
The UNSTRING statement causes contiguous data in a sending field to be separated and
placed into multiple receiving fields.
One UNSTRING statement can take the place of a series of MOVE statements, except
that evaluation or calculation of certain elements is performed only once, at the beginning
of the execution of the UNSTRING statement.
When the TALLYING phrase is specified, the field-count field contains a value equal to
the initial value, plus the number of data receiving areas acted upon.
149
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
VALUE SPACES.
VALUE SPACES.
VALUE SPACES.
PROCEDURE DIVISION.
100-MAIN-PARA.
MOVE RAJA ROMOHAN ROY TO FULL-NAME.
UNSTRING FULL-NAME DELIMITTED BY SPACE INTO
FIRST-NAME, MIDDLE-NAME,LAST-NAME.
150
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
EXAMINE Statement
EXAMINE
Identifier TALLYINY
EXAMINE
Literal - 3
Identifier REPLACING
ALL
LEADING
UNTIL FIRST
Literal - 1
ALL
LEADING
Literal - 2 BY
UNTIL FIRST
EXAMINE
Identifier TALLYINY
ALL
LEADING
UNTIL FIRST
Literal - 4
REPLACING BY Literal - 5
Notes:
This verb is used to scan a string to find the number of occurences of a given character in
it. In addition, the Verb can also be use to replace some or all occurrences of the said
character by another character.
Eg :
Let us consider the following DATA DIVISION entry
77 A PIC X(5) VALUE IS PPRIP.
Now the statement
EXAMINE A TALLYING ALL P.
Will store 3 in the TALLY register as there are altogether three Ps in the string.
However the statement
EXAMINE A TALLYING LEADING P
Will store 2 in the TALLY , Since there are only Two leading Ps.
The statement
EXAMINE A TALLYING UNTIL FIRST I.
Will store 3 in the TALLY as there are only Three characters before the character
I. It may be noted here that if a particular character is not found, TALLY is set to Zero,
When the ALL or LEADING phrase is used. For eg. In the statement
151
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
152
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
__
_identifier-
153
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
Notes:
TALLING Phrase counts the occurrence of a specific character (alphabetic, numeric, or
special character) in a data item.
Identifier-1:
Identifier-1 is the inspected item and can be any of the following:
An alphanumeric data item
An numeric data item with USAGE DISPLAY
An external floating point item
Identifier-2
Is the count field, and must be an elementary integer item defined without the symbol P
in its PICTURE character-string
You must initialize identifier-2 before execution of the INSPECT statement begins.
Identifier-3 or literal-1
Is the tallying field (the item whose occurrences will be tallied).
Eg:
PROCEDURE DIVISION.
INSPECT HELLO TALLYING TALLY-COUNT FOR ALL A
Let the picture of HELLO be X(20) and suppose its content before the execution of the
above statement is as follows :
APARNAbKUMARIbAMMAbb
If picture of TALLY-COUNT is 9(2) and originally contains 08, then after the execution
of the statement, TALLY-COUNT will contain 14, as there are a total of 6 As in
HELLO.
If ALL in the statement is changed to LEADING , TALLY-COUNT will be increased to
9, as there is only one leading A. if CHARACTERS is specified instead of ALL A,
TALLY-COUNT will be increased to 28 as there are a total of 20 characters in HE LLO.
154
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
<___________________________________________________________________________
__
identifier-4_
155
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
REPLACING Phrase
-identifier-3 or literal-1
Is the subject field (the item whose occurrences are replaced).
Identifier-3 can be:
An elementary alphanumeric data item
A numeric data item with USAGE DISPLAY
An external floating point item
Literal-1 must be non-numeric, and can be any figurative constant that does not begin
with the word ALL. If literal-1 is a figurative constant, it is considered to be a 1-character
nonnumeric literal.
Identifier-5 or literal-3
Is the substitution field
Identifier-5 can be:
An elementary alphanumeric data item
A numeric data item with USAGE DISPLAY
An external floating point item
Literal-3 must be nonnumeric, and can be any figurative constant that does not begin with
the word ALL.
The following replacement rules apply:
When the subject field is a figurative constant, the single-character substitution
field(which must be 1 character in length) replaces each character in the inspected item
equivalent to the figurative constant.
When the substitution field is a figurative constant, the substitution field replaces each
non-overlapping occurrence of the subject field in the inspected item.
When the subject and substitution fields are character-strings, the character-string
specified in the substitution field replaces each non-overlapping occurrence of the subject
field in the inspected item
156
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
UNIT 8
SORT / MERGE
157
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
SORT/MERGE
SORT Statement
MERGE Statement
SORT PROCEDURES
RELEASE/RETURN Statements
158
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
SORT Statement
The SORT statement accepts records, sorts them according to
specified keys, and makes the sorted results available for further
processing.
Format 1:
SORT file-name-1 ON ASCENDING/DESCENDING KEY data-name-1
USING file-name-2 GIVING file-name-3.
Format 2:
SORT file-name-1 ON ASCENDING/DESCENDING KEY data-name-1
INPUT PROCEDURE IS Procedure-name-1[THRU Procredure-name-2]
USING file-name-2 GIVING file-name-3.
Format 3:
SORT file-name-1 ON ASCENDING/DESCENDING KEY data-name-1
INPUT PROCEDURE IS Procedure-name-1[THRU Procredure-name-2]
USING file-name-2
OUTPUT PROCEDURE IS Procedure-name-3[THRU Procredure-name-4]
GIVING file-name-3.
Notes:
The SORT Statement accepts records from one or more files. Sorts them according to the
specified key(s), and makes the sorted records available either through an OUTPUT
PROCEDURE or in an output file. The SORT Statement can appear any where in the
procedure division except in the declarative portion.
File-name-1
The name given in the SD entry that describes the records to be sorted.
No pair of file-names in a SORT statement can be specified in the same SAME SORT
AREA , or Same SORT-MERGE AREA clause. File-names associated with the giving
clause (file-name-3) cannot be specified in the SAME AREA clause.
File-names associated with the giving clause (file-name-3) can be specified in the
SAME AREA clause.
159
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
160
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
MERGE Statement
The MERGE statement combines two or more identically sequenced
files(that is, files that have already been sorted according to an identical
set of ascending/descending keys) on one or more keys and makes
records available in merged order to an output procedure or output file.
Format:
>>___MERGE__file-name-1____ _____ ___ __ASCENDING__ ___ _______ __data-name-1__| __|_>
>___
_____________________________________________________
2_______>
|__ ________________ __SEQUENCE__ ____ _alphabet-name-1_|
|_COLLATING____|
|_IS_|
__USING__file-name-
<__________________
>_____file-name-3__|___________________________________________________________________>
>___ __OUTPUT PROCEDURE___ ___ __procedure-name-1__ __________________________ _ _><
|_
|_IS_|
|_ _ THROUGH_ procedure-name-2_|
_|
|
|_THRU____|
|
|
<________________
|
|_ GIVING___file-name-4__| _________________________________________________________|
Notes:
The MERGE statement combines two or more identically sequenced files(that is, files
that have already been sorted according to an identical set of ascending/descending keys)
on one or more keys and makes records available in merged order to an output procedure
or output file.
A MERGE statement can appear anywhere in the Procedure Division except in a
Declarative Section.
The file names given must be in the SD entry.
When the MERGE statement is executed, all records contained in file-name-2, file-name3,., are accepted by the merge program and then merged according to the key(s)
specified.
161
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
SORT PROCEDURES
Procedures can add, delete, alter or edit the records
With SORT .INPUT PROCEDURE you can specify processing
to be performed on the records before they are sorted
With SORT ..OUTPUT PROCEDURE you can specify
processing to be performed on the records after they are sorted
In an input procedure the RELEASE statement is used to place a
record into the file to be sorted
In an output procedure the RETURN statement is used to extract
a record from the sorted file
Notes:
INPUT PROCEDURE Phrase
This phrase specifies the name of a procedure that is to select or modify input
records before the sorting operation begins.
The input procedure can consist of any procedure needed to select, modify or copy the
records that are made available one at a time by the RELEASE statement to the file
referenced by file-name-1. The range includes all statements that are executed as the
result of a transfer of control by CALL, EXIT, GO TO, and PERFORM statements in the
range of the input procedure, as well as all statements in declarative procedures that Sare
executed as a result of the execution of statements in the range of the input procedure.
The range of the input procedure must not cause the execution of any MERGE,
RETURN, or SORT statement.
If an input procedure is specified, control is passed to the input procedure before the file
referenced by file-name-1 is sequenced by the SORT statement. The compiler inserts a
return mechanism at the end of the last statement in the input procedure. When control
passes the last statement in the input procedure, the records that have released to the file
referenced by file-name-1 are sorted.
OUTPUT PROCEDURE Phrase
This phrase specifies the name of a procedure that is to select or modify output
records from the sorting operation.
162
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
The output procedure can consist of any procedure needed to select, modify, or copy the
records that are made available one at a time by the RETURN statement in sorted order
from the file referenced by file-name-1. The range includes all statements that are
executed as the result of a transfer of control by CALL, EXIT, GO TO and PERFORM
statements in the range of the output procedure. The range also includes all statements in
declarative procedures that are executed as a result of the execution of statements in the
range of the output procedure. The range of the output procedure must not cause the
execution of any MERGE, RELEASE or SORT statement.
If an output procedure is specified, control passes to it after the file referenced by filename-1 has been sequenced by the SORT statement. The compiler inserts a return
mechanism at the end of the last statement in the output procedure and when control
passes the last statement in the output procedure, the return mechanism provides the
termination of the sort and then passes control to the next executable statement after the
SORT statement. Before entering the output procedure, the sort procedure reaches a point
at which it can select the next record in sorted order when requested. The RETURN
statements in the output procedure are the requests for the next record.
163
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
RELEASE Statement
The RELEASE statement is only used within the INPUT
PROCEDURE of a SORT
The RELEASE statement makes the contents of record-name-1
available to the initial phase of the SORT process
Upon completion of the INPUT PROCEDURE, the sort file
consists of all records placed there by the RELEASE statement
Format:
____RELEASE____record-name-1____
____________________><
_____________________________
|_FROM___identifier-1___________|
Notes:
The RELEASE statement transfers records from an input/output area to the initial phase
of a sorting operation.
The RELEASE statement can only be used within the range of an INPUT PROCEDURE
associated with a SORT statement.
Within an INPUT PROCEDURE, at least one RELEASE statement must be specified.
164
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
RETURN Statement
The RETURN statement is used only within the OUTPUT
PROCEDURE of a SORT or MERGE
The RETURN statement acts like a READ and makes the next
record from the sort/merge processing available to the application
The AT END clause must be specified
Format:
>>___RETURN___file-name-1___ ______________ ____ _____________________________
_______>
|_ RECORD __|
|_INTO___identifier-1___________|
>___
_____
1____________________________________________>
|_ AT _|
____END___imperative-statement-
Notes:
The RETURN statement transfers records from the final phase of a sorting or merging
operation to an OUTPUT PROCEDURE.
The RETURN statement can be used only within the range of an OUTPUT
PROCEDURE associated with a SORT or MERGE statement.
165
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
EXAMPLE : SORT
IDENTIFICATION DIVISION.
PROGRAM-ID. SORTING.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE ASSIGN TO DD1
ORGANIZATION IS SEQUENTIAL.
SELECT WORK-FILE ASSIGN TO DD2
ORGANIZATION IS SEQUENTIAL.
SELECT OUT-FILE ASSIGN TO DD3
ORGANIZATION IS SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD IN-FILE.
01
IN-REC.
02 NUM
PIC
X(2).
02 NAME
PIC
X(10).
02 ADDR
PIC
X(10).
02 FILLER
PIC
X(58).
SD WORK-FILE.
01
WORK-REC.
02 WNUM
PIC
X(2).
02 WNAME
PIC
X(10).
02 WADDR
PIC
X(10).
02 FILLER PIC
X(58).
FD OUT-FILE.
01
OUT-REC
PIC
X(80).
WORKING-STORAGE SECTION.
PROCEDURRE DIVISION.
SORT WORK-FILE ON ASCENDING KEY WNUM USING IN-FILE GIVING OUTFILE.
STOP RUN.
166
Copyright
Course materials may not be produced in whole or in part
without the prior written permission.
FLD1
FLD2
FLD3
FLD4
01 GRP1.
05
05
05
01 GRP2.
05
05
05
PIC
PIC
PIC
PIC
X(10).
X(6).
9(8).
9(5).
AAA
BBB
CCC
PIC
X(12).
PIC A(10).
PIC X(10).
AAA
BBB
CCC
PIC
X(12).
PIC A(10).
PIC X(10).
Accept the values for FLD1, FLD2, FLD3, FLD4 through Terminal (I, e JCL). Display the
Results.
3. Write a COBOL Program, to illustrate the REDEFINES & RENAMES Clauses.
Copyright
Course materials may not be produced in whole or in part
without the prior written.
1. Accept the following fields and then perform the ADD, SUBTRACT, MULTIPLY, DIVIDE
and operations on those fields. And also use COMPUTE Verb.
01
01
01
77
77
77
FLD1
FLD2
FLD3
FLD4
FLD5
FLD5
PIC
PIC
PIC
PIC
PIC
PIC
9(2).
9(3).
9(4)V99.
9(5).
9(5)V99.
9(5)V99.
50 units
100 units
250 units
400 units
Rs 1.20/unit
Rs 2.50/unit
Rs 3.50/unit
Rs 5.00/unit
Rs 7.50/unit
Accept the number units consumed by the customer and display the Customer-id, Name,
Address, Number of units consumed and Total price. Use both IF ELSE Statement and
EVALUATE Statement.
PIC
X (6).
168
Copyright
Course materials may not be produced in whole or in part
without the prior written.
EMP-NAME
DESG
AGE
DEPT
PIC
PIC
PIC
PIC
X (15).
X (10).
9 (2).
X (8).
Accept the values for above Data items and write into the file.
2. Enter the records to the file which is created by 1st exercise. Read the records from
that file and write into a new file and also display them. Update any one record.
Copyright
Course materials may not be produced in whole or in part
without the prior written.
3. Accept 12 elements to a single dimensional table and then sort out the elements.
Using SEARCH verb find out the given element.
170
Copyright
Course materials may not be produced in whole or in part
without the prior written.