You are on page 1of 32

Module 3

Overview:
This module provides the brief overview of the four divisions of COBOL program are
IDENTIFICATION DIVISION, ENVIRONMENT DIVISION, DATA DIVISION and PROCEDURE DIVISION. Each
division is discussed with examples for better understanding and learning of the COBOL program

Module Objectives:

After successful Completion of this module, you should be able to:

 Learn the basic character set, data types, constant and variables of the COBOL program.

 Discuss each division of the COBOL program

 Know and understand the basic syntax and rules of the 4 divisions of the program
Character set
In hierarchy 'Characters' are the lowest. They cannot divide further.

In COBOL character set consists of following characters:

Characters Description

A to Z Alphabets in Upper case

a to z Alphabets in Lower case

0 to 9 Numeric

+ Plus sign

* Asterisk

Space

- Hyphen or minus

, Comma

$ Currency sign

. Period or Decimal point

; Semicolon

( Left parenthesis

) Right parenthesis

“ Quotation marks

> Greater than

< Less than

' Apostrophe

= Equal sign

: Colon
COBOL Data Types

Data Types in COBOL


Data Types – Denoted by PICTURE (PIC) clause.

PIC clause provides the data type along with a length


1) Numeric – 0 to 9, maximum length is 18. Denoted by 9
2) Alphabet – A to Z, a to z or Space, maximum length is 255. It denotes ‘A’
3) Alphanumeric – Combination of numeric and alphabet. Denoted by ‘X’
These are 3 main data types in Cobol. \

We will cover all these in detail here

Numeric and other data types we use with numeric data types 
a) 9 –> Numeric – 0-9. Maximum length is 18. This holds actual memory space. Example PIC 9(2)
b) S –> Sign data type – It links a sign to a number. If this is present, the number is signed number.
If this is not present, the number is an unsigned number.
c) + sign -> Plus sign. Used to print (+) as sign
d) – Sign –> Minus sign. Used to print (-) as sign
e)  V –> Implied Decimal. Does not hold any memory space. Because it is not used to display but it
is useful for computation.
     Example: PIC 9(4)V99   –
  If the Value of this variable is defined as 123456, then it is like 1234.56 but it will not be
displayed like 1234.56. If we use this in computation, the value will be 1234.56 and this will take
part in computation
f). (Dot)–> Actual decimal point. Used for display, not for calculation or an arithmetic operation

     Example: PIC 9(4).99.


If the value is 123456, then it will display as 1234.56 but we can’t use this for computation
g)  Z –> Used to suppress only the leading Zeros with blanks. Does not do anything with non-zero

     Example: PIC ZZ99.99,


if we pass the value to this variable 0012.34 then it will display like bb12.34
h)  Comma –> To insert a comma into the data item at a particular position

i)   $ -> Dollar symbol – To insert a dollar sign at the first position. we normally use this for
currency

COBOL Variables

Variables in COBOL
 A variable is an identifier to hold a particular value or data. It identifies a memory
location.
 In COBOL variables are called as Data Names.
 It can be a maximum length of 30 characters.
 Variable must contain only digits(0-9), letters(A-Z), minus sign and Hyphens(-).
 A Variable must not be a reserved word of COBOL.
 A Variable should not contain any space in between, start or at end of the variable name.
Example of Variables
Examples of valid Variables are – 
 XYZ
 ABC-123
 A12
 9ABC
Examples of in-valid Variables are – 
 123_ABC – It does not allow Hyphen
 ACCEPT – This is a reserved word in COBOL
 ABC) – It does not allow Special characters
 MIK+*89
In the diagram which is shown below, we can see how variables are defined in COBOL.

Literals in COBOL
a) Literals are constants in COBOL. And it is directly hard-coded in the program
b) Literals are of 2 types-
Numeric literal
 Numeric literal allows a maximum of 18 numbers. Valid values allowed are
 0 to 9 (any number)
 One sign only( either + or -) which has to be used in left side only
 One decimal only(do not use at the end)
Example – 123
Non-Numeric( Alphanumeric) literal  
  Maximum 160 characters in length
   Must start and end with quotes
   Example –   “I AM AN EXAMPLE OF NON-NUMERIC LITERAL”                                                         
‘123’
Figurative Constants in COBOL
In COBOL Figurative Constants is reserved words. And these constants are predefined(build in) in
COBOL.

CAUTION:
Don’t use HIGH-VALUE/HIGH-VALUES/LOW-VALUE/LOW-VALUES with numeric fields
Divisions of COBOL program
A COBOL program consists of four divisions.

Identification Division

It is the first and only mandatory division of every COBOL program. The programmer and the
compiler use this division to identify the program. In this division, PROGRAM-ID is the only
mandatory paragraph. PROGRAM-ID specifies the program name that can consist 1 to 30
characters.

Environment Division

Environment division is used to specify input and output files to the program. It consists of two
sections −
 Configuration section provides information about the system on which the program is
written and executed. It consists of two paragraphs −
o Source computer − System used to compile the program.
o Object computer − System used to execute the program.
 Input-Output section provides information about the files to be used in the program. It
consists of two paragraphs −
o File control − Provides information of external data sets used in the program.
o I-O control − Provides information of files used in the program.

Data Division

Data division is used to define the variables used in the program. It consists of four sections −
 File section is used to define the record structure of the file.
 Working-Storage section is used to declare temporary variables and file structures which
are used in the program.
 Local-Storage section is similar to Working-Storage section. The only difference is that
the variables will be allocated and initialized every time a program starts execution.
 Linkage section is used to describe the data names that are received from an external
program.

Procedure Division

Procedure division is used to include the logic of the program. It consists of executable
statements using variables defined in the data division. In this division, paragraph and section
names are user-defined.
There must be at least one statement in the procedure division. The last statement to end the
execution in this division is either STOP RUN which is used in the calling programs or EXIT
PROGRAM which is used in the called programs.

I. RULES FOR INTERPRETING INSTRUCTION FORMATS

This module will follow specific format in typing program syntax. Rules are as follows:

A. Uppercase words are COBOL reserved words that have special meaning to the compiler.
B. Lowercase words are use-defined entries. They are within curly braces { }.
C. Underlined words are required in the paragraph.
D. If punctuation is specified in the format, it is required.
E. Brackets [ ] mean the clause or paragraph is optional.
F. The use of dots or ellipses (…) means that additional entries of the same type may be
included if desired.

