CBL Material
CBL Material
CBL Material
1
Table of Contents
1. COBOL – AN INTRODUCTION
2. COBOL – AN INTRODUCTION(Cntd)
3.1 DISPLAY
3.2 ACCEPT
3.3 MOVE STATEMENT
3.3.1 NUMERIC MOVE
3.3.2 NON-NUMERIC MOVE
3.4 ARITHMATIC VERB
3.4.1 ADD
3.4.2 SUBTRACT
3.4.3 MULTIPLY
3.4.4 DIVIDE
3.4.5 REMAINDER
3.4.6 ROUNDED
3.5 COMPUTE STATEMENT
4. CONDITIONAL STATEMENT
2
4.2.2 CLASS CONDITION
4.2.3 SIGN CONDITION
4.2.4 NESTED CONDITION
4.3 PERFORM STATEMENT
4.3.1 PERFORM
4.3.2 PERFORM …..TIMES
4.3.3 PERFORM …..UNTIL
4.3.4 PERFORM …..VARYING
4.3.5 NESTED PERFORM
4.3.6 INLINE PERFORM
6. INTRODUCTION TO FILES
7. SEQUENTIAL FILE
8. CONTROL BREAK
8.1 LEVEL – 1
8.2 LEVEL – 2
3
11.2 TABLE SEARCH
11.2.1 LINEAR SEARCH
11.2.2 BINARY SEARCH
12.1 INTRODUCTION
12.2 CREATION OF AN INDEX FILE
12.3 ACCESSING INDEX FILE
12.3.1 SEQUENTIAL ACCESS
12.3.2 RANDOM ACCESS
12.3.3 DYNAMIC ACCESS
12.4 ALTERNATE KEYS
12.5 UPDATION OF INDEX FILE
12.6 DELETION OF INDEX FILE
15. SUB-ROUTINE
15.1 CALL STATEMENT
15.2 CANCEL & IS INITAL
15.2 CALL BY REFERENCE
15.3 CALL BY CONTENT
15.4 STATIC & DYNAMIC CALL
4
Day-Wise Schedule
Day 1
Introduction
Programming Structure, Coding Formats
Constants, Identifiers, Figurative Constants
Identification Division.
Environment Division
Data Division
Level numbers
Elementary and group data names
Picture clauses [Edit and computation]
Value clause, Usage clause, Redefines clause, Renames clause
Procedure Division
Input and Output verbs
Simple Programs
Day 2
Arithmetic verbs
Move
Numeric, Non numeric, Group
Control verbs
If, Condition names, Goto, Perform
Sample Programs
Day 3
File Organization Concepts
Sequential Files
Creation
Access
Query
Copy verb
Day 4
Trapping Runtime errors
Simple reports
Reports with page totals and net totals
Day 5
INDEX FILES
Creation [Record key and alternate keys]
Conversion of sequential to indexed files
Random and sequential access
Edit and deleted records in indexed files
File maintenance
Report generation with index files
Day 6
Start verb
Single level control break
Double level control break
Day 7
Master, Transaction files
5
File update
Update existing master
Create new master
Day 8
SUB ROUTINE
Call verb
Linkage Section
CALL BY REFERENCE, CALL BY CONTENT
STATIC CALL, DYNAMIC CALL
Cancel, Initial Program
Character Handling
INSPECT, EXAMINE, STRING, UNSTRING
Day 9
SORT, MERGE
INPUT PROCEDURE, OUTPUT PROCEDURE
RELEASE, RETURN
Day10
TABLE HANDLING
One and two dimensional arrays
Redefines clause
Files and tables
Search a table
Linear & Binary Search
6
CHAPTER – 1
INTRODUCTION TO COBOL
DIVISIONS IN COBOL
IDENTIFICATION DIVISION
ENVIRONMENT DIVISION
DATA DIVISION
PROCEDURE DIVISION
7
1.Introduction
COBOL was developed in 1959 by a group called the CODASYL Committee. CODASYL is an abbreviation for
Conference on Data Systems Languages. This committee included representatives from academia, user groups, and
computer manufacturers. The ultimate objective of this committee was to develop a standard business-oriented
language for which all major manufacturers would provide compilers. The Department of Defense convened this
conference since it, as well as other government agencies, was particularly dissatisfied with the lack of standards in
the computing field.
Features of COBOL
1. Business-Oriented Language
As a business-oriented language COBOL is designed specifically for commercial applications, such as
payroll and inventory, that typically operate on a large volume of data.
2. A Standard Language
COBOL compilers are available for most computers. T
he same COBOL program may be compiled and run on a variety of different machines.
The universality of COBOL allows computer uses greater flexibility than they would have with many other
languages. A company is free to acquire different brands of computers while using a single programming language.
Similarly, conversion from one model computer to a more advanced or newer one presents no great problem as long
as there is a COBOL compiler for each model.
3. An English-like Language
COBOL is an English-like language. All instructions can be coded using English words rather than
complex codes. To add two numbers together, for example, we use the word ADD. Similarly, the rules for
programming in COBOL conform to many of the rules for writing in English, making it a relatively simple language
to learn.
DIGITS : 0123456789
8
CODING A COBOL PROGRAM
A COBOL program needs to be coded following the below mentioned coding rules (COBOL Coding Sheet)
Note : Division, Section, Paragraph Names and Sentences must end with a period followed by at least one space.
A. All divisions, sections and paragraph names must start in AREA A. They may extend into AREA B but
should end with a fullstop. Nothing else may be written on these lines.
B. All sentences and entries must be written in AREA B from any position and end in full-stops. Every
sentence must begin on a new line.
C. All operators (Arithmetic, Relational and Logical) must be preceded and followed by atleast one blank
space.
D. Parenthesis may be used in arithmetic expressions. There should be no blank spaces immediately after left
parentheses and before the right parentheses.
E. In most of the cases, COBOL defines the punctuation symbols to be used. The Period, comma and
semicolon, whenever used should not be preceded by a blank, but must be followed by at least one blank.
F. The statements within a sentence or clauses within an entry may be separated by blanks.
A clause consists of characters and words and is used to specify an attribute of an entry.
A statement is a syntactically valid combination of words and characters written in the PROCEDURE
DIVISION of a COBOL program and beginning with a verb.
A sentence is a sequence of one or more statements, the last of which is terminated by a period followed by
a space.
A division consists of one or more paragraphs or sections. Every COBOL program consists of four
divisions in the following order : IDENTIFICATION DIVISION, ENVIRONMENT DIVISION, DATA
DIVISION and PROCEDURE DIVISION.
9
Divisions
IDENTIFICATION DIVISION
The IDENTIFICATION DIVISION is the first division of a COBOL program. It supplies the
administrative information about the program to others who may read or use the program(Only for
documentation Purpose). The IDENTIFICATION DIVISION is divided into the following paragraphs :
PROGRAM-ID. Program-name.
Used to specify the program name. Use names of eight characters or less, letter and digits only, because
such names are accepted on all systems.
AUTHOR. author-name.
Used to specify the programmer’s name.
DATE-WRITTEN. Date.
Specify the date the program was coded.
DATE-COMPILED. Date.
Can be coded with an actual date. But if it is coded without a date entry, the compiler itself will
automatically fill in the actual date of compilation.
ENVIRONMENT DIVISION
The ENVIRONMENT DIVISION is the only machine-dependent division of a COBOL program. It
describes the facilities used by a program from the environment of the computer system on which it will be
executed. It is composed of two sections:
CONFIGURATION SECTION.
SOURCE-COMPUTER. Computer.
Computer used for compiling the program.
OBJECT-COMPUTER. Computer.
Computer used for executing the program.
INPUT-OUTPUT SECTION.
Supplies information concerning the input and output devices used. This section is required only if the
program uses files or prints reports.
FILE-CONTROL.
Describes the device type, organization, access mode, file status..Etc about the various files handled in the
program
DATA DIVISION
The DATA DIVISION defines and describes fields, records, and files in storage. Commonly, it consists of
the following sections :
FILE SECTION.
Defines all input and output files.
WORKING-STORAGE SECTION.
Reserves storage for fields not part of input or output but nonetheless required for processing. These
include constants, end-of-file indicators, and work areas.
LINKAGE SECTION.
Used to identify items that will be passed to the called program from the calling program and vice-versa.
10
PROCEDURE DIVISION
The PROCEDURE DIVISION is divided into paragraphs. Each paragraph is an independent module or
routine that includes a series of instructions designed to perform specific set of operations. Paragraph
names are coined by the programmer following the rules for forming data-names.
A PROCEDURE DIVISION may also consist of several user-defined sections. A section may contain
several paragraphs.
IDENTIFICATION DIVISION.
PROGRAM-ID. MYFIRST.
AUTHOR. DASH.
DATE-WRITTEN. 08/10/2006
DATE-COMPILED. 08/10/2006.
INSTALLATION. KASSHKU.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM-PC.
OBJECT-COMPUTER. IBM-PC.
INPUT-OUTPUT SECTION.
PROCEDURE DIVISION.
0000-MAINLINE.
DISPLAY ‘ MY FIRST COBOL PROGRAM ‘.
STOP RUN.
11
A sequence of continuous characters from the character set can form a word. There are two types of COBOL words,
reserved words and user-defined words.
Reserved words are words that are defined both syntactically and semantically by the COBOL language. The
programmer cannot use any of these words except in the form specified by the language.
User-defined words (data names) are words supplied by the programmer in order to satisfy the format of a clause or
statement in the language.
1 to 30 characters.
Letters, digits, and hyphens (-) only.
No embedded blanks.
At least one alphabetic character.
May not begin or end with a hyphen.
No COBOL reserved words such as DATA, DIVISION, etc.
Types of data
VARIABLE
Data entered by the user at run-time.
CONSTANT
Data required for processing that is not dependent on the input to the system. There are 3 types of literals.
1. NUMERIC LITERALS
When a array of numbers entered following certain rules, they are known as NUMERIC LITERAL. A
numeric literal is a constant used primarily for arithmetic operations and calculations.
2. NON-NUMERIC LITERALS
String of characters grouped together is known as NON-NUMERIC literal. A nonnumeric literal is a
constant that is used in the PROCEDURE DIVISION for all operations except arithmetic. Any literal
enclosed within quotes are called Non Numeric Literals.
12
c) Any character permitted in the COBOL character set may be used except the quotation mark.
3. FIGURATIVE CONSTANTS
A figurative constant is a COBOL reserved word that has special significance to the compiler.
Level numbers
The record description specifies the format of a record . Record description entries indicate :
1. The items or fields to appear in the record
2. The order in which the fields appear
3. How these fields are related to one another
Data is grouped in COBOL using the concept of a level. Records are considered the highest level of data in a file,
and are coded on the 01 level. There can be only one data-name at the 01 level for each record, as it is the all-
inclusive data-name. A field of data within the record is coded on a level subordinate to 01, i.e 02, 03 and so on up to
49. Any level number between 02 and 49 may be used to describe fields within a record.
Eg : 01 CUSTOMER-ADDRESS
02 NAME.
03 FIRST-NAME
03 LAST-NAME
02 STREET
03 S-NUMBER
03 STREET-NM
02 ZIP-CODE
13
COBOL allows a special level – 77 which generally used for data names which are independent and do not have any
sub group but an elementary item by itself. They are used only for those data names which are not part of any record
in a file i.e used in the working-storage section.
Group items are defined by a level number and a name, which is followed by a period. Elementary items must be
described with a PICTURE (or PIC, for short) clause.
Defines and describes all data storage fields required for Processing (Including constants).
These fields are not part of any input/output file.
14
Tables etc.
The ACCEPT and DISPLAY verbs are used for input and output in conjunction with storage fields that are not part
of any files.
15
ACCEPT STATEMENT
The instruction ACCEPT identifier enable the user to enter input data directly from a keyboard at the time of
program execution or from the JCL submission. The program will halt for the user to enter the value or supplied
from the SYSIN of JCL while executing that instruction. On entering the value, it occupies that memory location
allocated for that data name.
Eg ACCEPT MY-NAME.
DISPLAY STATEMENT
DISPLAY is used for displaying the contents of the variables or the literals on the screen. The
Display verb can be followed by data names, Literals, or a combination of both each being
separated at least by a single space.
Eg DISPLAY MY-NAME.
DISPLAY “HELLO”.
STOP RUN
The verb STOP RUN terminates program execution. A program may contain more than one STOP RUN statement,
depending on the logic of the program. STOP RUN signifies the logical end of the program.
Eg IDENTIFICATION DIVISION.
PROGRAM-ID. SAMPLE2.
*This program accepts the user’s name and displays a message.
AUTHOR. XYZ.
DATE-WRITTEN. 1-JUN-2000.
DATE-COMPILED.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM-PC.
OBJECT-COMPUTER. IBM-PC.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 USER-NAME PIC X(15).
PROCEDURE DIVISION.
0000-MAIN.
DISPLAY “Enter your name : “.
ACCEPT USER-NAME.
16
MOVE statements
A value can be moved from one storage location to another by the move statement. Contents of receiving field are
changed(Over written) after the operation.
Verb
SENDING
RECEIVING FIELD
The contents of AMT-In will be copied to the second field, AMT-OUT, as a result of the MOVE operation.
Eg 4.1:
MOVE TOTAL TO PRINT-TOTAL
MOVE ‘INVALID’ TO MESSAGE.
MOVE ZEROS TO TOTAL
Types of MOVE :
The MOVE statement can be categorized based on the receiving field : numeric MOVEs and non-numeric MOVEs.
Numeric MOVE
A numeric move is one in which a numeric field or literal is moved to a numeric receiving field.
When Sending and Receiving fields have the same PIC clauses
If the PIC clauses of both fields are identical, the contents of identifier-2 will be replaced with the contents of
identifier-1 and the sending field will be unchanged.
Eg 4.2:
05 AMT-IN PIC 999 VALUE 123.
05 AMT-OUT PIC 9(4) VALUE 4567.
MOVE AMT-IN TO AMT-OUT.
Result :
AMT-OUT = 0123
Avoiding truncation
In a numeric move, if the receiving field has fewer integer positions than the sending the field, the most significant
digits will be truncated.
17
MOVE AMT-IN TO AMT-OUT.
Result AMT-OUT = 23
Eg 4.4:
a. When receiving field has more decimal positions than the sending field
05 AMT-IN PIC 99V99 VALUE 12.34.
05 AMT-OUT PIC 99V999 VALUE 56.789.
Result :
AMT-OUT = 12.340
b. When receiving field has fewer decimal positions than the sending field
05 AMT-IN PIC V99 VALUE.34.
05 AMT-OUT PIC V9 VALUE .5.
Result :
AMT-OUT = .3
Note : The same rules apply to numeric literals moved to numeric fields
Non-numeric MOVE
A non-numeric MOVE operation occurs in the following cases :
1. Moving an alphanumeric or alphabetic field, defined by a PICTURE of X’s or A’s, to another
alphanumeric or alphabetic field.
2. Moving a non-numeric literal to an alphanumeric or alphabetic field.
3. Moving a numeric field or numeric literal to an alphanumeric field or to any group item.
Rule :
In a non-numeric move, data is transmitted from the sending field to the receiving field from left to right.
Low-order or rightmost positions of the receiving field that are not replaced with sending field characters are filled
with spaces.
Eg 4.4:
a. When receiving field is larger than the sending field
Result :
NAME-OUT = ABCbb
18
b. When receiving field is smaller than the sending field
Result :
NAME-OUT = AB
c. When the sending field is numeric integer and the receiving field is non-numeric
Result :
NAME-OUT = 321bb
Result :
NAME-OUT = XYZbb
Result :
NAME-OUT = bbbbb
All group items, even those with numeric subfields, are treated as alphanumeric fields.
Eg 4.5:
05 DATE-OUT.
10 MONTH-OUT PIC 99 .
10 YEAR-OUT PIC 99.
Result :
DATE-OUT = 0194
Result :
19
DATE-OUT = 194b
In the MOVE CORRESPONDING statement, all elementary items within the sending group-item that have the
same names as corresponding elementary items in the receiving group-item will be moved. The same-named fields
in the receiving group-item need not be in any specific order. Any fields of the sending record, that are not matched
by the same-named fields in the receiving record are ignored.
ARITHMETIC VERBS
All the basic arithmetic operations of ADD, SUBTRACT, MULTIPLY, and DIVIDE require that the fields operated
on (1) have numeric PIC clauses and (2) actually have numeric data when the program is executed.
In all cases, the resultant field must be an identifier or data-name, not a literal.
ADD statement
The result, or sum, of an ADD operation is always placed in the last field mentioned. The only field that is altered as
a result of the ADD operation is this last field, which is the one directly following the word TO.
When using the TO format in an ADD statement, all the data-names and literals are added together, and the result
placed in the last field specified.
When using the GIVING format, all fields and literals preceding the word GIVING are added together and the sum
is placed in the field following the word GIVING.
Syntax:
ADD value1 value2 TO data-name
ADD value1 TO value2 GIVING data-name1 data-name2….
Result :
EMP-TOTAL = 4100
20
Result :
EMP-TOTAL = 4650
SUBTRACT statement
All fields and literals preceding the word FROM will be added together and the sum subtracted from the field
following the word FROM. The result, or difference, will be placed in this same field if no GIVING option is used.
All other fields will remain unchanged.
Syntax :
SUBTRACT value FROM data-name
SUBTRACT value FROM data-name1 GIVING data-name2
Result :
EMP-BASIC = 4000
Result :
EMP-GROSS = 3900
Result :
EMP-GROSS = 4250
EMP-NET = 3900
MULTIPLY Statements
With MULTIPLY statement specified; only two operands can be multiplied. Always make sure the receiving fields is
large enough to store the result. The preposition used with the MULTIPLY verb is always BY.
a. MULTIPLY 12 BY EMP-GROSS.
Result :
EMP-GROSS = 48000
21
EMP-ANN-SAL=48000
DIVIDE Statements
With DIVIDE statement specified; only two operands can be divided. Always make sure the receiving field is large
enough to store the result. In the DIVIDE operation, the preposition is either BY or INTO.
When performing a division operation, the result will be placed in the receiving field according to the PIC
specifications of that field.
Eg 5.4:
DIVIDE 130 BY 40 GIVING WS-TOTAL.
ROUNDED Option
A frequent need exists for rounding numeric values.
In the code given above, the result 37.999 is placed in an accumulator. When this value is move to the field AMT3 ,
the low-order decimal position is truncated and 37.99 is stored in the field. A more desirable result would be 38.00
since 38 is closer to the sum of 37.999. We consider results more accurate if they are rounded to the nearest decimal
position.
22
To obtain rounded results, the ROUNDED option may be specified with any arithmetic statement. In all case, it
directly follows the resultant data-name.
If ROUNDED and REMAINDER are to be used in the same DIVIDE statement, ROUNDED must appear first.
ON SIZE ERROR
Consider the following :
The effect of the above statement would be the same as coding MOVE 1000 TO AMT3. In this case, the resultant
field is not large enough to store the accumulated sum. In other words , an overflow or size error condition has
occurred. This will produce erroneous results.
In a divide, the size error condition has additional significance. If an attempt is made to divide by zero, a size error
condition will occur. This is because division by zero yields a result of infinity which makes it impossible to define a
sufficiently large receiving field.
If the ON SIZE ERROR option is employed along with the ROUNDED option, the word ROUNDED always
precedes ON SIZE ERROR.
COMPUTE statement
If complex or extensive arithmetic operations are required in a program, the use of the four arithmetic verbs may
prove cumbersome. The COMPUTE verb provides another method of performing arithmetic. The COMPUTE
statement uses the following arithmetic symbols :
+ Add
- Subtract
* Multiply
/ Divide
** exponentiation
23
* or / (whichever appears first from left to right)
+ or - (whichever appears first from left to right)
The use of parentheses overrides rules 1-3. That is, operations with parentheses are performed first.
4. CONDITIONAL STATEMENT
OPERATORS IN COBOL
The COMPUTE statement seen earlier uses the ARITHMATIC operators while there are more
types of operators used with conditional statements. They are CONDITION AL operators and
LOGICAL operators.
The Relational operators are used in conditional statements whenever there is a necessary for
comparing two values. The result of these comparisons will be only a TRUE or FALSE value.
EQUAL TO =
GREATER THAN >
LESS THAN <
GREATER THAN OR EQUAL TO >=
LESS THAN OR EQUAL TO <=
NOT EQUAL TO <>
The Logical operators used in COBOL are NOT, AND and OR. These operators are used when
more than one condition (Compound Condition) have to be checked for taking appropriate
decisions.
NOT (All simple relation, class or sign tests may be coded using a negated conditional). AND (If a statement or
statements are to be executed only when all of several conditions are met). OR (Performs an operation or a series
of operations if any one of several conditions exists.
CONDITIONAL STATEMENTS
24
SIGN CONDITION STATEMENTS
NESTED CONDITION STATEMENTS ( USING ELSE STATEMENTS)
OR
IF < Condition > Statement ELSE
Statement
OR
IF AMT1 IS EQUAL TO AMT2
DIVIDE QTY INTO TOTAL.
ELSE
ADD UNIT-PRICE TO FINAL-TOTAL.
This is used for checking the contents of the data-item whether they are numeric or alphabetic. The statement returns
a TRUE value depending on whether the test is successful or not.
This Condition is used to check whether the value present in a data-item is positive, negative or a zero.
The data-name can also be replaced with an expression where the value of the expression will be forst evaluated and
then the condition checked.
Eg : IF AMT IS POSITIVE
PERFORM 200-CALC-RTN.
NESTED CONDITIONS
25
Cobol allows nested conditional statements i.e. condition within condition. Logic in coding nested conditions should
be absolutely flawless as otherwise it might give unexpected results after the execution of the program.
Eg 6.2:
IF AMT1 = AMT2
NEXT SENTENCE
ELSE
ADD 1 TO TOTAL.
CONDITION NAMES
A condition-name is an entity which itself is a condition and can either have a TRUE or FALSE value. A data name
can be defined independently whereas a condition name is always associated with the data name and this data name
is known as conditional variable for the associated condition name. There may be more than one condition name
associated with a conditional variable.
An 88-level coded in the DATA DIVISION is a condition-name that denotes a possible value for an identifier. A
condition-name is always coded on the 88 level and has only a VALUE clause associated with it. Since a condition-
name is not the name of a field, it will not contain a PICTURE clause.
Eg : 05 MARITAL-STATUS PIC X.
88 SINGLE
VALUE “S”.
Eg : IF MARITAL-STATUS = “S”
PERFORM 1000-SINGLE-ROUTINE.
OR
IF SINGLE
PERFORM 1000-SINGLE-ROUTINE.
26
ITERATIONS
PERFORM statement
The MODULAR approach in COBOL is achived by the use of the PERFORM statements. This statement
temporarily stops the sequential execution of the statements of the program, transfers the control to some other part
of the program as required by the user, to execute a predefined set of statements before returning to the starting place
to continue the sequence. This helps to separate the program into smaller modules, so that managing the program
becomes simpler and easier to comprehend.
A.SIMPLE PERFORM
This simple PERFORM statement is used to execute a specified routine from one point in a program.
OR
The PERFORM THRU executes all statement beginning at pargraph-name-1 until the end of paragraph-
name-2 is reached. Control is then transferred to the statement directly following the PERFORM.
Eg: PERFORM
para1.
OR
PERFORM para1 THRU para5.
PROCEDURE DIVISION.
100-MAIN-MODULE.
:
PERFORM 400-HEADING-RTN.
:
200-CALC-RTN.
:
:
400-HEADING-RTN.
:
OR
100-MAIN.
PERFORM 300-PARA THRU 500-PARA.
:
200-PARA.
:
300-PARA.
:
400-PARA.
:
500-PARA.
EXIT.
600-PARA.
:
B. PERFORM N TIMES.
The PERFORM..TIMES construct is used to execute a sequence of steps a fixed number of times. It is not
necessary to establish a counter that must be incremented each time through the loop. When using the
TIMES format (PERFORM paragraph-name-1 identifier-1 TIMES) : (1) the identifier must be specified in
the DATA DIVISION; (2) it must have a numeric PICTURE clause; and (3) it must contain only integers or
zeros.
27
PERFORM para1 4 TIMES
OR
PERFORM para1 THRU para4 5 TIMES.
Loop to be performed
1. Code the steps required in the loop as a separated paragraph.
2. Increase or decrease the value in the field to be tested (e.g., ADD 1 TO COUNTER1).
Syntax is
PERFORM para1 VARYING variable
FROM initial-value BY step-value UNTIL condition
28
E.g. PERFORM parax VARYING I FROM 1 BY 1 UNTIL I < 10.
E. NESTED PERFORM
Syntax is
PERFORM para1 VARYING variable1
FROM initial-value BY step-value UNTIL condition AFTER
VARIABLE2 FROM initial-value BY step-value UNTIL condition.
E.g. Syntax is
PERFORM parax VARYING I
FROM 1 BY 1 UNTIL I < 2
AFTER J
FROM 1 BY 1 UNTIL J < 3.
G. INLINE PERFORM
GO TO
A GO TO permanently transfers control to another paragraph. It does not return control to the paragraph from where
it was issued.
The GO TO with depending phrase can transfer control to two or more procedure names depending on the value of
the control variable mentioned with the phrase.
Alternatively the GO TO statement can be used to transfer control to one of the different procedure names.
29
5. EDITING FEATURES IN COBOL
Edited fields
The purpose of editing is to make data more suitable for human reading. Thus editing in its most common use is
associated with printing data on the printer. For example, we may suppress leading zeros, we may use commas to
make long numeric values more legible, we may insert a dollar sign in front of a value, etc.
The Z PICTURE character is used to replace leading zeros by blanks and thus performs a function identical to that
of the floating. Zero suppression terminates when the first nonzero digit or the character is encountered, whichever
occurs first. When Z’s have been designated for all positions in a field and the value to be inserted in that field is
zero, in that case the entire field is blanked. Z’s can appear before as well as after the decimal point. The number of
Z’s representing integers in the report-item should be equal to the number of integers or 9’s in the sending field.
Eg 2.10:
PIC Description Value Printed as
Z99 25 b25
ZZZV99 25 b2500
ZZZV99 0.10 bbb10
ZZZVZZ 0.052 bbb05
ZZZVZZ 0.00 bbbbb
* - Check Protection
The * character is referred to as a check-protect character and is normally used to protect dollar amounts
written on checks or other negotiable documents. Asterisks are zero-suppression characters that replace each non-
significant zero and comma with * instead of a space.
Eg 2.11:
PIC Description Value Printed as
**999 04678 *4678
***99 00052 ***52
$ - Dollar Sign
By use of the $ PICTURE character the dollar sign is written in the position in which it is to appear in the output.
Since the $ sign is counted in the size of the field, the field should be assigned at least one more position than the
maximum number of significant digits expected. The $ may also be floated, by which we mean that it will be
entered to the left of the first significant digit in the field and be preceded by blanks. The $ float stops when whether
the first nonzero digit or the . or V is encountered. When the $ sign appears in all positions and the value is zero, the
effect is to blank the entire field (including any . and ,).
Eg 2.12:
PIC Description Value Printed as
$999V99 125.13 $12513
$9(5)V99 100.00 $0010000
$$99V99 12.49 b$1249
30
$$$$9V99 150.10 $15010
$$$$V99 0.15 bbb$15
$$$$V$$ 0.0 bbbbb
. - (Decimal Point)
The . (decimal) PICTURE character indicates the position of the decimal point and serves to align the actual
decimal values in the field, only one such character may appear in a field. Further, a field cannot contain both a V
and a . PICTURE character. It cannot be the rightmost character. If all the digits before and after the decimal point
are zero, the resulting field will be blank.
Eg 2.13:
PIC Description Value Printed as
$9,999.99 2,350.22 $2,350.22
$9,999.99 150.31 $0,150.31
$$,999.99 150.31 bb$150.31
$$,$$$.99 24.40 bbb$25.40
$$,$$$.999 0.019 bbbbb$.019
$$$$$.$$$ 0.0 bbbbbbbbb
, - (Comma)
The comma is placed in the positions in which it is desired. A field may include more than one , (comma) PICTURE
character if the size of the field warrants it. A comma will be appropriately suppressed if no significant digit
precedes it. It cannot appear as the leftmost or rightmost character.
Eg 2.14:
PIC Description Value Printed as
$9,999.99 2,350.22 $2,350.22
$9,999.99 150.31 $0,150.31
$$,999.99 150.31 bb$150.31
$$,$$$.99 24.40 bbb$25.40
$$,$$$.999 0.019 bbbbb$.019
The + PICTURE character is used to print wither a plus sign or a minus sign for all values. A + sign will be
generated for positive or unsigned quantities, and a – sign will be generated for negative quantities. The sending
field should have an $ in its PICTURE clause for it to be interpreted as a signed number.
31
The + or – PICTURE character can appear as the leftmost or rightmost character. They can also be floated, similar to
the $ character. However , the +, -, and $ are mutually exclusive as floating characters. If we want to have both $
float and + or – sign representation, we write the + or – to the right of the field.
Eg 2.15:
PIC Description Value Printed as
+999.9 35.2 +035.2
999.9+ 35.2 035.2+
999.9+ -35.2 035.2-
++9.9 -001.3 b-1.3
+++9.99 .05 bb+0.05
+++9.99 -.05 bb-0.05
++++.++ .01 bbb+.01
DB/CR
In accounting applications there is often need to identify values that represent debits or credits. The
COBOL language facilitates such differentiation by means of the DB (debit) and CR (CR) editing characters. The
DB or CR symbol is written only to the right of a field in the PICTURE clause, and in both cases it is represented in
storage for the purpose of subsequent output only when the value is negative.
+ + -
- Blank -
DB Blank DB
CR Blank CR
Eg 2.16:
PIC Description Value Printed as
$999.99DB 135.26 $135.26bb
$999.99DB -135.26 $135.26DB
$,$$99.99CR -10.50 bb$10.50CR
B – (blank)
The B insertion editing character results in blanks being entered in the designated positions.
Eg 2.17:
05 NAME PIC A(10) VALUE ‘RBSMITH’.
NAME = RbBbSMITHbbbbb
0 - (Zero)
The zero insertion character causes zeros to be inserted in the positions in which it appears.
32
Eg 2.18:
05 AMOUNT PIC 9(4)000 VALUE 1365.
AMOUNT = 1365000
/ - (stroke)
Each / (stroke) in the PICTURE character string represents a character position into which the stroke character will
be inserted.
Eg 2.19:
05 PRINT-DATE PIC 99/99/99 VALUE 040798.
PRINT-DATE = 04/07/98
VALUE Clause
In addition to defining storage fields using the PICTURE clause it is often desirable to assign initial values to
elementary items in the WORKING-STORAGE SECTION. Such a value may remain unchanged throughout the
program, or it may change in the course of program execution. Such initial values are generally not assigned to FILE
SECTION items, since such fields either receive their data from the external medium or from other storage location
as the result of program execution.
Eg 2.20:
02 PAGE-TITLE PIC A(14) VALUE “SAMPLE PROGRAM”.
02 TAX-RATE PIC V99 VALUE IS 0.03.
A nonnumeric literal may contain up to 120 characters. The same applies to a nonnumeric literal in a VALUE clause.
Sometimes a nonnumeric literal may not fit on a single line, hence it is necessary to continue this literal.
Eg 2.21:
01 PAGE-HEADING PIC X(36) VALUE “MONTHLY TRANSACTIONS FOR AP
- “RIL 2000”.
USAGE clause
Numeric data in a computer may be represented in one of two basic modes. They may be represented as character
data or as numeric data. The arithmetic registers of computers perform arithmetic with numeric data that is in
numeric, not character mode. If numeric data is represented in character mode, it must first
be converted to numeric mode before arithmetic computations can be performed.
In COBOL, data in character mode is described in DISPLAY mode, while data in numeric mode is described as
being COMPUTATIONAL MODE. All data items are assumed to be in DISPLAY mode unless they are declared to
be COMPUTATIONAL. The declaration is done in the DATA DIVISION with the USAGE clause.
33
Eg 2.22:
02 AMOUNT-1 PIC 99.
02 AMOUNT-2 PIC 99 USAGE DISPLAY.
02 AMOUNT-3 PIC 99 USAGE COMPUTATIONAL.
02 AMOUNT-4 PIC 99 USAGE COMP.
REDEFINES Clause
The REDEFINES clause can be used to allow the same storage location to be referenced by different data-names or
to allow a regrouping or different description of the data in a particular storage location to conserve the memory
space. REDEFINES clause can be used to redefine group items as well as elementary items
Restrictions
Eg:
1 SAMPLE-REC.
02 RECEIVABLE.
03 CUSTOMER-NUMBER PIC 9(8).
03 CUSTOMER-NAME PIC X(11).
03 AMOUNT PIC 9(4)V99.
02 PAYABLE REDEFINES RECEIVABLE.
03 VENDOR-NUMBER PIC 9(6).
03 VENDOR-NAME PIC X(12).
03 VENDOR-OWED-AMT PIC 9(5)V99.
RENAMES Clause
The RENAMES clause provides the programmer with the capability of regrouping elementary data items. It
resembles the REDEFINES clause, except that it can form a new grouping of data items which combines several
items. Use of the RENAMES clause is always signaled by the special 66 level number.
Eg 2.24:
01 TAX-RECORD.
02 SOC-SEC-NUMBER PIC X(9).
02 NAME.
03 FIRST-NAME PIC X(10).
03 LAST-NAME PIC X(15).
02 TOTAL-YTD.
03 GROSS-PAY PIC 9(8)V99.
03 NET-PAY PIC 9(8)V99.
03 TAX PIC 9(5)V99.
66 LAST-GROSS RENAMES LAST-NAME THRU NET-PAY.
Qualification of names
A data name that is not unique must be qualified in COBOL verbs.
Eg 2.25:
01 IN-REC
34
05 NAME PIC X(10).
05 AGE PIC 99.
01 OUT-REC.
05 NAME PIC X(10).
05 B-DATE PIC 9(6).
NOTE : There is hardly any common between RENAMES and REDEFINES clauses. While RENAMES is a special
data description entries and appears only as the 66level entry, REDEFINES can appears a clause in any entry at
levels 01 to 49. RENAMES provides means for alternate regrouping of elementary items within a record,
REDEFINES on the other hand provides means to have alternate descriptions for group or elementary items.
SYNCHRONIZED Clause
The SYNCHRONIZED clause or SYNC is used to specify alignment of an elementary data item to the natural
boundaries of the computer memory. This clause can be used with any USAGE clause. The natural boundary can be
a double word boundary, one word boundary, half word boundary or byte boundary depending o the USAGE format
used.
The SYNCHRONIZED clause enables programmer to obtain more efficient object program, however due to the use
of SYNCHRONIZED clause, the elementary fields in a group may not have contiguous space in the memory. A few
bytes may be unused in between two consecutive fields called slack bytes.
E.g :
35
6.File Handling
File Organization methods
It is important that file be so organized that efficient processing can be accomplished by matching the file
data characteristics, processing method, and file organization. Basically, three methods of file organization are
available on disk systems : sequential, indexed sequential, and relative file organization.
Any program that (1) reads data from input files or (2) produces output files, requires an INPUT-OUTPUT
SECTION and a FILE SECTION to describe the input and output areas.
INPUT-OUTPUT SECTION
The INPUT-OUTPUT SECTION of the ENVIRONMENT DIVISION follows the CONFIGURATION
SECTION and supplies information concerning the input and output devices used in the program. In the FILE-
CONTROL paragraph, a file-name is selected for each file to be used in the program; in addition, each file-name
selected is assigned to a device. The SELECT statement is coded in Area B.
Eg 8.1:
ENVIRONMENT DIVISION.
:
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT EMPLOYEE-FILE
ASSIGN TO “EMP.DAT”
ORGANIZATION IS LINE SEQUENTIAL.
FILE SECTION.
Each file is described in the FILE SECTION with an FD sentence that may consist of a series of clauses.
After the clauses are specified, the FD sentence ends with a period. FD is an abbreviation for File Description. Each
FD entry will describe a file defined in a SELECT statement in the ENVIRONMENT DIVISION.
The two entries, DATA DIVISION and FILE SECTION, are coded in Area A. FD is also coded in Area A.
The file-name, however, is typically coded in Area B.
Eg 8.2:
DATA DIVISION.
FILE SECTION.
36
FD EMPLOYEE-FILE
LABEL RECORDS ARE STANDARD
RECORD CONTAINS 70 CHARACTERS
BLOCK CONTAINS 10 RECORDS.
Label Records
Label records are usually created as the first and last records of a disk or tape to provide identifying
information about the file on disk or tape. Labels are created on output files so that, when the same file is later read
as input, the labels may be checked to ensure that the file being accessed is the correct one. Labels are created on
output files and checked on input files. The COBOL compiler will supply the routine for writing labels on output
files or for checking labels on input file if the entry LABEL RECORDS ARE STANDARD is included.
The clause LABEL RECORDS ARE STANDARD is permitted for disk and tape files only. Devices such as
printers do not use label records, since identifying information is unnecessary where data is visible to the human
eye. The clause LABEL RECORDS ARE OMITTED is used for such files.
Eg 8.3:
01 EMPLOYEE-REC.
05 EMP-NAME.
10 EMP-FIRST-NAME PIC X(10).
10 EMP-LAST-NAME PIC X(15).
05 EMP-DEPT PIC X(4).
05 EMP-SALARY PIC 9(5)V99.
05 EMP-DOJ PIC 9(6).
37
Input/output verbs
There are 4 input/output verbs : OPEN, READ, WRITE, CLOSE.
OPEN statement
Before an input or an output file can be used by the program it must be opened. An OPEN statement,
designates files as either input or output. It also accesses the specific devices, and makes the files available for
processing. It performs header label routines if label records are STANDARD. The OPEN statement checks the
header label to determine if the correct file has been accessed.
Eg 8.4:
OPEN INPUT EMPLOYEE-FILE.
OPEN OUTPUT REPORT-FILE.
The order in which files are opened is not significant. The only restriction is that a file must be opened
before it may be read or written; a file must be accessed before it may be processed. Since the OPEN statement
accesses the files, it is generally on of the first instructions coded in the PROCEDURE DIVISION.
READ statement
After an input file has been opened, it may be read. A READ statement transmits data from the input
device, assigned in the ENVIRONMENT DIVISION, to the input storage area, defined in the FILE SECTION of
the DATA DIVISION.
The primary function of the READ statement is to transmit one data record to the input area reserved for
that file. That is, each time a READ statement is executed, one record is read into primary storage.
The READ statement has, however, several other functions. Like the OPEN statement, it performs certain
checks. It checks the length of each input record to ensure that it corresponds to the length specified in a RECORD
CONTAINS clause in the data DIVISION. If a discrepancy exists, an error message prints, and a program interrupt
occurs.
The READ statement will also use the BLOCK CONTAINS clause, if specified, to perform a check on the
blocking factor.
The AT END clause in the READ statement tests to determine if there is any more input. An AT END
clause of the READ statement tells the computer what to do if there is no more data to be read.
Eg 8.5:
READ EMPLOYEE-FILE
AT END
MOVE “YES” TO END-OF-FILE.
WRITE statement
The WRITE instruction takes data in the output area defined in the DATA DIVISION and transmits it to the
device specified in the ENVIRONMENT DIVISION.
Note that although files are read, we write records. The record-name appear on the 01 level and is generally
subdivided into fields. The record description specifies the format of the output.
Eg 8.6:
WRITE EMPLOYEE-REC.
CLOSE statement
A CLOSE statement is coded at the end of the job after all records have been processed to release these
files and deactivate the devices. All files that have been opened at the beginning of the program are closed at the end
of a program. The CLOSE statement, like the OPEN, will perform additional functions. When creating disk or tape
records, for example, the CLOSE will create trailer labels; it will also rewind a tape.
38
Eg 8.7:
CLOSE EMPLOYEE-FILE.
COPY statement
A COPY statement is used to bring into a program a series of prewritten COBOL entries that have been
stored in a library. Copying entries from a library, rather than coding them, has the following benefits : (1) it could
save a programmer a considerable amount of coding and debugging time; (2) it promotes program standardization
since all programs that copy entries from a library will be using common data-names and/or procedures; (3) it
reduces the time it takes to make modifications and reduces duplication of effort; if a change needs to be made to a
data entry, it can be made just once in the library without the need to alter individual programs; and (4) library
entries are extensively annotated so that they are meaningful to all users; this annotation results in better-
documented programs and systems.
Most often, the COPY statement is used to copy FD and 01 entries that define and describe files and
records. In addition, standard modules to be used in the PROCEDURE DIVISION of several programs may also be
stored in a library and copied as needed.
Contents of EMP.REC
Eg 8.8a:
01 EMPLOYEE-REC.
05 EMP-NAME.
10 EMP-FIRST-NAME PIC X(10).
10 EMP-LAST-NAME PIC X(15).
05 EMP-DEPT PIC X(4).
05 EMP-SALARY PIC 9(5)V99.
05 EMP-DOJ PIC 9(6).
Eg 8.9:
IDENTIFICATION DIVISION.
PROGRAM-ID. FILE-CRT.
* This program creates a sequential EMPLOYEE file.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT EMPLOYEE-FILE ASSIGN TO "EMP.DAT"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
39
FILE SECTION.
FD EMPLOYEE-FILE
LABEL RECORDS STANDARD.
01 EMPLOYEE-REC.
05 EMP-NO PIC 9(4).
05 EMP-NAME.
10 EMP-FIRST-NAME PIC X(10).
10 EMP-LAST-NAME PIC X(15).
05 EMP-DEPT PIC X(4).
05 EMP-SALARY PIC 9(5)V99.
05 EMP-DOJ PIC 9(6).
WORKING-STORAGE SECTION.
01 WS-ANS PIC X(01) VALUE "Y".
88 ANS-NO VALUE "N" "n".
PROCEDURE DIVISION.
0000-MAIN.
OPEN OUTPUT EMPLOYEE-FILE.
PERFORM 1000-ACPT-PARA UNTIL ANS-NO.
CLOSE EMPLOYEE-FILE.
STOP RUN.
1000-ACPT-PARA.
DISPLAY "ENTER YOUR EMP CODE : " WITH NO ADVANCING.
ACCEPT EMP-NO.
DISPLAY "ENTER YOUR FIRST NAME : " WITH NO ADVANCING.
ACCEPT EMP-FIRST-NAME.
DISPLAY "ENTER YOUR LAST NAME : " WITH NO ADVANCING.
ACCEPT EMP-LAST-NAME.
DISPLAY "ENTER YOUR DEPARTMENT : " WITH NO ADVANCING.
ACCEPT EMP-DEPT.
DISPLAY "ENTER YOUR SALARY : " WITH NO ADVANCING.
ACCEPT EMP-SALARY.
DISPLAY "ENTER YOUR DATE OF JOINING : " WITH NO ADVANCING.
ACCEPT EMP-DOJ.
WRITE EMPLOYEE-REC.
DISPLAY "DO YOU WANT TO ADD MORE RECORDS : "
WITH NO ADVANCING.
ACCEPT WS-ANS.
40
9.Sorting and merging files
Sorting
Records in files frequently must be sorted into specific sequences for updating, answering inquiries, or
generating reports. Sorting is a common procedure used for arranging records into a specific order so that sequential
processing can be performed.
COBOL has a SORT verb, which can make it very useful as part of a COBOL program.
The programmer must specify whether the key field is to be an ASCENDING KEY or a DESCENDING
KEY, depending on which sequence is required :
ASCENDING : From lowest to highest
DESCENDING : From highest to lowest
The SORT verb may be used to sequence records with more than one key field. For example, to sort an
employee file so that it is in alphabetic sequence by name within each department.
Eg 9.1:
SORT SORT-FILE
ON ASCENDING KEY S-EMP-DEPT
ON ASCENDING KEY S-EMP-NAME
USING EMPLOYEE-FILE
GIVING SORT-EMPLOYEE-FILE.
All these files would be defined in the ENVIRONMENT DIVISION using standard ASSIGN clauses, which are
system dependent. The SORT-FILE is actually assigned to a temporary work area that is used during processing but
not saved. Only the unsorted disk file and the sorted output disk file are assigned standard file-names so that they
can be permanently stored.
FDs are used in the DATA DIVISION to define and describe the input and output files in the usual way. The sort
or work file is described with an SD (sort file description) entry. The only difference between SD and FD entries is
that an SD must not have a LABEL RECORDS clause. Note, too, that the field(s) specified as the KEY field(s) for
sorting purposes must be defined as part of the sort record format.
Eg 9.2:
ENVIRONMENT DIVISION.
:
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT SORT-FILE
ASSIGN TO DISK.
:
DATA DIVISION.
FILE SECTION.
SD SORT-FILE.
01 SORT-REC.
05 S-EMP-NAME.
10 S-EMP-FIRST-NAME PIC X(10).
10 S-EMP-LAST-NAME PIC X(15).
05 S-EMP-DEPT PIC X(4).
05 FILLER PIC 9(13).
41
The SORT statement can, however, be used in conjunction with procedures that process records just before
they are sorted and/or process records after they are sorted.
INPUT PROCEDURE
The INPUT PROCEDURE processes data from the incoming file prior to sorting. An INPUT
PROCEDURE may be used to perform the following operations prior to sorting : (1) validate data in the input
records, (2) eliminate records with blank fields, (3) count input records.
With COBOL 74, the procedure-name of an INPUT PROCEDURE must be a section-name and not a
paragraph-name. A section is a series of PROCEDURE DIVISION paragraphs that is treated as a single entry or
unit. Rule for forming section-names are the same as rules for forming paragraph-names. The word SECTION,
however, follows a section-name (e.g., A000-ERROR SECTION). The end of a section is recognized when another
section-name is encountered, or when the end of the program is reached.
Explanation
1. The first section in the PROCEDURE DIVISION contains the SORT instruction, any processing to be
performed before or after the SORT verb is executed, and a STOP RUN.
2. The second section begins with the main module of the INPUT PROCEDURE. It opens the input file, reads
the first record, and then performs a process routine (in a separate paragraph within this second section)
until there is no more data.
3. After the separate paragraph is executed until ARE-THER-MORE-RECORDS = “NO”, control returns to
the main module of the second section to be terminated, control must pass to the last statement within the
section. This means that a GO TO is required. We code GO TO A300-TEST-IT-EXIT as the last sentence.
Since no operations are required in this last paragraph, EXIT is coded, which passes control back to the
SORT statement, where the file is then sorted.
42
OUTPUT PROCEDURE
The OUTPUT PROCEDURE is used to process the sorted records prior to, or perhaps even instead of,
placing them in the output file. The OUTPUT PROCEDURE can be used instead of the GIVING option. The
OUTPUT PROCEDURE is similar to the INPUT PROCEDURE. When the INPUT PROCEDURE is complete, the
file is then sorted. An OUTPUT PROCEDURE processes all sorted records in the sort file and handles the transfer
of these records to the output file.
In an INPUT PROCEDURE we RELEASE records to a sort file rather than writing them. In an INPUT
PROCEDURE we RETURN records from the sort file rather than reading them.
Merging
The MERGE statement combines two or more files into a single file. Its format is similar to the SORT. The
file to be merged is a work file designated as an SD. At least two file-names must be included for a merge, but more
than two are permitted. Unlike, the SORT, however, an INPUT PROCEDURE may not be specified with the
MERGE statement. That is, using the MERGE statement, you may only process records after they have been
merged, not before. The OUTPUT PROCEDURE has the same format as with the SORT.
Eg 9.4:
MERGE MERGE-FILE
ON ASCENDING KEY M-EMP-DEPT
USING OLD-PAYROLL
NEW-PAYROLL
GIVING EMPLOYEE-FILE.
Eg 10.1:
SELECT EMPLOYEE-FILE
ASSIGN TO “EMP.DAT”
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS WS-STATUS.
:
WORKING-STORAGE SECTION.
01 WS-STATUS PIC X(2).
The possible values that may be placed in the FILE STATUS field when an input or output operation is
performed are listed in Appendix – E.
43
44
11.CONTROL BREAK PROCESSING
What is a control break procedure ?
A control break procedure is used if records are in sequence by a control field and the number of records in
each control field is variable.
45
12.Indexed Files
An indexed file is really two files – the data file, which is created in sequence but can be accessed
randomly, and the index file, which contains the value of each key field and the disk address of the record with that
corresponding key field. To access an indexed record randomly, the key field is looked up in the index file to find
the disk address of the record; then the record is accessed in the indexed data file directly.
The index on a disk is similar to a book’s index, which has unique subjects (keys) and their corresponding
page numbers (addresses). There would be two ways to find a topic in the book. You can read the book sequentially,
from the beginning, until that topic is found, but this would be very time consuming and inefficient. The best method
would be to look up the topic in the index, find the corresponding page number, and go directly to that page. This is
precisely how records can be accessed on a disk file that has an index.
With an indexed file, records can be accessed either sequentially or randomly, depending on the user’s
needs. The term random access implies that records are to be processed or accessed in some order other than the one
in which they were physically written on the disk.
Eg 12.1:
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IND-EMP-FILE
ASSIGN TO “INDEMP.DAT”
ORGANIZATION IS INDEXED
ACCESS IS SEQUENTIAL
RECORD KEY IS I-EMP-NO.
DATA DIVISION.
FILE SECTION.
FD IND-EMP-FILE
LABEL RECORDS STANDARD.
01 IND-EMP-REC.
46
05 I-EMP-NO PIC 9(4).
05 I-EMP-NAME PIC X(25).
05 I-EMP-DEPT PIC X(4).
05 I-EMP-SAL PIC 9(5)V99.
Eg 12.2:
WRITE IND-EMP-REC
INVALID KEY
PERFORM 2000-ERROR-PARA.
With READ
When reading a disk file randomly, we do not test for an AT END condition because we are not reading the
file in sequence; instead, we include an INVALID KEY test. If there is no record in the INDEXED-FILE with a
RECORD KEY equal to T-EMP-NO, the INVALID KEY clause will be executed.
Eg 12.3:
DISPLAY “ENTER EMPLOYEE CODE :”
ACCEPT T-EMP-EMP-NO.
MOVE T-EMP-NO TO I-EMP-NO.
READ IND-EMP-FILE
INVALID KEY
PERFORM 600-ERR-RTN.
DELETE verb
The DELETE verb can be used to delete records from indexed files. Note that we use the file-name with
the DELETE verb, but the word RECORD can be specified as well. That is, both the statements DELETE
INDEXED-FILE and DELETE INDEXED-FILE RECORD can be used to delete the record in the INDEXED-FILE
storage area.
To delete a record from an indexed file, you should first read the record into storage and then instruct the
computer to delete it.
Eg 12.4:
MOVE “Y” TO WS-FOUND.
MOVE 1001 TO I-EMP-NO.
READ IND-EMP-FILE
INVALID KEY
MOVE “N” TO WS-FOUND.
IF WS-FOUND = “Y”
DELETE IND-EMP-FILE
INVALID KEY
DISPLAY “ERROR DELETING RECORD”.
47
Using ALTERNATE RECORD KEYs
Indexed files may be created with, and accessed by, more than one identifying key field. That is, we may
want to access employee records using the name as the key field. To enable a file to be accessed randomly using
more than one key field, we would need to establish an ALTERNATE RECORD KEY.
To establish multiple key fields for indexing, we use an ALTERNATE RECORD KEY clause in the
SELECT statement.
Note:
1. More than one ALTERNATE record key can be used.
2. WITH DUPLICATES means than an ALTERNATE RECORD KEY need not be unique. Thus, fields like
EMP-DEPT can be used as a key even though numerous records may have the same department no.
3. A record can be accessed by its RECORD KEY or any of its ALTERNATE RECORD KEYs.
Eg 12.5:
SELECT IND-EMP-FILE
ASSIGN TO “INDEMP.DAT”
ORGANIZATION IS INDEXED
ACCESS IS SEQUENTIAL
RECORD KEY IS I-EMP-NO
ALTERNATE RECORD KEY IS I-EMP-DEPT WITH DUPLICATES.
Eg 12.6:
MOVE “Y” TO WS-FOUND.
MOVE 1001 TO I-EMP-NO.
START IND-EMP-FILE
KEY > I-EMP-NO
INVALID KEY DISPLAY “THERE IS NO EMP NO > 1001”
MOVE “N” TO WS-FOUND.
IF WS-FOUND = “Y”
READ IND-EMP-FILE
48
AT END
MOVE “Y” TO WS-EOF.
Suppose we wish to begin processing with an I-EMP-NO greater than 006. We must include a KEY clause
with the START because we wish to position the file at allocation greater than the value of a RECORD KEY. The
KEY clause can be omitted only if the record to be located has a RECORD KEY equal to the one stored.
49
13.Table Handling
Use of OCCURS clause
1. An OCCURS clause is used in COBOL to indicate the repeated occurrence of fields with the same format.
2. Defining a series of totals in WORKING-STORAGE to which amounts are added; after all data is
accumulated, the total can be printed.
3. Defining a table in WORKING-STORAGE to be accessed by each input record. For example, using the
contents of some input field to “look up” the required data in the table.
Eg 13.1:
01 TEMP-REC.
05 TEMPERATURE OCCURS 24 TIMES PIC S9(3).
Subscript
A subscript is used in the PROCEDURE DIVISION to indicate which specific item within the array we
wish to access.
The subscript is used along with the identifier that is defined with an OCCURS, to refer to an item within
an array.
Eg 13.2:
MOVE TEMPERATURE (2) TO TEMP-OUT.
Eg 13.3:
01 MONTH-NAMES.
05 STRING-1 PIC X(36) VALUE
‘JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC’.
05 MONTH REDEFINES STRING-1 OCCURS 12 TIMES PIC XXX.
50
To use a SEARCH statement, two additional entries are required : the INDEXED BY clause along with
OCCURS, and the SET statement in the PROCEDURE DIVISION.
Table entries must be specified with an index rather than a subscript. An index is similar to a subscript, but
it is defined along with the table entries as part of the OCCURS description.
Eg 13.4:
01 SALES-TAX-TABLE,
05 TABLE-ENTRIES OCCURS 1000 TIMES INDEXED BY X1.
10 WS-ZIPCODE PIC 9(5).
10 WS-TAX-RATE PIC V999.
However, unlike a subscript, an index is not defined separately in WORKING-STORAGE. The compiler
automatically provides an appropriate PICTURE clause. An index is processed more efficiently than a subscript,
because the index contains the displacement from the start of the table.
A SEARCH statement does not automatically initialize the index at 1 because sometimes we may want to
begin searching a table at some point other than the beginning. Initializing an index at 1 must be performed by a
SET statement prior to the SEARCH.
Eg 13.5:
SET X1 TO 1.
SEARCH TABLE-ENTRIES
AT END MOVE 0 TO WS-SALES-TAX
WHEN ZIP-IN = WS-ZIPCODE (X1)
COMPUTE ........
An index can be used to reference an element only in the table or array for which it was defined.
With 2 WHEN clauses, the computer begins by performing the first comparison. Only if the condition in the
first WHEN is not met does it test the second WHEN.
To search for multiple matches in a table, it is better to use a PERFORM rather than a SEARCH statement for
processing the entire table.
If you have parallel table with CUST-NO-TABLE storing 25 customer numbers and CUST-ARRAY storing the
corresponding BAL-DUE for each. In such a case the SEARCH... VARYING can be used.
Eg 13.6:
SET X1, X2 TO 1.
SEARCH EACH-CUST-NO VARYING X2
AT END PERFORM 300-ERR-RTN
WHEN CUST-NO-IN = EACH-CUST-NO (X1)
ADD AMT-IN TO BAL-DUE (X2).
Binary search
When table entries are arranged in sequence by some field, such as T-CUSTOMER-NO, the most efficient type
of look-up is a binary search. The SEARCH ALL verb is used to perform a binary search. A SET statement is not
necessary with the SEARCH ALL, since the computer sets the index to the appropriate point initially when each
binary search begins.
51
5. The OCCURS item and its index, which define the table argument, must appear to the left of the equal sign.
Eg 13.7:
WHEN S-AMT (X1) = AMT1 .....
To use the SEARCH ALL statement, we must indicate which table entry will serve as the key field. The
identifier used in the ASCENDING KEY clause must be an entry within the table and it must appear before the
INDEXED BY clause.
Eg 13.8:
01 TABLE-1.
05 DISCOUNT-TABLE OCCURS 50 TIMES
ASCENDING KEY T-CUSTOMER-NO
INDEXED BY X1.
10 T-CUSTOMER-NO PIC 9(4).
10 T-DISCOUNT-PCT PIC V999.
Eg 13.9:
01 INVENTORY-TABLE.
05 WAREHOUSE OCCURS 50 TIMES INDEXED BY X1.
10 ITEM-X OCCURS 100 TIMES INDEXED BY X2.
15 PART-NO PIC 9(4).
15 UNIT-PRICE PIC 999V99.
The identifier used with the SEARCH refers to the lowest-level OCCURS entry.
e.g. SEARCH ITEM-X.
Note that SEARCH ITEM-X increments the lowest-level index only. Hence if X1 is set to 1 initially, the SEARCH
will perform a look-up on items in warehouse 1 only, that is (1,1) to (1,100).
To search all warehouses, the major index X1 must be incremented.
52
14.Call statement
Structured programs should consist of a series of independent modules that are executed from the main
module.
When programs are properly structured :
1. Each module may be written, compiled, and perhaps even tested independently.
2. The modules may be written in different stages, in atop-down manner. They may even be coded by
different programmers.
3. If a specific module needs to be modified, the entire logical flow should still function properly
without the need for extensive revision to other parts of the program.
Modules within a program can be viewed as subroutines that are called or executed from the main module.
But a program may also CALL or reference independent subprograms stored in a library that are entirely separate
from the main program itself. The main program that references or calls a subprogram is referred to as the calling
program. The subprogram that is linked and executed within the main program is referred to as the called program.
The called program would need to be compiled so that it may be called when needed.
When the CALL is performed, data is passed from the calling to the called program (if the calling program has
assigned values to fields used in the called program). The entire called program is executed, data is passed from
the called program back to the calling program, and control return to the calling program.
LINKAGE SECTION
A LINKAGE SECTION must be defined in the called program for identifying those items that (1) will be passed to
the called program from the calling program and (2) passed back from the called program to the calling program.
The LINKAGE SECTION of the called program, then, describes all items to be passed between the two programs.
The LINKAGE SECTION, if used, is coded after the FILE and WORKING-STORAGE SECTIONs of the called
program. This section is similar to WORKING-STORAGE except that VALUE clauses from initializing fields are
not permitted in the LINKAGE SECTION.
EXIT PROGRAM
The last executed statement in the called program must be the EXIT PROGRAM. It signals the computer to return
control back to the calling program. With COBOL 74, EXIT PROGRAM must be the only statement in the last
paragraph.
53
Sample Program
54
15.String Handling
The STRING Statement
A STRING statement may be used to combine several fields to form once concise field. This process is called
concatenation.
05 NAME.
10 LAST-NAME PIC X(10) VALUE “EDISON”.
10 FIRST-NAME PIC X(10) VALUE “THOMAS”.
10 MIDDLE-NAME PIC X(10) VALUE “ALVA”.
01 NAME-OUT PIC X(33).
STRING
FIRST-NAME DELIMITED BY ‘ ‘
‘ ‘ DELIMITED BY SIZE
MIDDLE-NAME DELIMITED BY ‘ ‘
‘ ‘ DELIMITED BY SIZE
LAST-NAME DELIMITED BY ‘ ‘
‘ ‘ DELIMITED BY SIZE
INTO NAME-OUT.
Output
OVERFLOW Option
The OVERFLOW option specifies the operation(s) to be performed if the receiving field is not large enough to
accommodate the result.
POINTER Option
We may count the number of characters actually moved to the receiving field if it is initialized at zero.
MOVE 1 TO WS-COUNT.
STRING FIRST-NAME DELIMITED BY ‘ ‘
INTO NAME-OUT
WITH POINTER WS-COUNT.
Output
WS-COUNT = 6
55
5. The STRING statement move data from left to right just like alphanumeric fields are moved, but
a STRING does not pad data from left to right just like alphanumeric fields are moved, but a
STRING does not pad with low-order blanks, unlike an alphanumeric MOVE.
Output
FIRST-NAME = THOMAS
MIDDLE-NAME = ALVA
LAST-NAME = EDISON
ACCEPT WS-NAME.
INSPECT WS-NAME TALLYING CTR-1 FOR ALL SPACES.
This code will check for the number of spaces in the field WS-NAME and store the value in the field CTR-1.
Successful Completion
56
* 00 Successful completion - no error occurred
* 02 Duplicate alternate record key (when WITH DUPLICATES) is
not specified
04 Successful read, but length of record does not conform to FD
entry
Unsuccessful Completion
* 10 Sequential read attempted, but there are no more input records
* 21 Keys are not in correct sequence
* 22 Attempt to write a record, which will create a duplicate primary
record key
* 23 Required record not found during a READ
* 24 Attempt to write beyond the pre-established boundaries of an
indexed file
* 30 Permanent data error has occurred (hardware problem)
34 Boundary error for a sequential file
37 Permanent error has occurred because an OPEN statement has
been attempted on a file that will not support the mode
specified in the OPEN statement.
41 An OPEN statement has been attempted on a file that is already
open.
42 A CLOSE statement has been attempted on a file that has not
been opened
* 43 An attempt has been made to DELETE or REWRITE a record
after an unsuccessful READ
57
COBOL Syntax
Uppercase words are COBOL reserved words that have special meaning to the compiler.
Underlined words are required in the paragraph.
Lowercase words represent user-defined entries.
Braces { } denote that one of the enclosed items is required.
Brackets [ ] mean the clause or paragraph is optional.
If punctuation is specified in the format, it is required.
The use of dots or ellipses (...) means that additional entries of the same type may be included if desired.
IDENTIFICATION DIVISION.
PROGRAM-ID. Program-name.
[AUTHOR. [Comment-entry]....].
[INSTALLATION. [Comment-entry]....].
[DATE-WRITTEN. [Comment-entry]....].
[DATE-COMPILED. [Comment-entry]....].
[ SECURITY. [Comment-entry]....].
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. computer-name.
OBJECT-COMPUTER. computer-name.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT file-name-1
ASSIGN TO implementor-name-1
[ ORGANIZATION IS ] SEQUENTIAL
INDEXED
DATA DIVISION.
FILE SECTION.
FD file-name-1
LABEL RECORD IS OMITTED
RECORD ARE STANDARD
58
1 RECORD-NAME-1.
05 FIELD-1.
05 FIELD-2.
05 FIELD-3.
WORKING-STORAGE SECTION.
(level-number 02-49) identifier-1 OCCURS integer-1 TIMES
INDEXED BY index-name-1
LINKAGE SECTION.
PROCEDURE DIVISION.
OPEN INPUT
OUTPUT file-name-1 ...
I-O
EXTEND
READ file-name-1
[ AT END statement-1...] .
READ file-name-1
[ INVALID KEY statement-1...] .
READ file-name-1
KEY IS alternate-record-keyname
[ INVALID KEY statement-1...] .
59
IS >
IS NOT LESS THAN
IS NOT <
[ INVALID KEY imperative-statement-1 ]
CLOSE file-name-1... .
DISPLAY identifier …
literal-1
[ WITH NO ADVANCING ]
ACCEPT identifier-1
[ FROM mnemonic-name-1]
GO TO paragraph-name-1.
PERFORM procedure-name-1 .
PERFORM procedure-name-1
UNTIL condition-1.
STOP RUN.
60
ADD identifier-1 ... TO identifier-2 ...
literal-1
IF condition
statement-1...
[ ELSE
statement-2 ... ] .
61
WHEN condition-1 imperative-statement-2 ...
NEXT SENTENCE
imperative-statement-2
NEXT SENTENCE
==pseudo-text-1== ==pseudo-text-2==
identifier-1 identifier-2
REPLACING literal-1 BY literal-2 ...
word-1 word-2
EXIT PROGRAM.
CHARACTERS
ALL identifier-2
LEADING literal-1
FIRST
62
SORT file-name-1
ON DESCENDING KEY data-name-1 ...
ASCENDING
USING file-name-2
INPUT PROCEDURE IS procedure-name-1 THRU procedure-name-2
THROUGH
GIVING file-name-3
OUTPUT PROCEDURE IS procedure-name-1 THRU procedure-name-2
THROUGH
RELEASE sort-record-name-1
[ FROM identifier-1 ]
RETURN sort-file-name-1
AT END imperative-statement-1
INTO identifier-4
63