II. FOUR DIVISIONS OF COBOL


A. IDENTIFICATION OF DIVISION
Syntax:
IDENTIFICATION DIVISION.
PROGRAM-ID. program name
[AUTHOR. {comment-entry}…..]
[INSTALLATION. {comment-entry…..}]
[DATE-WRITTEN. {comment-entry…}]
DATE-COMPILED.
[SECURITY. {COMMENT-ENTRY….}]
REMARKS.

IDENTIFICATION DIVISION is the first division of CPBOL program. The function of this
division is to supply information about the program to others who may use it as reference. It
describes the program to potential users. It states the name of the program and other optional
information such as information regarding author, the date the program was written, security.
Etc. its purpose is to identify program and its author and to provide other general information
about the program, such as the dates the program is written and compiled, any program security,
and so forth. They start in column-8 of the COBOL coding sheet. The only required paragraph in
this division is the PROGRAM-ID paragraph while the rest is optional.

TIP:

 A clause with a combination of two words is always connected with hyphen.


Ex. PROGRAM-ID, INPUT-OUTPUT SECTION, FILE-CONTROL
 Take note also of how the statement is separated by period.
The following example includes optional paragraphs.

Fig. 1 Example of IDENTIFICATION DIVISION

All paragraph-names start in column 8 and, as indicated above, are optional with
exception of the PROGRAM-ID. The compiler does not process what follows the COBOL words but
only prints the content. Thus, after the DATE-WRITTEN we could have written ANYTIME IN JUNE.
The compiler derives no more meaning from it, therefore, the programmer should be concerned
simply with choosing verbal descriptions that will be meaningful to the potential readers of the
program.

The DATE-COMPILED paragraph may be left blank. The compiler will insert the actual date
and the source listing will include that date.

B. ENVIRONMENT DIVISION

Syntax:
ENVIRONMENT DIVISION.
[CONFIGURATION SECTION.
SOURCE-COMPUTER. {computer name}
OBJECT-COMPUTER {computer name}
SPECIAL NAMES {computer name}
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT {filenamne-1}
ASSIGN TO {implementor-name-1}….]
ENVIRONMENT DIVISION is the second division of COBOL program. It describes the
“computing environment” of the program. The “computing environment” refers to the type of
computer hardware on which the program is written and run. It is the only machine-dependent
division of a COBOL program. It is the only machine-dependent division of a COBOL program. It
supplies information about the computer equipment to be used in the program. That is, the
entries in this division will be dependent in [1] the computer system and [2] the specific devices
or hardware used in the program. This division also briefly describes the data files required by the
program.
The ENVIRONMENT DIVISION is the only division of a COBOL program that will change
significantly if the program is to be run on a different computer. Since computers have various
models and equipment, each computer center will require division. The CONFIGURATION SECTION
and the INPUT-OUTPUT SECTION, which are again divided into paragraphs.

SECTION OF THE ENVIRONMENT DIVISION

1. CONFIGURATION SECTION

Syntax

[CONFIGURATION SECTION.
SOURCE-COMPUTER. {computer-name}
OBJECT-COMPUTER. {computer-name}
SPECIAL-NAMES. {computer-name}

It supplies the information about the computer on which the COBOL program will be
compiled and executed. (It is optional in COBOL-85) Configuration section provides
documentation information which includes the computer manufacturer, computer number and
computer model number. The two sections are coded in column 8. Configuration section has
three paragraphs:

a. SOURCE-COMPUTER – The computer that will be used for compiling the program.
b. OBJECT-COMPUTER – The computer that will be used for executing or running the program.
c. SPECIAL-NAMES – A third paragraph that is optional in the configuration section. This
focuses on special devices used for reading or displaying data.

Fig. 2 Sample of CONFIGURATION SECTION

Each paragraph name is directly followed by a period and then a space. The designated
computer IS ALSO FOLLOWED BY A PERIOD. In the example, the source and object computers are
the same. In general, this will be the case, since compilation and execution are usually
performed on the same computer. If, however, the program will be compiled on one model
computer and executed, at some future time, on another model computer, these entries will
differ. This is also optional in COBOL program.
2. INPUT-OUTPUT SECTION

Syntax:

FILE-CONTROL.
SELECT {filename}
ASSIGN TO {implementor-name-1}

The INPUT-OUTPUT SECTION supplies the information about the specific devices used in
the program namely: terminals, printers, and disk drives. It is also optional but is being used most
of the time since programs use files. It supplies information concerning the input and output
devices used in the program. This is started by the FILE-CONTROL paragraph. The FILE-CONTROL
paragraph consists of SELECT statements. A SELECT statement defines a filename and assigns a
device to that file (A file is the major collection of data for a given application). Typically, we
have an input file and an output file. Input file is a collection of data which serves as a reference
for processing of data and the output file is a second collection of data which serves as an output
report. The implementor-name is not a COBOL reserved word nor user-defined word. It is
machine-dependent device specification provided by the computer center.

TIP:

 A filename must correspond to the rules of forming user-defined names stated above (i.e.
maximum of 30 characters, letters, digits, and hyphen only, no embedded blanks, at least
one alphabetic character and not a reserved word).
 It is more logical to define the input file first before the output file.
 Write SELECT statement on two lines for better readability. The best format is as follows:
SELECT filename
ASSIGN TO device

 A filename must be specific and meaningful. Avoid acronyms, person’s name and aliases.
Typically, a filename must be related to the subject of the program. If the program is
regarding sales, it can be regarded as SALES-INPUT on input file and SALES-OUTPUT on
output file.

 Assigned filename must be enclosed in double quotes.


Ex. SELECT WORKREC ASSIGN TO “WORKREC.DAT”

Fig. 3 Sample of INPUT-OUTPUT SECTION


The implementor-name varies depending on the computer used. It can rather be cryptic as:
SELECT CUSTFILE ASSIGN TO SYS005-UR-2540R-S or can be easily as: SELECT GRADEFILE ASSIGN TO
“MYFILE.DAT”. The program syntax of Implementor-name is as follows:

SELECT {filename} ASSIGN TO {DISK Drive Letter:\sub-directory\filename


{PRINTER

The drive Letter\sub-directory is the path where filename is located.

Example:

SELECT SALES-INPUT ASSIGN TO B:\DATA\SALES-INPUT.DAT.

It must be noted that PRINTER is commonly used in an output file and that there must be a
connected printed to it.

C. DATA DIVISION

Syntax

DATA DIVISION
FILE SECTION
FD filename
[LABEL RECORDS IS STANDARD]
[LABEL RECORDS ARE OMMITED]
[RECORD CONTAINS _______ CHARACTERS]
[DATA RECORD IS _________________]
01 record name
WORKING-STORAGE SECTION
{record description entry
variables description entry
77-level description entry

DATA DIVISION is the one concerned with the identification and description of storage fields and
data used by program. It describes the input and output files to specific devices in the INPUT-
OUTPUT SECTION of CONFIGURATION SECTION. It defines and describes fields, records, and files in
storage. Whatever variables and constant used in PROCEDURE DIVISION statements are declared
here. It consists of two sections: the FILE SECTION and the WORKING-STORAGE SECTION.

THE TWO MAIN SECTIONS OF THE DATA DIVISION

A. FILE SECTION – Defines all input and output files including the records needed.

Syntax:

DATA DIVISION.
FILE SECTION.

FD filename
[LABEL RECORD IS STANDARD]

[LABEL RECORDS ARE OMMITED]

[RECORD CONTAINS ______ CHARACTERS]

[DATA RECORD IS _________________]

1 RECORD NAME.

1. FILE DESCRIPTION ENTRIES

A. FD clause – is an abbreviation for file description and is coded in Area A. this


paragraph is followed by file name that must correspond exactly, character for
character, to a file that is already mentioned in the ENVIRONMENT DIVISION.

B. LABEL RECORD or LABEL RECORDS clause – These clauses are optional for COBOL 85.
This used to indicate if a header and a trailer record are to be created by system.
Both records provide file identification information about the file. The first record will
bear the header label while the last record will be a trailer label. These are created
on output files and checked on input files. If LABEL RECORDS is used, these records
are not created. This is typically used on files that are directed to a printer or screen
which do not require the header and trailer records.

C. RECORDS CONTAINS clause – clause that indicates the number of characters in record.
Data entered on a terminal is typically specified with the clause RECORD CONTAINS 80
CHARACTERS, although the number of character will depend on how many characters
can be stored on one line of the specific terminal.

For disk or tape files, the RECORD CONTAINS clause varies. One of the
advantages of storing data on the media is that records can be of any size.

D. BLOCK CONTAINS clause – indicates the blocking factor for disk or tape.

E. DATA RECORD clause – pertains to the name of the designated record.

RULES FOR CODING DESCRPTION ENTRIES


 FD is coded in Area A.
 All other entries should be coded in Area B (i.e. filename and LABEL RECORDS)
 If LABEL RECORDS clause is used, no period is coded from FD until the last clause
has been specified.
 Commas are always optional in a program to separate clause. If used, they must
be followed by at least on blank.
 It is recommended that each clause appear on separate line for clarity and ease
of debugging.
Fig. 4 Sample of FILE SECTION of DATA DIVISION

2. RECORD DESCRIPTION ENTRIES

We have earlier defined record as a collection of related data items within a file.
After a file is declared in FD clause, the record description entries follow. Record
description specifies the format of a record. There are various level numbers in a record.
Record name takes the value of 01. this is considered as the highest level of data in a file.
Since record is divided in fields, all other subordinate field is coded with any level number
between 02 and 49. level 01 is coded in Area A and all other levels are coded in Area B.

Fig. 5 Sample of FILE SECTION of DATA DIVISION


Fig. 6 describes INREC as level 01, AREA-CODE, SALENO and SALE-NAME are all coded in Level 05.
all items that are coded on the same level are not subordinated to or related to one another.
These are called independent items. Note that level 05 is used and not level 02. we will use levels
5, 10, 15 and so on in our further discussion. This is to provide additional levels in case insertion
of field is needed. In case the SALE-NAME field is further divided as LASTNAME, FIRSTNAME and
MIDINITIAL, this will be declared as:

SALE-NAME becomes a group data field and its subordinate an elementary field. Note that
declaration of data field length and type is coded on elementary field.

Although it is legal to choose any level numbers to describe the data fields that are at the
same level. The following example illustrate this point.

Fig.6 Sample of Record Entry with Invalid Elementary and Group Level

B. WORKING-STORAGE SECTION – this is where variables and constant which not part of input
but nonetheless required for processing are declared. These include counters, end-of-file
indicators, and work areas.

Syntax:
DATA DIVISION.
WORKING-STORAGE SECTION
01 [variable entry]
01 [variable entry]
01 [variable entry]
05 [variable entry]
05 [variable entry]

The WORKING-STORAGE SECTION immediately follows the FILE SECTION in the program.
This section provides for the storage of data items that are not part of any section, such as
intermediate calculations, report headings for printing and numeric constants for use in
calculations. The procedure for declaring an elementary and group item is still the same
however, in older version of COBOL, it uses a special level number 77 to describe all
elementary independent items. The practice is not common now since using level 77 and level
01 is just the same. It is also suggested to put a prefix of WS for every variable entry to
distinguish it from those in FILE SECTION.
RULES FOR USING WORKING-STORAGE SECTION.

 The WORKING-STORAGE SECTION must succeed the FILE SECTION. It must not
be encoded before the FILE SECTION.
 WORKING-STORAGE SECTION is coded on Area A and ended with a period.
 Group item that will be submitted into other subordinate levels must be
defined PIC clause must appear in the elementary item.
 Elementary item must contain a PIC clause. It may contain an initial value using
VALUE clause. VALUE clause may precede or succeed PIC clause.
 VALUE clause is used only in the WORKING-STORAGE SECTION and not in the
FILE SECTION.
 All items must follow the rules for forming data-names.

2. USES OF WORKING STORAGE SECTION

a. For storing intermediate results, counters and end-of-file indicators through the use of variable
data field.

b. For storing initial value not contained in the input file.

III. DATA ITEM ENTRY AND CLAUSES

There are item entries and clauses which define the type of data that will be used in
variable declaration.

A. THE PICTURE CLAUSE FOR DATA DESCRIPTION

After declaring the level numbers, the term PIC follows. PIC is short for PICTURE clause.
Each elementary data field must be defined by a PIC clause that provides information as to
its(numeric, alphabetic, or alphanumeric.) and its size. A specific picture character A gives a
specific type of data. It is also used to declare the size or length of the field (i.e. the number
enclosed in parentheses). Size or length of the fields pertains to a maximum number of characters
that will be accepted by the program. The PIC clause is written at the end of the data field
(leaving at lest one blank space after the data field name with the word PIC followed by at least
one blank space and the appropriate picture character (X or 9 or A). if a data field requires more
that one picture character, the number of characters to which the picture characters applies be
enclosed in parentheses following the picture character.

RULES FOR CHOOSING PIC CLAUSE

 A group data field must not have a PIC clause.


 The word PIC can start in any column after the data field. However, for easy readability,
the word PIC for all data fields should start in the same column.
 There must be at least one blank space between the word PIC and the picture characters.
There must not be a space between the character type and field length enclosed in
parentheses.
 If a data field requires more than one picture character, the number of times the picture
character appears can be enclosed in parentheses. Thus, PIC 9999 is the same as PIC9(4).
 Each group data field must end with a period. Each elementary data field must also end
with a period after the picture characters.
 It is important to understand the distinction between a numeric data value that can be
contained in a data field with 9’s for picture characters and in a data field with X’s as
picture characters. Only a data field that specifies “9” for picture characters can contain
numeric data that is to be used in a calculation.

1. The 9 PICTURE Character

In fig. 10 AMOUNT is of numeric type with a maximum digit of 7. The numeric 9 indicates
that a storage position should only contain any of the numeric digits from 0 to 9. If the input data
is of numeric type, it cannot contain anything else other than numbers, not even a decimal point.
Leading spaces may however be allowed. Thus, a data value with decimal point as in 1200.50
cannot be directly stored in a numeric data field. The following examples illustrate how the letter
V works in PIC clauses.

Data Value Stored Data Field’s PIC Clause Value Contained

12050 PIC 999V99 120.50

020050 PIC 9V999 1.200

125 PIC 99V9 12.5

Again, note that a numeric field can contain only the digits 0-9. Blanks are not numeric
characters. When a value does not fill a numeric 9 field completely, the value is justified to the
right, and the extra positions are filled with zeros. When entering data, you should be careful to
zero-fill a field with leading zeros, otherwise you may be in for some surprising results. Thus, in a
field of six positions the numeric value 143 should be entered as follows:

000143

2. The V PICTURE Character

The V character indicated the position of an assumed decimal point. This means that the
decimal point is not written as part of the field and therefore is not included as part of the field
size. This does not occupy a storage position. Instead, the information about decimal-point
location is stored elsewhere in the computer, so that any arithmetic computations can be done
correctly.

3. The X PICTURE Character

The X PICTURE character denotes that alphanumeric positions are contained in a field. Fig.
9 declares AREA-CODE as of alphanumeric type and of size 1, SALENO is alphanumeric and size 10,
SALE-NAME is alphanumeric and can contain a maximum of 25 characters. This means that it can
include alphabetic characters, numeric characters and special symbols. The PICTURE X is most
likely to use in variable NAME because there are names which contain special symbol as
“SHAQUILLE O’NEAL”. When the characters do not fill an X field completely they are left justified,
with blanks filling the remaining positions in the right.

4. The A PICTURE Character

The A PICTURE character is similar to the X character, except that it indicates only
alphabetic characters and blanks.

5. The P PICTURE Character


The P PICTURE character is used in conjunction with the V character to indicate the
position of a decimal point in cases where the decimal po8int is not within the number. The P
character is not used very much in administrative applications. It is used best to scientific
computational needs, which are likely to be better satisfied by the use of languages other than
COBOL. This character is used, for example, when it is understood that a value held in storage
represent thousands of units and we wish to indicate the decimal position for this value. The
following examples indicate the use of this character. As before, the caret indicates the position
of an assumed decimal point.

Numeric Value Data Field’s PIC Clause Equivalent

12 PIC 99PPPV 1200^

1250 PIC VP(3)9(4) ^0001250

6. The S PICTURE Character

The S character is used to designate a numeric field that is signed (i.e., one that can be
negative in value.) In COBOL, all fields are considered positive unless the S has negative value.
Only one S character may be used in a field. It is coded in the leftmost character. The S is not
counted in the size of the filed, and therefore S999 is a field of three positions. In the following
examples, the negative sign in machine representation is as shown “-“ on top of the rightmost
digit, in order to preserve the concept that it does not take up an extra position.

Numeric Value Data Field’s PIC Clause Storage Value

250.25 PIC S9999V99 0250^25

-1250.25 PIC S9999V99 ^1250^25

B. THE FILLER CLAUSE

FILLER is a COBOL reserved word used to define areas within a record that will not be
referenced individually during processing. This is a generic data name used extensively in the
DATA DIVISION with respect to data items that are not referenced specifically in the PROCEDURE
DIVISION.
Fig. 7 Example of Data Division using FILLER command.

FILLER clause is also optional in record description. The following is acceptable:

Fig. 8 Example of Data Division without FILLER command.

C. THE VALUE CLAUSE

This process of assigning an initial value to a data field is known as initialization and is
performed by using a VALUE clause. A VALUE clause consists of the word VALUE followed by the
initial value, and is placed immediately after the PICTURE clause. The initial value can be a
numeric literal, a nonnumeric literal, or a figurative constant. Rules for this paragraph Literal and
figurative constants were discussed earlier.

Example of VALUE Clause Using Numeric Literals

05 WS-DISCOUNT PIC V99 VALUE .15.

05 WS-LABORATORY-FEE PIC 9(3) VALUE 350.

05 WS-TAX-DEDUCTION PIC 9(3)V9(3) VALUE 125.250.

Examples of Value Clause using Nonnumeric Literals

05 WS-TITLE PIC X(14) VALUE “MONTHLY REPORT”.

05 WS-MORE-RECORDS PIC X(3) VALUE “YES”.

05 WS-TITLE PIC X(24) VALUE “ALET’S FINANCIAL REPORT”.


D. THE PICTURE CLAUSE FOR DATA EDITING

FUNCTION OF DATA EDITING

 Printing of decimal points where decimal alignment is implied.


 Suppression of leading zeros.
 Printing of dollar signs and commas.
 Printing of asterisks for check protection.
 Printing of plus and minus signs.
 Printing of debit or credit symbols for accounting applications.
 Printing of spaces or zeros as separators within fields.

The purpose of editing is to increase readability of the report. Editing is associated with printing
data on the printer. All editing is accomplished by moving an elementary item to a report item
which contains appropriate edit symbols. The editing function involves a change in the form of
data. 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, and so forth. This module will
not tackle the $, +, -, DB, CR, * picture characters since these are not typically used.

1. The Decimal and the Comma PICTURE Insertion Characters

Decimal (.) PICTURE character indicates the position of the decimal point and serves to
align the actual decimal values in the field. Only one decimal point must appear in a field while a
field may contain more than one comma if the size of the field conforms to it. Each of these
insertion characters is used to indicate the position of the indicated character in the storage
location. A field cannot contain both V and . PICTURE characters.

Numeric Value Data Field’s PIC Clause Storage Value

2,500.25 PIC 9,999.99 2,500.25

250.25 PIC 9,999.99 0,250.25

25 PIC 9,999.99 0,025.000

2. The Z PICTURE Character

The Z PICTURE character is used to replace zeros by blanks. Zero-suppression terminates


when the firs non-zero digit or the ._ decimal character is encountered whichever occurs first.
The letter b in the following example represents a blank space.

Numeric Value Data Field’s PIC Clause Storage Value

2,500.25 PIC Z,ZZZ.ZZ b2,500.25

250.25 PIC Z,ZZ9.99 bb250.25

25 PIC ZZ.9 b25


3. The B PICTURE Character

The B PICTURE character is used to insert blank in the data entry. This is used to increase
readability of the data entry. For example, 05 SALENAME PIC X (!0) BX (14) BX. The first 10
characters will be written first then a blank is inserted another 14 characters before another blank
is inserted.

4. The 0 PICTURE Character

The Zero insertion character is used to insert zeros in the designated position. This is best
used when we want to show the full value of digits. Let’s say, in thousands form. For example 05
WS-POPULATION PIC 9,999,000 will result in 2,500.00. Assuming it has value of 2500.

5. The / PICTURE Character

The / PICTURE character is used to insert a stroke or forward slash in the designated
position. This is best used is editing date of birth. For example,

05 DATE OF BIRTH PIC 9(6) VALUE 011591

05 DOB PIC 99/99/99

Further processing as MOVE DATEOFBIRTH TO DOB will cause DOB to contain 01/15/91.

Summary of Characters Available for Use in PICTURE Clause

TYPE OF CHARACTER SYMBOL USE

Field Definition Characters

9 Numeric field

A Alphabetic field

X Alphanumeric field

Numeric Field Special Character

V Assumed decimal point

P Decimal scaling

S Positive or negative sign


Editing Characters

$ Insertion of dollar sign

Z Zero suppression

* Check protection

. Decimal point

, Comma

+ Plus sign

- Minus sign

DB Debit sign

CR Credit sign

B Blank insertion

0 Zero insertion

/ Stroke insertion

D. Procedure Division

OPEN Statement

The OPEN statement used to link the actual physical file to a program file and prepare the
file to be processed. The file must be opened before data can be read from or written to a file.
The OPEN statement has the following format:

INPUT

OPEN OUTPUT filename-1, filename-2,..., filename-n

I-O

EXTEND

The filename must be exactly the same as it is in the SELECT statement of the
ENVIRONMENT DIVISION and the FILE SECTION of the DATA DIVISION.

A file may be opened for one of the following purpose:

1. INPUT
2. OUTPUT
3. I-O
4. EXTEND
When the program needs to read data from a file, the file must be opened for INPUT. A file is
opened for OUTPUT when the program will write data to the file. Only direct access files can be
opened for I-O and therefore the file should be stored in a direct access storage medium such as
magnetic disks. A file is opened in EXTEND mode when additional data are to be added to the end
of the file. To use a file in EXTEND mode, the file must already exist.

Examples:
OPEN INPUT STUDENT-FILE.
OPEN OUTPUT REPORT-FILE.
OPEN I-O EMPLOYEE-FILE.
OPEN EXTEND SALES-FILE.
In the first statement the file STUDENT-FILE must be opened for INPUT function only and
the second statement, REPORT-FILE is opened for OUTPUT function only. The third statement
opened EMPLOYEE-FILE as I-O, which means it can be used as an INPUT and at the same time as
OUTPUT. In the fourth statement, SALES-FILE was opened using the EXTEND option which means
that any new record written to it will be appended to the end of the file.

A single OPEN statement may be used to open more than one file.

Each filename must be preceded by the keyword INPUT or OUTPUT. A period is placed at the end
of the last file name only.

Example:

OPEN INPUT STUDENT-FILE


OUTPUT REPORT-FILE

A single OPEN statement can also be used to open several input and output files.

Example:

OPEN INPUT FILE1, FILE2, OUTPUT FILE2, FILE4.

READ Statement

The READ statement reads data from the input file. The data are read one record at a
time. When processing a sequential input file, the READ statement reads records in sequence.
The first READ statement reads the first record, the next record reads the second record, and so
on. The READ statement has the following format.

READ filename AT THE END imperative statement.

The READ statement refers to the name of the input file defined in the INPUT-OUTPUT
SECTION of the ENVIRONMENT DIVISION.

The AT END clause of the READ statement tests whether the end of the file has been
reached. If the READ statement provided with the AT END clause execution time or a run time
error will result.

Example

READ STUDFILE AT END MOVE 1 TO EOFSW.


In the give example, the records of the STUDFILE will be sequentially read until the end of
the file is reached. The imperative statement MOVE 1 TO EOFSW will be executed only if all
records in the file have been read already or a run time error was encountered. In this READ
statement, the PIC clause of EOFSW must be defined as a numeric data field with one digit (PIC
9). The value moved to the EOFSW, can be anything that the programmer wants it to be as long
as its PIC clause is appropriate for that value.

MOVE Statement

The MOVE statement moves a literal or the contents of a data field to another data
field. The data field receiving the value is called the receiving data field. The data field from
which the data are copied is the sending data field. There are two types of the MOVE
statements: the direct MOVE in which a value is directly moved to a data field, and the indirect
MOVE, in which a value contained in data field is moved to another data field. The general format
of the direct MOVE statement is

MOVE value TO data-field.

Examples:

MOVE 1 TO EOF-SW.
MOVE “Y” TO OPTION.
MOVE 20 TO COUNTER.
In the first example, the value 1 was moved to the data field EOF-SW. The second
example moved the character “Y” to the data field OPTION which was defined in the DATA
DIVISION having an alphabetic or an alphanumeric picture. The third example moved the value 20
to the data field COUNTER which was previously defined in the DATA DIVISION having a numeric
picture.

When moving a data value, it is important to ensure that the receiving data field is
correctly defined for that value. If a numeric value is moved, the data field should be defined as
numeric with an adequate number of digits. If an alphanumeric value is moved, the data field
should be defined as alphanumeric with an adequate number of characters.

In the previous examples, the data fields to which data values are moved should be
defined in this way:

05 EOF-SW PIC 9.
05 OPTION PIC X.
05 COUNTER PIC 99.

It also possible to use figurative constants with the MOVE statement. Figurative constants
have predefined meanings to the COBOL compiler. The following table gives the meanings of
some commonly used figurative constants when used in the MOVE statements.

Figurative Constants Meaning

SPACE(S) Fills the field with blank spaces.


ZERO(S) Fills the field with zeroes.
HIGH-VALUE(S) Fills the field with the highest binary value
The computer system being used is able to represent it.

LOW-VALLUE(S) Fills the field with the lowest binary value


The computer system being used is able to represent it.

If a record named REMARKS is defined as:


05 REMARKS PIC X(10).
Its value can be initialized to 10 blank spaces using this MOVE statement.
MOVE SPACES s REMARKS.

The movement of data to the receiving data field depends on the type of data being
moved. If alphanumeric or alphabetic data are being moved, the movement starts from the
leftmost character. If numeric data are being move, the movement starts from the rightmost
digit. If the receiving data field is longer than the value being moved, the additional positions are
padded with blanks on the right if the data field is defined as alphanumeric, and with zeroes on
the left if the data field is defined as numeric. Truncation or cutting off of the value occurs from
the right is the data field is defined as alphanumeric, and from the left of the value if the data
field is defined as integers. The following examples show the effect of the MOVE statements on
longer and shorter receiving data fields:

RECEIVING DATA FIELD


Name PIC clause MOVE statement Contents of Receiving Data Field
REMARKS PIC X (10) MOVE “PASSED” TO REMARKS. PASSED
REMARKS PIC X (5) MOVE “FAILED” TO REMARKS. FAILED
COUNTER PIC 999 MOVE 20 TO COUNTER. 020
COUNTER PIC 99 MOVE 100 TO COUNTER. 00
COUNTER PIC 9 MOVE 123 TO COUNTER. 3

In the first example, four remaining positions on the right of the receiving data field are
padded with blanks. In the second example, truncation of the data value occurs from the right.
In the third example, a data field is padded with a zero on the left. In the fourth and fifth
examples, Truncation of data values occur from the left.

Either the VALUE clause or the MOVE statement may be used to assign an initial value to a
data field. The VALUE clause assigns the value during compilation, whereas the MOVE statement
assigns it during program execution. In general, if the value of data field is not going to change, it
is better to use the VALUE clause because it uses less execution time. However, if the value
needs to be continually reinitialized, the MOVE statement should be used.

The general format of the indirect MOVE statement is

MOVE data-field-1 TO data-field-2.

The following examples illustrate the indirect MOVE statement.

MOVE EMPNAME TO EMPNAME-OUT.


MOVE SALARY TO SALARY-OUT.
Contents of data fields before MOVE

PATRICK
EMPNAME EMPNAME-OUT

Contents of data fields after MOVE

PATRICK PATRICK

EMPNAME EMPNAME-OUT

The PIC clauses of both the sending data field and the receiving data field play an important role
in what happens after the MOVE statement is executed. If the PIC clauses of both data fields are
exactly identical and exact copy of the data value is moved from the sending data field to the
receiving data field. If the PIC clause of the receiving data field is longer or shorter than that of
the sending data field, the extra spaces are padded, or truncation occurs, in the same way as
mentioned for direct moves.

RULES FOR THE MOVE STATEMENT

1. Non numeric literals or data values may not be moved to alphabetic data fields.
2. Data may move from a non numeric field to a numeric field only if the non numeric field
contains a valid integer (It cannot be a real number). However, the practice is
discouraged because of unpredictable results. If the field does not contain valid integer
data, an error will occur.
3. Non numeric literals or data values may not be moved to numeric data fields.
4. When a receiving field is non numeric, the value moved into it is left-justified in the field.
This means that the first character is placed in the first position on the left side of the
data field. If the field is larger than the data value, blank spaces will be placed on the
right side to fill out the field. If the size of the field is smaller than the data value, the
value will be truncated to fit in the field. Any extra characters on the right side will be
cut off.
5. When the receiving field is numeric, the data will be aligned at the decimal point. If there
are fewer digits than the size of the field, zeroes will be added to fill out the field. If the
numeric data are longer than the field, the number will be truncated to fit into the field.

EFFECTS OF PIC CLAUSE ON DATA MOVES


Receiving Data field
---------------------------------------------------------------------------------------------------------
Sending Data field LONGER SHORTER
Alphanumeric Pad on the RIGHT with blanks Truncate on the RIGHT
Decimal
Integer position Pad on the LEFT with zeroes Truncate on the LEFT
Decimal position Pad on the RIGHT with zeroes Truncate on the RIGHT
Integer Pad on the LEFT with zeroes Truncate on the LEFT

WRITE Statements
The function of the WRITE statement is to write a record to an output file. WRITE
statement has the following format.

WRITE output-record-name

The data field output-record-name must be the same as that declared in the FILE SECTION
of the DATA DIVISION. The WRITE statement causes all data fields that are part of the record to
be written, one after another, to the output file.

Before writing an output record, make sure that a data value has been moved to each data
field that is part of the output record.

The WRITE statement is always followed by the name of the output record, not the output
file. Numeric edited (rather than numeric) fields should be used to output numeric data that are
destined for a printer.

Example:

WRITE EMPLOYEE-REC.

CLOSE Statement

The CLOSE statement closes a file so that it is no longer available for processing. All files
that are opened at the beginning of a program should be closed before ending the program. The
format of the CLOSE statement is

CLOSE filename-1, filename-2, … filename-n.

More than one file may be closed with one CLOSE statement. If more than one file is
closed with on CLOSE statement, the period appears at the end of the last file name. Do not use
the keyword INPUT or OUTPUT before the file name in a CLOSE statement.

Example

CLOSE STUDENT-FILE
CLOSE EMPLOYEE-FILE TAXTABLE-FILE REPORT-FILE

STOP RUN Statement

The STOP RUN statement terminates execution of the program. As soon as this statement
is encountered, the program stops execution. The format of the STOP RUN statement is

STOP RUN.

The STOP RUN statement does not have to the last physical statement in the program, but
it must be the last statement that is executed by the program.

ARITHMETIC STATEMENTS

ARITHMETIC STATEMENTS perform arithmetic calculations. The basic arithmetic


statements are ADD, SUBTRACT, MULTIPLY, DIVIDE, and COMPUTE. The first four of these
statements can be used both with and without a COBOL feature called the GIVING clause.

ADD Statement
The ADD statement is used to add the contents of two or more numeric data fields. The
format of the ADD statement is

ADD datafield-1 TO datafield-2.

This statement performs two functions: it adds the contents of datafield-1 and datafield-2
and stores the result in datafield-2. With an ADD statement, the result is stored in the data field
listed last. On execution of the ADD statement, the contents of the data field that receives the
result of the calculation are changed. However, the contents of the other data field remain
unchanged.

Examples:

ADD SALARY TO TOTAL-SALARY

SALARY TOTAL-SALARY
PIC 9(5) PIC 9(7)

Before Execution
50000 0

After Execution
50000 50000

ADD PRODUCTION TO TOTAL-PRODUCTION

PRODUCTION TOTAL_PRODUCTION
PIC 9(4) PIC 9 (6)

Before Execution
200 500

After Execution
200 700

When using the ADD statement, care must be taken to ensure that the data field that
receives the result of the calculation is large enough to hold the result. Otherwise, overflow
occurs and the result stored will be incorrect.

More than two data fields can be used in the ADD statement.

Example:

ADD QUIZ1 QUIZ2 QUIZ3 TO TOTAL-QUIZ.

In this example, the content of the data TOTAL-QUIZ has been changed, but the contents
of the other three data fields remain unchanged.

A numeric literal can be used in the ADD statement. However, the result of the calculation may
not be stored in a numeric literal. The result of the calculation must always be in a data field in
order to be valid.

Example:
ADD 1 TO RECORD-COUNTER.
ADD 100 TO SALARY.

RULES FOR THE ADD STATEMENT

1. The contents of the first data field are added to the contents of the last data field.
2. The result is always stored in the last data field. All other data fields remain unchanged.
3. Any number of data fields may be listed in an ADD statement. They all will be added to
the last data field.
4. The last data field must be large enough to hold the sum.
5. Numeric literals may be used in add statements. However, the sum must be stored in a
data field. The last item in an ADD statement must be a data field.
6. All data fields that take part in a calculation must be numeric with PIC 9s (may have an S
or V). The data field that holds the result must be either numeric or numeric edited. If it
is numeric, the data field can be used in other additional arithmetic statements.
However, if it is numeric edited, the data field may not be used in other arithmetic
statements. This rule applies not only to the ADD statement, but also to all other
arithmetic statements.

SUBTRACT Statements

The SUBTRACT statement subtracts the contents of two or more data fields. It has the
following format

SUBTRACT datafield-1 FROM datafield-2.

The SUBTRACT statement subtracts the contents of datafield-1 from the contents of
datafield-2 and stores the result of the calculation in datafield-2. The contents of datafield-1
remain unchanged after the statement is executed.

Examples:

SUBTRACT DISCONT FROM PRICE.


DISCOUNT PRICE
PIC 9(3) PIC 9(5)

Before Execution
250 1000

After Execution
250 750

SUBTRACT EXPENSES FROM INCOME.


EXPENSES INCOME
PIC 9(4) PIC 9(6)

Before Execution
1000 25000

After Execution
1000 24000
It is possible to subtract the contents of several data fields from that of another data field.

Example:

SUBTRACT WITH-TAX, MEDICARE FROM GROSS-INCOME.

The contents of WITH-TAX and MEDICARE remain unchanged, the contents of GROSS-
INCOME was changed.

It is possible to use a numeric literal in the SUBTRACT statement.

Example:

SUBTRACT 1000 FROM PRICE.

RULES FOR THE SUBTRACT STATEMENT

1. The contents of the first data field are subtracted from the contents of the last data field.
2. The result is always stored in the last data field. All other data fields remain unchanged.
3. Any number of data fields may be subtracted from the last data field.
4. As with ADD statements, numeric literals may be used in SUBTRACT statements. However,
the result must be stored in a data field. The last item in a SUBTRACT statement must be
a data field.

MULTIPLY Statement

The MULTIPLY statement multiplies the contents of two data fields.

The format of the MULTIPLY statement is

MULTIPLY datafield-1 BY datafield-2.

This statement multiplies the contents of datafield-1 and datafield-2 and stores the result
in datafield-2. The contents of datafield-1 are not changed after the statement is executed; the
contents of datafield-2 are changed.

Examples:
MULTIPLY RATE BY PRICE.
RATE PRICE
PIC 9(3) PIC 9(5)

Before Execution
10 150

After Execution
10 1500
The result in product is stored in PRICE.

RULES FOR THE MULTIPLY STATEMENT


1. The contents of two data fields are multiplied together.
2. The result is always stored in the second data field. The other data field remains
unchanged.
3. A MULTIPLY statement can involve only two data fields.
4. Care must be taken to ensure that the PIC clause of the data field where the result of the
multiplication will be store is large enough to hold the result.
5. As with ADD and SUBTRACT statements, numeric literals are allowed in the MULTIPLY
statement, but the result of the multiplication must be sorted in a data field.

DIVIDE Statement

The DIVIDE statement divides the contents of one data field by the contents of another
data field. There are two methods of writing the DIVIDE statement.
The format of the first type is

DIVIDE datafield-1 BY datafield-2.

The dividend is the number being divided and the divisor is the datafield-2. In a DIVIDE
statement, the result of the division is saved in the dividend data field. Thus, the above DIVIDE
statement saves the result in datafield-1.

The format of the second type is

DIVIDE datafield-1 INTO datafield-2.

In this statement, the dividend is datafield-2 and the divisor is datafield-1. The result of
division is stored in datafield-2.

Example:
DIVIDE 10 INTO AVE.
DIVIDE AVE BY 10.
DIVIDE SALES BY RATE.
DIVIDE RATES INTO SALES.

RULES FOR THE DIVIDE STATEMENT

1. There are two formats for the DIVIDE statement. DIVIDE BY and DIVIDE INTO.
2. When the DIVIDE BY statement is used, the first data field is the dividend and the second is
the divisor.
3. When the DIVIDE INTO statement is used, the first data field is the divisor and the second
is the dividend.
4. Regardless of which format is used, the quotient is always placed in the dividend field.
5. The divisor may be a numeric literal, but he dividend must always be a numeric data field.

CLAUSES
The GIVING clause

All four arithmetic statements (ADD, SUBTRACT, MULTIPLY and SUBTRACT) can be written
in another form. This form uses the GIVING clause. When the GIVING clause is used in any of
these statements, the result of the calculation is stored in a data field specified by the GIVING
clause. This data field can be either one of the data fields that are part of the calculation or a
new data field. The GIVING clause is added toward the end of these statements. Since the
GIVING clause can be used and not interfere with the contents of the data fields that take part in
an arithmetic operation.

The GIVING clause for the ADD statement:

ADD datafield-1 datafield-2 ... datafield-n GIVING datafield-x.

In this format, the contents of datafield-1 and datafield-2 are added and the result
is stored in a different data field called datafield-x.

Example:

ADD QUIZ1 QUIZ2 GIVING TOTAL-QUIZ.

In this example, the contents of QUIZ1 and QUIZ2 values did not changed, only the
content of the data field TOTAL-QUIZ was changed.

The GIVING clause for the SUBTRACT statement:

SUBTRACT datafield-1 FROM datafield-2 GIVING datafield-3.

This statement subtracts the contents of datafield-1 from the contents of


datafield-2 and stores the result in datafield-3. More than two data fields may participate
in the calculation.

Examples:

SUBTRACT 10 FROM COUNTER-1 GIVING COUNTER-2.


SUBTRACT DEDUCTION FROM GROSS-SAL GIVING NET-SAL.
SUBTRACT W-TAX, SAL-LOAN FROM GROSS-SAL GIVING NET-SAL.

The GIVING clause for the MULTIPLY statement

The MULTIPLY statement with the GIVING option has the following format:

MULTIPLY datafield-1 BY datafield-2 GIVING datafield-3.

Examples:

MULTIPLY QTY BY PRICE GIVING SALES.


MULTIPLY SALARY BY 0.20 GIVING BONUS.
The GIVING clause for the DIVIDE statement

The format of the DIVIDE statement with the GIVING clause is

DIVIDE datafield-1 BY datafield-2 GIVING datafield-3.


OR
DIVIDE datafield-1 INTO datafield-2 GIVING datafield-3.

Both of these DIVIDE statements yield the same result.

Examples:

DIVIDE TOTAL-GRADE BY UNITS GIVING AVE-GRADE.


DIVIDE ANNUAL-SALES INTO 12 GIVING AVE-SALES.

The ROUNDED clause

Problems with insufficient size of the data field to hold the digits to the right of
the decimal points can be handled by using the ROUNDED clause. This clause rounds the
digits on the right of the decimal point to the number of decimal places that the receiving
data field can hold. Truncation of data is more common with multiplication and division
operations than with addition and subtraction.

The REMAINDER clause

The REMAINDER clause is used in COBOL to store the remainder of a division


operation. The REMAINDER clause is used with the DIVIDE statement for this purpose.
When using the REMAINDER clause, the DIVIDE statement must use the GIVING clause. The
format of the REMAINDER clause is

DIVIDE datafield-1 BY datafield-2


GIVING datafield-3
REMAINDER datafield-4.

The contents of datafield-1 are divided by the contents of datafield-2, the integer
part of the division operations is stored in datafield-3, and the remainder is stored in
datafield-4.

The ON SIZE ERROR clause

When a data field is not large enough to hold the digits to the left of the decimal
point or digits without any decimal point, the ONSIZE ERROR clause is used. If this clause
is not used in a situation where an insufficient size problem occurs, digits are truncated
from the left, which greatly affects the value stored. The use of the ON SIZE ERROR
option provides a warning signal and does not store the truncated value in the receiving
data field.
The format of the ON SIZE ERROR is

ON SIZE ERROR imperative-statement.

Example:

MULTIPLY SALES BY RATE GIVING COMMISSION


ON SIZE ERROR MOVE 1 TO ERROR-SW.

COMPUTE statement

The COMPUTE statement is used to determine the value of arithmetic expressions.


It has the following format:

COMPUTE data-field = arithmetic expression.

The word COMPUTE is followed by a blank space, a data field where the result of
the calculation is to be stored, another blank space, an equal sign (=), another blank
space, and then the expression. All arithmetic signs, such as the equal sign, plus sign, and
minus sign, must be preceded and followed by at least one blank space. The COMPUTE
statement makes it possible to perform several arithmetic operations in a single
statement.

Examples:

COMPUTE TOTAL-GRADE = GRD1 + GRD2.


COMPUTE DISCOUNT = PRICE * DISC-RATE.
COMPUTE NET-SALES = GROSS-SALES – EXPENSES.
COMPUTE AVE-GRADE = TOTAL-GRADE / TOTAL UNITS.
COMPUTE SALARY = (HOURLY-RATE * 40) – (NO-OF-HRS - 4) * OT-RATE.

RULES FOR ORDER OF ARITHMETIC OPERATIONS

Arithmetic operations are performed in the following order:

1. Anything in parentheses is calculated first. If parentheses are nested, they are


evaluated from the inside out.
2. Exponentiation.
3. Multiplication / Division.
4. Addition / Subtraction.

Note: Operations of equal priority are evaluated left to right.

Course Materials:
https://www.tutorialbrain.com/mainframe/cobol_divisions/
https://devops.com/the-beauty-of-the-cobol-programming-language-v
https://www.infoworld.com/article/3539057/what-is-cobol-cobol-programming-explained.html
http://www.3480-3590-data-conversion.com/article-reading-cobol-layouts-1.html

You might also like