Download as pdf or txt
Download as pdf or txt
You are on page 1of 81

PROGRAMMING 3

(STRUCTURED
PROGRAMMING)

Compiled by:

Elias A. Austria
Aleta C. Fabregas
Rachel A. Nayre
1
PROGRAMMING 3 (STRUCTURED PROGRAMMING)
INSTRUCTIONAL MATERIAL

General Overview

This Instructional Material gives a basic knowledge on structured programming which focuses
specifically on COBOL Programming language. COBOL stands for Common Business Oriented
Language. It is a high-level programming language design specifically for business applications.
COBOL is primarily used in business, finance, and administrative systems for companies and
governments.

General Objectives

After completion of this Instructional Material student should be able to:

1. Understand concept or logic behind the COBOL program


2. Visualize what COBOL program is supposed to do
3. Fix logic and syntax errors in COBOL
4. Develop a simple application using COBOL Programming language
5. Appreciate of what programmers and computers can do.

“Everybody in this country should learn how to program a computer… because it teaches you
how to think” – Steve Jobs

General Course Materials

Watch:
You can watch these Youtube link for more information about COBOL Programming Lanuage:
https://www.youtube.com/watch?v=ZOLB4KqHmBs
https://www.youtube.com/watch?v=TBs7HXI76yU
https://www.youtube.com/watch?v=AT0Qtl2Ub2c

Read:

For your course guide, read the Programming 3 Course Syllabus and the Instructional Materials.

Activities/ Assessments:

Make sure that you read and understand the each module to answer the assessment activity at
the end of each module.

2
Table of Contents

Module 1 Objectives 4
Flowchart 5
Types of Flowchart 5
Flowchart Symbols and their Usage 5–9
Examples for Algorithm Flowcharts 9
Rules of Drawing flowcharts for Algorithms 10
Drawing a Structured flowchart 11 -13
Flowchart Module Standards 14
Course Materials 15
Activities and Assessments 15 – 16
Module 2 Objectives 17
Is COBOL Dying 18
Benefits of COBOL 18
Where, How and Why we use COBOL 18
Introduction of COBOL 18 - 19
COBOL History 19
History of COBOL Standards 19
Legacy 19
COBOL 2002 and object-oriented COBOL 20
COBOL Advantages 20
Characteristics that contribute to COBOL’s success 20 - 21
Features of the COBOL Program 22
Structure of COBOL Program 22
COBOL Coding Rules 22
Areas in COBOL Program 23 – 24
Record’s Layout 25
FILLER and PICTURE clause 25 – 27

3
Special Formatting Characters 27

Columns, Line Numbers, and Comments 28

Levels and Groups 29

COBOL's 66 and 88 Levels 29

Course Materials 30

Activities and Assessments 31

Module 3 Objectives 32

COBOL Character set 33

Data Types in COBOL 34

COBOL Variables 34

Literals and Figurative Constants 35

Divisions of COBOL Programs 36

RULES FOR INTERPRETING INSTRUCTION FORMATS 37


IDENTIFICATION OF DIVISION 37
ENVIRONMENT DIVISION 38 – 41
DATA DIVISION 41 -50
PROCEDURE DIVISION 50 – 74
Course Materials 75
Assessment and Activities 75

4
Module 1

Overview:
A flowchart is a formalized graphic representation of a logic sequence, work or
manufacturing process, organization chart, or similar formalized structure. The purpose of a flow
chart is to provide people with a common language or reference point when dealing with a project
or process.

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

 Discuss the basic and advanced techniques of constructing process, data and
business process modeling flowchart

 Know and understand the basic concepts of COBOL programming

 Learn the modularized notation of the flowchart

5
A Flowchart, is a graphical representation of algorithm, workflow or process, is incredibly
versatile. It helps simplify a process to allow easy troubleshooting.

The three most popular flowchart types are:

1. Process flowchart (PFD) is a diagram that illustrates and analyzes the overall flow of
activities in producing a product or service, which is always used in process engineering
and chemical industry where the depiction of the relationship between major components
needed. This type of flowchart can be used for:

 Oil and petroleum refining


 Natural gas systems
 Green energy, such as wind and solar power
 Water treatment and processes
 Piping and irrigation systems
 Electrical power plants

2. Data Flowchart (DFD), one of the main tools for structural analysis, shows the way data
is processed. We can use the data flowchart for:

 Data management
 Modeling process aspects
 An overview of the system
 Visualization of data processing
 Where the data will come from and go to

3. Business Process Modeling Diagram (BPMN), a graphical representation for


specifying business processes in a workflow, is used for creating graphical models of
business process operations. This type of flowchart can assist us in:

 Analytical representation of business processes


 Illustrating business processes of an organization
 Process improvement
 Simplifying understanding of business activities flows and processes

Flowchart Symbols and Their Usage

Name
Flowchart Symbol
Usage

Process

represents a step in your process.

6
Predefined process

indicates a set of steps that combine to create a sub-process that is defined elsewhere, often on
another page of the same drawing.

Decision

indicates a point where the outcome of a decision dictates the next step. There can be multiple
outcomes, but often there are just two - yes and no.

Start points

indicates the starting of a process.

Terminal points

indicates the ending points of a process.

Data shape

indicates that information is coming into the process from outside, or leaving the process.

7
Delay shape

represents a waiting period where no activity is done. In Process Mapping, delays are often
important as they may result in adding to the cost of the product or simply delaying its
production.

Database shape

Use this shape for a step that results in information being stored.

Step

represents a single step within a process, and usually contains the name of a specific action.

Page symbols

refer to individual web pages, which may or may not contain multiple elements.

File symbols

represent those data elements that exist independently of navigational properties outside of that
page, e.g., audio sounds, movie clips, or a portable document file (PDF).

Decision point

indicates a sequence in the process at which the end user chooses an option, i.e., a "yes-no", or
"true-false" response, and then branches to different parts of the flowchart.

Arrows and connecting lines

diagram the logical progression through the course, subject to the choices made at decision or
action points within the process.

Input/action symbol

8
represents a user response that directs the course flow from that point onwards, i.e., an online
test or questionnaire form.

Conditional selector

is similar to the conditional branch except that the user has the option to choose from a number
of paths that will fulfill the requested conditions, e.g., the results of a search engine request.

Annotations

provide helpful comments or explanations, e.g. denoting the location where an undeveloped
new page/process will fit into the navigational flow structure, or notes for specific team members
for further development.

Flow references and flow areas

are symbols for reusable sequences, such as logging in with a specific user id and password to
enter the course or to initiate an on-line quiz. The flow reference symbol acts as a placeholder
for the flow area sequence in the chart in every situation in which it is repeated. Flow area is
used as a flow area. It documents sections that share similar components/repeated steps within
that flow, and requires the use of the following two symbols: entry and exit points.

Exit point

concludes the subroutines, such as when the proper user id and password are verified, and
documents where the user re-enters the master flowchart.

Entry point

documents the place within the master flowchart where the process deviates into a subroutine.

Reference

is used as a connecting point when the flowchart necessitates using more than one page, or
refers to a complicated subroutine that would be impossible to contain on the main flowchart
page.

On-page reference

indicates that the next or previous step is somewhere else on the flowchart. It is particularly
useful for large flowcharts.

9
Off-page reference

use a set of hyperlinks between two pages of a flowchart or between a sub-process shape and
a separate flowchart page that shows the steps in that sub-process.

Flowchart Shapes

The designers can click this multi-shape to set to any of the following shapes: Data, Document,
Decision, or Process. Any text you type onto the shape, or information you add to its Shape
Data, remains with the shape.

Document

represents a step that results in a document.

Examples for Algorithm Flowcharts

Algorithms and flowcharts are two different ways of presenting the process of solving a
problem. Algorithms consist of a set of steps for solving a particular problem, while in flowcharts,
those steps are usually displayed in shapes and process boxes with arrows. So flowcharts can
be used for presenting algorithms.

Flowcharts are diagrams that visually present the process of solving problems. The steps of
creating algorithms flowcharts are described below.

Rules of Drawing Flowcharts for Algorithms

There are some basic shapes and boxes included in flowcharts that are used in the structure
of explaining steps of algorithms. Knowing how to use them while drawing flowcharts is crucial.
Here are some rules that should be known:

1. All boxes of flowcharts are connected with arrows to show the logical connection
between them,
2. Flowcharts will flow from top to bottom,
3. All flowcharts start with a Start Box and end with a Terminal Box

Examples of Flowcharts for Algorithms

These examples will help you get a better understanding of flowchart techniques.

10
Example 1: Calculate the Interest of a Bank Deposit

Algorithm:

 Step 1: Read amount,


 Step 2: Read years,
 Step 3: Read rate,
 Step 4: Calculate the interest with formula "Interest=Amount*Years*Rate/100
 Step 5: Print interest,

Flowchart:

Example 4: Determine Whether A


Student Passed the Exam or Not:

Algorithm:

 Step 1: Input grades of 4 courses M1, M2, M3 and M4,


 Step 2: Calculate the average grade with formula "Grade=(M1+M2+M3+M4)/4"
 Step 3: If the average grade is less than 60, print "FAIL", else print "PASS".

Flowchart:

Drawing a Structured Flowchart

Structures You can make your flowcharts easier to understand and less subject to errors
by using only a fixed set of structures. These structures include:

11
 Sequence
 Decision
 Loop
 Case

Whether you are flowcharting software programs or business processes, using


only these structures will make it easier to find and correct errors in your charts.
Each structure has a simple flow of control with one input and one output. These
structures can then be nested within each other. Any chart can be drawn using
only these structures. You do not have to use GOTO or draw spaghetti
diagrams just because you are drawing a flowchart. You can draw structured
flowcharts.

Sequence

The flowchart above demonstrates a sequence of steps. The reader would start at the Start shape
and follow the arrows from one rectangle to the other, finishing at the End shape. A sequence is
the simplest flowcharting construction. You do each step in order.

Decision

This structure is called a decision, "If Then.. Else" or a conditional. A question is asked in the
decision shape. Depending on the answer the control follows either of two paths. In the chart

12
above, if the temperature is going to be less than freezing (32 degrees Fahrenheit) the tomatoes
should be covered.

Loop

This structure allows you to repeat a task over and over. The red chart above on the left does the
task and repeats doing the task until the condition is false. It always does the task at least once.
The green chart on the right checks the condition first and continues doing the task while the
condition is true. In the green chart the task may not be done at all. You can also have the
conditions reversed and your loop is still a structured design loop.

The above chart is a "For Loop." In this example the task is performed 10 times as X counts from
0 to 10. Depending on the condition, the task may not be performed at all.

There is also a "For Each" structure that is like the for loop, but has no counter. It will go through
each item of a collection and do the task. You don't have to know the length of the collection or
use a counter. It is essentially saying "do this for every item in the collection".

13
Case

The structure above is called the case structure or selection structure. The
decision works fine if you have only two outputs, but if there are several, then
using multiple decisions makes the chart too busy. Since the case structure can
be constructed using the decision structure, it is superfluous, but useful. The
case structure helps make a flowchart more readable and saves space on the
paper.

Pseudo-code & Flowchart Module Notation

 Pseudo-Code Module Standards


(most of these standards are implied by the example pseudo-code modular
decomposition earlier in these notes.)

module names
Module names are composed of 2 to 4 words. The first word should be a verb; the last
word should be a noun; intermediate words should be adjectives which qualify the noun.
The first letter of the first word should be capitalized and (within pseudo-code) the words
should be joined with underscores.
e.g. Calculate_Net_Pay
reference to a module
When we wish to perform the activities described by a sub-module, the pseudo-code
simply states the sub-module name as if it were a statement or expression by itself.
Typically the sub-module name is "marked" in some way which makes it obvious that it is
a reference to a sub-module; the form used here, is to enclose the sub-module name in
"square brackets" representing a "named" box whose exact contents are "hidden" from
the current view. .
module definition
A pseudo-code module has the same form as a complete program pseudo-coded
algorithm, except that the terminator used for a sub-module is RETURN (instead of END).

14
 Flowchart Module Standards
The following image demonstrates the method for drawing modular flowcharts based on
the top two levels of the same "Employee Pay" problem as previously used in the modular
pseudo-code and the hierarchy chart examples.

Note:

o references to a sub-module are drawn as boxes with vertical double sides.


o a sub-module flowchart definition starts with the sub-module name (in a rounded
box) and ends with a Return (also in a rounded box); in all other respects a sub-
module flowchart definition is the same as a normal program flowchart definition.

Course Materials:

The edrawsoft.com and rff.com have free trial versions and could be downloaded to be used in
the subject for flowchart creation. The following URL will help you to be more fluent in flowcharting

https://www.edrawsoft.com/flowchart/three-types-of-flowchart.html

https://www.edrawsoft.com/flowchart-symbols.html

https://www.edrawsoft.com/algorithm-flowchart-examples.html

https://www.rff.com/structured_flowchart.php

http://elearning.algonquincollege.com/coursemat/pincka/dat2219d/lectures/27-
Modules.htm#ref01b

15
Activities/Assessments:

Create a flowchart for the following problems.

1. Input file: Student file


Student Student Name Course Code Midterm Grade Final Grade
Number
9(10) X(25) 9 99V99 99V99

Requirements.
1. For every record, read student number, student name, course code,
midterm grade and final grade.
2. The course code with the corresponding course name are the
following:
1 = IT
2 = CS
3. Compute the average grade = (midterm grade + final grade)/2
4. For every record print the student number, student name, course
name and average grade.
5. At the end of file, print the total no. of students, total no.of IT and
total no. of CS.

2.Input file: Employee file


Employee Employee Department Rate per No. of Employee
Number Name Code hour hours status
worked code
9(10) X(25) 9 999V99 99V99 A

Requirements.
1. For every record, read employee number, employee name,
department code, rate per hour, no. of hours worked and employee
status code
2. The department code with the corresponding department name are
the following:
1 = HRD
2 = MANAGEMENT
3= MARKETING
4 = SALES
3. Compute the Gross pay = rate per hour * no. of hours worked
4. The employee code with the corresponding employee status name
are the following: Process only those records with employee status
code = ‘P’.
P = “Permanent”
R = “Probationary”
5. For every record print the employee number, employee name,
department name, gross pay.
6. At the end of file, print the total no. of employees per department.
7.

16
3.Input file: Salesman file
Area Code Salesman number Salesman Name Sales
X 9(10) X(25) 9(7)V99

Output Layout:

SAN MIGUEL CORPORATION

STA. MESA, MANILA

2 spaces

Area Saleman Salesman Sales


Code Number Name Amount
X 9(10) X(25) 99,999.99

Total no. of Salesmen per Area: 999

Total Accumulated Sales: Php 99,999,999.99

X 9(10) X(25) 99,999.99

Grand Total No. of Salesmen: 9,999

17
Module 2
Overview:
This module is a gives the detailed introduction and description of the COBOL
program. COBOL, a business oriented programming language which is very old but proven
very useful and helpful because of its unique structures. This module explains the uniqueness
of the one and only business programming language, very long program but easy to understand.

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

 Know the importance and history of the COBOL program

 Learn and understand the basic structures of the COBOL programming

18
19
Where, How and Why we use COBOL
 In Mainframe COBOL is one of the most important programming languages
 COBOL is a programming language and we use it in multiple industries like Retail, Healthcare,
Insurance, Banking, Share Market, Finance and much more.
 And it is like the English language so it is self-documenting
 We can use 30years older COBOL code till now and run without any changes in modern
Mainframe server.
 COBOL is a procedural language and works well in legacy applications.
 Lots of COBOL programmers are retired and this is a great opportunity for a fresher to learn
COBOL to land a dream job.
 Most of the fortune 500 companies use Mainframe and COBOL is the most preferred
programming language is always a ‘MUST TO HAVE’ skills for a Mainframe Developer.

Introduction of COBOL
 COBOL stands for Common Business Oriented Language.
 It is started in 1959 by CODASYL committee (Conference on Data Systems Language).
 COBOL programs appear like an ENGLISH language.
 COBOL can handle a huge volume of data.
 ANSI (American National Standards Institute) worked further on improving this and major
development took place in improvement in the year 1965 and 1974.
 The improved version of COBOL was introduced in 1985, with new features.
 In Mainframe COBOL is one of the most important programming languages
 COBOL is a programming language and we use it in multiple industries like Retail, Healthcare,
Insurance, Banking, Share Market, Finance and much more.
 And it is like the English language so it is self-documenting
 We can use 30years older COBOL code till now and run without any changes in modern
Mainframe server.
 COBOL is a procedural language and works well in legacy applications.
 Lots of COBOL programmers are retired and this is a great opportunity for a fresher to learn
COBOL to land a dream job.
 Most of the fortune 500 companies use Mainframe and COBOL is the most preferred
programming language is always a ‘MUST TO HAVE’ skills for a Mainframe Developer.

COBOL history
COBOL arose in the late 1950s and early 1960s. The development of the language was a project
sponsored by the United States Department of Defense (DoD) that included a consortium of
computer companies including IBM, Honeywell, Sperry Rand, and Burroughs. The goal was to
create a programming language with the following attributes:

 Portability between computer systems, thus making it easier to migrate software both across
generations of hardware and between hardware makers.
 More English-like syntax than other languages of the time (e.g., FORTRAN) as a way to
encourage programming by a wider audience, even if at the expense of some operational speed.
 The ability to accommodate future changes to the language.

The first official COBOL specifications came out in 1960. Over the next decade, and to the
consternation of its critics, COBOL became the default choice for writing business applications.
One reason for its fast spread was network effects: IBM, one of the original collaborators on the
language, became an aggressive early adopter, and IBM’s dominating presence in the computing
world helped contribute to COBOL adoption.

20
History of COBOL standards
The specifications approved by the full Short Range Committee were approved by the Executive
Committee on January 3, 1960, and sent to the government printing office, which edited and
printed these specifications as COBOL 60.
The American National Standards Institute (ANSI) has since produced several revisions of the
COBOL standard, including:

 COBOL-68
 COBOL-74
 COBOL-85
 COBOL 2002

From 2002, the ISO standard is also available to the public coded as ISO/IEC 1989.

21
Legacy
COBOL programs are in use global in governmental and military agencies, in commercial
enterprises, and on operating systems such as IBM’s z/OS, Microsoft’s Windows, and the POSIX
families (Unix/Linux etc.). In 1997, the Gartner Group reported that 80% of the world’s business
ran on COBOL it over 200 billion lines of code in existence and with an estimated 5 billion lines
of new code annually.
Near the end of the twentieth century the year 2000 problem was the focus of significant systems
decades before. The particular level of effort required for COBOL code has been attributed both
to the large amount of business-oriented COBOL, as COBOL is by of the COBOL language such
as the PICTURE clause, which can be used to define fixed-length numeric fields, including two-
digit fields for years.
COBOL 2002 and object-oriented COBOL
The COBOL2002 standard supports Unicode, XML generation and parsing, calling conventions
to and from non-COBOL languages such as C, and support for execution within framework
environments such as Microsoft’s .NET and Java (including COBOL instantiated as Enterprise
JavaBeans). Fujitsu and Micro Focus currently supports object- oriented COBOL compilers
targeting the .NET framework.
COBOL Advantages

 It is machine independent. A COBOL program can be executed in different types of


computer with slight or no modifications at all.
 It is easy to maintain. There are few modifications of the COBOL-74, it was revised only
in 1985 through COBOL-85, the latest began its modification only in late 1990s.
Therefore, a program that is easy to maintain can save a company considerable time and
money.
 It is easy to understand. The programming language instructions or code contain
English-like words and phrases.
 COBOL programs are uniquely organized. Only four major parts are needed to
understand. These parts are called divisions.
 A COBOL program can handle voluminous commercial data processing using only
a few statements. COBOL is well suited for handling and processing large quantities of
data by writing several program statements using its input-output operations. This is
unique in comparison with other language.
 COBOL as defined in the original specification included a PICTURE clause for
detailed field specification. It did not support local variables recursion, dynamic memory
allocation, or structured programming constructs. Support for some or all of these features
have been added in later editions of the COBOL standard.
 COBOL has many reserved words (over 400), called keywords. The original COBOL
specification supported self-modifying code via the infamous “ALTER X TO PRECEED
TO Y “statement. This capability has since been removed.

Characteristics that contribute to COBOL’s success

 COBOL is self-documenting

22
One of the design goals for COBOL was to make it possible for non-programmers such
as supervisors, managers and users, to read and understand COBOL code. As a
result, COBOL contains such English-like structural elements as verbs, clauses,
sentences, sections and divisions. As it happens, this design goal was not realized.
Managers and users nowadays do not read COBOL programs. Computer programs
are just too complex for most laymen to understand them, however familiar the
syntactic elements. But the design goal and its effect on COBOL syntax has had one
important side-effect. It has made COBOL the most readable, understandable and
self-documenting programming language in use today. It has also made it the most
verbose.
It is easy for programmers unused to the business programming paradigm, where
programming with a view to ease of maintenance id very important, to dismiss the
advantage tat COBOL’s readability imparts. Not only does this readability generally
assist the maintenance process but the older a program gets the more valuable this
readability becomes.
When programmers are new, both the in-program comments and the external
documentation accurately reflect the program code. But over time as more and more
revisions are applied to the code, it gets out of step with the documentation until the
documentation is actually a hindrance to maintenance rather than a help. The self-
documenting nature of COBOL means that this problem is not as severe with COBOL
programs as it is with other languages.

 COBOL is simple
COBOL is a simple language (no pointers, no user defined functions, no user defined
types) with a limited scope of function. It encourages a simple straightforward
programming style. Curiously enough though, despite its limitations, COBOL has
proven itself to be well suited to its targeted problem domain (business computing).
Most COBOL programs operate in a domain where the program complexity lies in the
business rules that have to be encoded rather than in the sophistication of the data
structures or algorithms required. And in cases where sophisticated algorithms are
required COBOL usually meets the need with an appropriate verb such as the SORT
and the SEARCH.
We noted above that COBOL is a simple language with a limited scope of function.
And that is the way it used to be but the introduction of OO-COBOL has changed all
that. OO-COBOL retains all the advantages of previous versions but now includes-
 User Defined Functions
 Object Orientation
 National Characters – Unicode
 Multiple Currency Symbols
 Cultural Adaptability (Locales)
 Dynamic Memory Allocation (pointers)
 Data Validation Using New VALIDATE Verb
 Binary and Floating Point Data Types
 User Defined Data Types

23
 COBOL is non-proprietary (portable)
The COBOL standard does not belong to any particular vendor. The vendor
independent ANSI COBOL committee legislates formal, non-vendor-specific syntax
and semantic language standards. COBOL has been ported to virtually every
hardware platform – from every favor of Windows, to every falser of UNIX, to AS/400,
VSE, OS/2, DOS, VMS, Unisys, DG, VM, and MVS.

 COBOL is Maintainable
COBOL has a 30 year proven track record for application maintenance, enhancement
and production support at the enterprise level. Early indications form the year 2000
problem are that COBOL applications were actually cheaper to fix than applications
written in more recent languages.[Capers Jones] [Kappleman] One reason for the
maintainability of COBOL programs has been given above – the readability of COBOL
code. Another reason is COBOL’s rigid hierarchical structure. In COBOL programs
all external references, such as to devices, files, command sequences, collating
sequences, the currency symbol and the decimal point symbol, are defined in the
Environment Division.
FEATURES OF A COBOL PROGRAM
A. English-like syntax
B. Unusual length compared with programs written in other languages.
C. Column-sensitive organization of the program.
D. Spacing between any two-program segments.
E. Use of indentations.

Structure of COBOL Program


You must write the COBOL programs in an organized way. It must follow a modular programming
approach which is a subset of procedural programming approach.
The COBOL program consists of a logical structure and this is called structure COBOL
programming.
Please follow these important points while designing your COBOL program –

 A COBOL Program is divided into DIVISION. Each DIVISION in COBOL Programs is further
divides into SECTION.
 A SECTION is further divided into PARAGRAPH.
 PARAGRAPH is divided into multiple SENTENCE.
 A SENTENCE contains STATEMENT.
 A STATEMENT is subdivided into CLAUSE or VERB.
 At the lowest Level, we have CHARACTER or WORD.

COBOL Coding Rules


COBOL coding rules are very important and you should follow a particular COBOL format
because of COBOL of structured programming language.

24
This picture shows the major areas in a COBOL Program.

Areas in COBOL Program

There are 5 areas in COBOL Program –


1. Sequence Number Area
2. Indicator Area
3. Area A
4. And Area B
5. Program Identification Area

1. Sequence Number Area


 Column 1 to Column 6 is the Sequence Number area.
 The Sequence number area stores the Sequence numbers.
 And its number is for labeling a source statement line.
In the above picture of a COBOL program, the numbers in yellow area (column 1-6) are
Sequence numbers.

Question: Can you find the sequence numbers in the below?


|----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
000001 01 SEQUENCE-NUMBER PIC 9(03.
000002 01 WS-TEMP-VAR PIX X(3).
Answer
000001
000002

2. Indicator Area
 Column 7 is the Indicator Area in COBOL.
 You can start Comments, Continuation or Debugging mode(D) in this Indicator Area.
CommentsTo give a comment a line in COBOL, you can give an 'asterisk' i.e. '*' symbol in
Column 7
The Syntax of Comment:
|----+----1----+----2----+----3----+----4----+----5----+----6----+----7--

25
000001 *THIS IS A COMMENT IN COBOL
Example of Comments in the COBOL program

Continuation
If you are writing a COBOL program and for some reason, you are not able to fit your current
code in a line, then you can continue it from the next line by providing a hyphen i.e. '-' symbol in
the 7th column.
Note: We will discuss Continuation in detail as we will move further.

26
The Syntax of Continuation:
|----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
000001 -

3. Area A
 Area A Starts from Column 8 to Column 11.
 All Division, Section, Paragraph, Start Declarative, End Declarative, 01 Level number must
start in Area A.

Example of Area A

In this sample screenshot, you can see that DIVISION, SECTION and 01 LEVEL starts from
Area A.

4. Area B
 Area B Starts from Column 12 to Column 72.
 All Statement, Sentence, Verbs, Clause, Characters, and Words must start in Area B.
The Sentence is divided into various types of Statements and there can be one or more
statements in a sentence.
Let us take all the lines one by one –
Line1 – MOVE “HELLO WORLD” TO WS-HELLO.
This is a Sentence which is also a Statement.

Line2 – MOVE A TO B
This is a Statement.

Line3 – MOVE C TO D
This is also a Statement.

Line4 – COMPUTE E = C + D.
This is also a Statement.

If we combine Line 2, Line 3 and Line 4, then it becomes a Sentence so at the end of a
Sentence, we must have a dot(.).

27
In, MOVE A TO B, MOVE is a verb.
Similarly, COMPUTE E = C + D. -> Here COMPUTE is also a Verb.

Example of Area B

In this sample screenshot, you can see that statement, sentence etc are starting in Area B.

5. Program Identification Area


 Program Identification area in COBOL Program is the area between column 73 to column 80.
 They do not have much relevance in today’s world.
 The idea of using this area was to enter the program identification of each individual lines in
the code.
Example of Program Identification Area

Record Layouts

A record layout is a description of all the individual fields that comprise each record in the
data file. COBOL layouts follow specific rules. Since we are not teaching you how to program in
COBOL, we will only discuss the rules you need to know to read layouts.

The layout specifies at-least the name of each field, its type, size, and position in the record. A
layout may give a detailed description of the use of each field and the values found in it, but that
information is often contained in the data dictionary. A COBOL layout usually pertains to a
single disk or tape file, as opposed to a table within a database.

Fields and the PIC clause

The lowest level data item in a COBOL layout is a field, also called an elementary
item. Several fields can be associated to form a group. All the fields together form a record.

28
A COBOL layout is comprised of a line for each field or group. A COBOL field definition gives
the level (discussed later), field name, and a "picture", or PIC clause, which tells you the data
type or data category of the field, and its size. The three data types you are likely to see are:

1. "A" for alpha (A-Z, a-z, and space only).


2. "9" for a numeric field (numbers 0-9, but no letters).
3. "X" for any character, (including binary).

For example, the following field (elementary item) is called ZIP-CODE and is 5 digits wide, as
specified by the five 9s. i.e., the "picture" of the field is "99999".
05 ZIP-CODE PIC 99999.
This could also be written:
05 ZIP-CODE PIC 9(5).
Where the 9 means the field type is numeric, like the first example, and the (5) says there are
five digits. The 9(5) and the 99999 are identical field specifications. The parentheses are
usually used when it makes the definition shorter or clearer, as in 9(11) vs: 99999999999. The
period at the end separates this field definition from the next one.

A character field such as last name could be written:

05 LAST-NAME PIC A(15).


Meaning it's a 15 character alphabetic field. But it's actually more common to see character
fields specified as PIC X, like:
05 LAST-NAME PIC X(15).
PIC X allows any character, including numbers, punctuation, and binary codes.

Like the numeric example above, a PIC X field specification could be written as either multiple
Xs or a count in parentheses, like these two identical field specifications:

05 LAST-NAME PIC X(15).


05 LAST-NAME PIC XXXXXXXXXXXXXXX.

Although not commonly seen in COBOL files, you can mix types in a field. For example,

05 ZIP-PLUS-9 PIC 99999X9999.


permits a dash (or anything) between ZIP and ZIP+4, like 01886-2001.

A decimal point in a PIC, like "PIC 999.99" separates the integer portion from the decimal
portion. This is discussed in more detail later, along with implied decimal.

Let's practice one more, just to get the point across. The following are different ways of
specifying the same thing:

05 AMOUNT PIC 999.99.


05 AMOUNT PIC 9(3).9(2).
05 AMOUNT PIC 9(3).99.
05 AMOUNT PIC 999.9(2).

Filler

29
There is a special type of COBOL field called FILLER. This reserves space in a COBOL record,
commonly for future expansion or to fill a gap created by a redefined field. FILLER is a reserved
word, and you can have as many FILLER fields in a record as you want -- the name does not
have to be unique as field names generally must be.

Filler can also be used to create a field, or place holder, that you will never need to refer to by
name, so you might find it contains actual data, not just blank space.

It's also common for a vendor to use fields for some internal purpose, for example as a key field,
but to mark those fields as FILLER when the data is sent outside the company. So FILLER fields
can contain anything, including binary data. You should not expect them to be neatly filled with
spaces.

Special Formatting Characters

There are a number of special characters that cause specific actions on the data, such as leading
zeros or spaces, floating signs, leading or trailing signs, decimal points, etc. We will mention only
a few common ones:

1. A literal in a field causes that character to appear in that location. For example,

05 ZIP-PLUS-9 PIC 99999-9999.


specifies a field with five digits, a dash, and four more digits. The dash is not part of the variable
data -- it is a literal character.

2. A decimal point in a numeric field does two things: it places an actual decimal point into the file,
and determines the location of the decimal for calculations. The following field is six bytes wide
and has a "real decimal" in the file:

05 AMOUNT PIC 999.99.


If you view a record containing the value 123.45 in this field, you will see "123.45"

3. A "V" in the PIC clause specifies the location of an implied decimal. This is discussed later,
in the section on numeric fields. The following field is five bytes wide and has an "implied decimal"
at the location of the V:

05 AMOUNT PIC 999V99.


If you view a record containing the value 123.45 in this field, you will see "12345" .

4. A minus sign, "-", reserves a byte in the record for an actual sign, and puts a "-" in negative
values, and a space in positive values.

5. Similarly, a "+" in the PIC puts a "-" in negative values and a "+" in positive values. See the
section below on "signed fields" for the representation of PIC S9 fields.

6. A "P" in a PIC clause scales the value. This is seldom seen, so we will be brief, via two
examples:

30
The three 9s cause this field to be three bytes in size, and the three Ps scale it
PIC
UP by 1000. If the field contains the digits 123, the actual value represented is
999PPP.
123,000.

PIC This scales the value DOWN. If the field contains the digits 123 the actual value
PPP999. is 0.000123

Columns, Line Numbers, and Comments

Columns 1-6 in most COBOL layouts are ignored by the compiler, as is everything after column
72. You will often find line numbers or other comments (such as when a field was added or
changed, or where it originated) in these columns. These may be useful to you in finding your
way around a large layout; just be aware they are ignored by the compiler.

COBOL layouts are divided into "areas", and there are many rules for what data may be found in
which area, but one you should remember is that an asterisk, *, in column 7, the "indicator area"
turns the entire line into a comment, which is ignored by the COBOL compiler. Even if that line
contains a field specification, it will be ignored if there is an * in column 7.

There are variations on COBOL layouts that discard columns 1-6, shifting the entire layout
left. And, some printed documentation may not show these columns. You can usually find your
place in the layout from the 01 level, which normally starts in column 8. All other levels should
start in column 12 or above.

A COBOL field definition does not need to be entirely on one line. A line-ending has no
significance to the compiler; it's the period at the end that's the COBOL separator, not the
carriage-return.

Levels and Groups

COBOL layouts have levels, from level 01 to level 49. These levels tell the COBOL compiler how
to associate, or group, fields in the record. Level 01 is a special case, and is reserved for the
record level; the 01 level is the name of the record. Levels from 02 to 49 are all "equal" (level 2
is no more significant than level 3), but there is a hierarchy to the structure. Any field listed in a
lower level (higher number) is subordinate to a field or group in a higher level (lower number). For
example, LAST-NAME and FIRST-NAME in the example below are part of, or belong to, the group
CUSTOMER-NAME, as can be seen by the level numbers of 05 and 10.
05 CUSTOMER-NAME.
10 LAST-NAME PIC X(15).
10 FIRST-NAME PIC X(8).

Notice that CUSTOMER-NAME does not have a PIC, since it's a group, not a field. Also notice
that the two fields within the group are at a lower level, level 10, than the 05 group. Lower levels
are normally indented further for clarity, but this is not required, and in fact the compiler doesn't
care.

For the rest of this tutorial we will use levels 05, 10, and 15 to be consistent. Just remember these
choices are arbitrary; we could have used 02, 03, and 04, or any other numbers between 02 and
49.

31
There can be many levels. Here is a brief example of a record with three levels:

01 MAILING-RECORD.
05 COMPANY-NAME PIC X(30).
05 CONTACTS.
10 PRESIDENT.
15 LAST-NAME PIC X(15).
15 FIRST-NAME PIC X(8).
10 VP-MARKETING.
15 LAST-NAME PIC X(15).
15 FIRST-NAME PIC X(8).
10 ALTERNATE-CONTACT.
15 TITLE PIC X(10).
15 LAST-NAME PIC X(15).
15 FIRST-NAME PIC X(8).
05 ADDRESS PIC X(15).
05 CITY PIC X(15).
05 STATE PIC XX.
05 ZIP PIC 9(5).
Most of the fields in this record (company, address, city, state, zip) are simple fields that need no
comment. But there are some interesting things about the contact fields:

There is a group called CONTACTS at the 05 level. Within this group are three 10 level
groups. The first one is PRESIDENT, and within this group are the LAST-NAME and FIRST-
NAME fields for the president. So far this is similar to the previous example, with one more
level. This group is 23 bytes (15 + 8).

Next we have a group to contain the name of the VP of Marketing. This group is also 23
bytes. Notice it uses the same field names, LAST-NAME and FIRST-NAME, as used in the
president's group. Although this isn't commonly seen, it is permitted in COBOL. They are
considered different fields because they are within different groups. In COBOL you distinguish
them by referring to "LAST-NAME OF PRESIDENT" for the president's name, and "LAST-NAME
OF VP-MARKETING" for the name of the VP of Marketing.

The last group in the CONTACTS group is for "alternate contacts". This one contains a field
called TITLE which contains the title of the alternate contact (e.g., CEO). Like the others, it
contains LAST-NAME and FIRST-NAME fields. This group is 33 bytes.

COBOL's 66 and 88 Levels

These two levels have special meaning. The 66 level assigns an alternate name to a field or
group. It doesn't add a new field to the record, it just assigns an alternate name to an existing
field. You are not likely to see level 66.

You are likely to see the 88 level, though. The 88 level simply equates a value with a
name. Here's a simple example:

05 SEX PIC X.
88 MALE VALUE "M".

32
88 FEMALE VALUE "F".
This equates the value "M" with "MALE", and the value "F" with "FEMALE" for the field SEX. (This
allows your COBOL program to, for example, test IF MALE rather than having to say IF SEX IS
EQUAL TO "M"). Since we are not teaching COBOL programming, this is incidental to us, but
here's what is important to know about the 88 level:

1. The 88 level does not define a field, and does not take space in the record; it is merely a
value definition.
2. The 88 level does not limit the possible codes to only those listed. There could be other
values used in that field; M and F are not the only values you might find. (Although a
good layout will list them all.) In this case there might be a "U" (unknown), or a blank.
3. If the layout is complete, this is a handy list of the values you can expect to find in this
field. Sometimes it's all you have to go on.

88 levels may specify multiple values, or a range of values, such as:


88 ODD-NUMBERS VALUE 1, 3, 5, 7, 9.
88 PRE-SCHOOL VALUE 0 THROUGH 4.

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

Activities/Assessments:

A. Apply and convert each layout of the input files into COBOL entries using the standards
convention of Characters, Fillers and level numbers of the program.

1. Input file: Student file


Student Student Name Course Code Midterm Grade Final Grade
Number
9(10) X(25) 9 99V99 99V99

Requirements.
1. For every record, read student number, student name, course code,
midterm grade and final grade.
2. The course code with the corresponding course name are the
following:
1 = IT
2 = CS
3. Compute the average grade = (midterm grade + final grade)/2

33
4. For every record print the student number, student name, course
name and average grade.
5. At the end of file, print the total no. of students, total no.of IT and
total no. of CS.

2. Input file: Employee file


Employee Employee Department Rate per No. of Employee
Number Name Code hour hours status
worked code
9(10) X(25) 9 999V99 99V99 A

Requirements.
1. For every record, read employee number, employee name, department
code, rate per hour, no. of hours worked and employee status code
2. The department code with the corresponding department name are the
following:
1 = HRD
2 = MANAGEMENT
3= MARKETING
4 = SALES
3. Compute the Gross pay = rate per hour * no. of hours worked
4. The employee code with the corresponding employee status name are the
following: Process only those records with employee status code = ‘P’.
P = “Permanent”
R = “Probationary”
5. For every record print the employee number, employee name, department
name, gross pay.
6. At the end of file, print the total no. of employees per department.

34
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

35
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

36
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 can use Numeric data types with other data types
(picture clause) as well.

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.

37
 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.

38
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

39
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

40
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}….]

41
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

42
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”

43
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.
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.

44
DATA DIVISION.
FILE SECTION.
FD filename
[LABEL RECORD IS STANDARD]
[LABEL RECORDS ARE OMMITED]
[RECORD CONTAINS ______ CHARACTERS]
[DATA RECORD IS _________________]
01 RECORD NAME.

I. 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.

45
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:

46
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.

I. 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.

47
 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.
VI. 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.

48
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

49
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.

50
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.

51
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

52
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

53
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

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

54
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

55
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.

56
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 TO 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.

57
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

58
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.

59
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.

60
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:

61
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.

62
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.

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:

63
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 FORM 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.

64
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.

65
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.

CONDITION STATEMENTS

CONDITION STATEMENTS are special group of COBOL statements that use selection
structures to check for specified conditions that control whether a statement or group of
statements will be executed.

The IF Statement

The IF statement is a kind of condition statement that compares two values. If the result
of the comparison is true, a specified statement or set of statements is executed. The format of
the IF statement is

IF condition
Statement(s).
Example:

IF GRADE > 74.00


MOVE “PASSED” TO REMARKS.

66
Format of IF-ELSE statement is

IF condition
Statement(s)
ELSE
NEXT SENTENCE

Statement(s)

Example:

IF TRANSCODE = “D”
ADD AMOUNT TO BALANCE
ELSE
SUBTRACT AMOUNT FROM BALANCE.

RELATION CONDITIONS

The IF statement is an example of a relation condition. In relation conditions, two


operands are compared. The IF statement determines how these operands are “related to” one
another. The outcome of the comparison will always be either true or false.

The relational operators are:

1. IS EQUAL TO (=)
2. IS NOT EQUAL TO (NOT =)
3. IS LESS THAN (<)
4. IS NOT LESS THAN (NOT <)
5. IS GREATER THAN (>)
6. IS NOT GREATER THAN (NOT >)
7. IS GREATER THAN OR EQUAL TO (> =)
8. IS LESS THAN OR EQUAL TO (< =)
The general format of the relation conditions is

EQUAL TO
NOT EQUAL TO
LESS THAN
IF datafield-1 NOT LESS THAN datafield-2
GREATER THAN
NOT GREATER THAN
GREATER THAN OR EQUAL TO
LESS THAN OR EQUAL TO

It is possible to combine arithmetic operations with relation condition. If a statement contains


both arithmetic operations and relation conditions, the arithmetic operations are always performed
first.

CLASS CONDITIONS

COBOL can test the class type of a data field to determine whether the contents are
numeric or non numeric. The condition is called CLASS condition.

67
The general format is

NUMERIC
IF datafield-1 NOT NUMERIC
ALPHABETIC
NOT ALPHABETIC

The numeric test is valuable because it can be used to test the contents of a numeric data
field before using its contents in arithmetic calculations. When numeric data fields containing non
numeric data values are used in arithmetic calculations, errors are generated. The numeric test
prior to using a data field in calculations helps avoid such errors.

SIGN CONDITIONS

The sign of the contents of a numeric data field can be tested using sign condition
statements. A sign condition test can determine whether the contents of a data field are less than
zero, greater than zero, equal to zero, or not equal to zero. This statement has the following
format.

NEGATIVE
NOT NEGATIVE
IF datafield-1 POSITIVE
NOT POSITIVE
ZERO
NOT ZERO

Note that NOT POSITIVE is the same as NEGATIVE, and NOT NEGATIVE is the same
as POSITIVE. Similarly, NEGATIVE is the same as LESS THAN ZERO, and POSITIVE,
is the same as EQUAL TO or MORE THAN ZERO.

CONDITION NAMES

In COBOL, conditions can be defined and names assigned. This is done through the
WORKONG-STORAGE SECTION of the DATA DIVISION, using level 88. Defined conditions
can be referred to by name in the PROCEDURE DIVISION.

Example:

05 END-OF-FILE PIC X.

END-OF-FILE is a condition data field in the WORKING-STORAGE SECTION. This data


field can be given condition names with level number 88, and the condition names can be
associated with particular values of END-OF-FILE.

05 END-OD-FILE PIC X VALUE “N”.


88 NO-MORE-DATA VALUE “Y”.
88 MORE-DATA VALUE “N”.

68
These level 88 entries are the condition names. The first condition name, NO-MORE-
DATA, is true if END-OF-FILE is equal to “Y”, the second condition name, MORE-DATA, is true
if END-OF-FILE is equal to “N”. Once defined, both condition names can be used in the
PROCEDURE DIVISION of a program.

PERFORM PROCESS-DATA
UNTIL NO-MORE-DATA.

LOGICAL OPERATORS

Compound condition statements evaluate more than one condition. These conditions are
joined using logical operators. LOGICAL operators are operators used with logical conditions, or
conditions that can always be evaluated as true or false.

The format of a compound condition statement is

AND
IF condition-1 OR condition-2
NOT

Examples:

IF AGE > 50 OR AGE < 18


PERFORM ERROR-RTN.

IF COLLEGE-CODE = “CCMIT”
AND YR-LEVEL = 3
AND SEX-CODE = “M”
ADD 1 TO COUNTER.

IF NOT (EMPLOYEE-RATE IS GREATER THAN 125.00)


PERFORM COMPUTE-RTN.

RULES FOR EVALUATING MULTIPLE LOGICAL OPERATORS

The following order of priority enforced in evaluating multiple logical operators in a single COBOL
statement.

1. Logical operators within parentheses (from inner parenthesis to outer parentheses).


2. The logical operator NOT.
3. The logical operator AND.
4. The logical operator OR.

The PERFORM Statement

It is a statement that transfers program control to specific part of the program. This
statement is one of the most powerful and widely used in COBOL statements. On the transferring
control to the specified paragraph, the paragraph executes all the statements in that paragraph,
and then returns to the statement immediately following the PERFORM statement.

Classification of Perform statement

69
1. Unconditional – a statement in which the specified action is taken. The format of the simplest
unconditional PERFORM statement is

a. PERFORM statement
Example:
MAIN-RTN.
PERFORM BSIT-RTN
PERFORM BSCS-RTN
BSIT-RTN.
MOVE STUDENT-NO TO P-STUDENT-NO.
MOVE STUDENT-NA TO P-STUDENT-NA.
BSCS-RTN.
WRITE OUTREC FROM PRT-AREA AFTER 1 LINE.

BSIT-RTN and BSCS-RTN are two paragraph names that will be executed immediately
by the PERFORM statement. The first PERFORM statement transfers control to
paragraph BSOR-RTN in which the two MOVE statements are executed, then the control
returns to the MAIN-RTN to execute the next unconditional PERFORM statement. Control
will transfer to the paragraph BSCS-RTN to execute the WRITE statement and again will
return to the calling routine to perform the next statement.

b. PERFORM paragraph-name integer TIMES.

This statement will execute the paragraph several times depending on the integer
times specified on the statement.
Examples:

BSIT-RTN.
PERFORM CCMIT-RTN 5 TIMES.
CCMIT-RTN.
MOVE ‘COMPUTER’ TO PRT-AREA-COURSE.
WRITE OUTREC FROM DETAIL AFTER 1 LINE.

CCMIT-RTN will be executed five times. This will result to an output displayed below.

COMPUTER
COMPUTER
COMPUTER
COMPUTER
COMPUTER

c. PERFORM paragraph-name-1 THROUGH paragraph-name-2.

This statement transfers control to paragraph-name-1, executes all the statements


in paragraph-name-1 through paragraph-name-2, and then returns to the statement
immediately following the PERFORM statement. If there are two statements in paragraph-
name-1 and three statements in paragraph-name-2, the PERFORM statement will
execute five statements, once each, before returning to the statement immediately
following the PERFORM statement.
Example:

70
CCMIT-RTN.
PERFORM BSIT-RTN THROUGH BSCS-RTN.

BSIT-RTN.
MOVE STUDEN-NO TO PRT-STUDNO.
MOVE STUDEN-NA TO PRT-STUDNA.
MOVE COURSE TO PRT-COUR.
MOVE YEAR TO PRT-YR.
BSCS-RTN.
WRITE OUTREC FROM DETAILS.
ADD 1 TO TOTAL.

This statement will perform the two paragraphs immediately, before returning to
the next statement following the PERFORM statement.

CCMIT-RTN.
PERFORM BSIT-RTN THRU BSIT-RTN-END.
BSIT-RTN.
MOVE STUDEN-NO TO PRT-STUDNO.
MOVE STUDEN-NA TO PRT-STUDNA.
MOVE COURSE TO PRT-COUR.
MOVE YEAR TO PRT-YR.
WRITE OUTREC FROM DETAILS.
ADD 1 TO TOTAL.
BSIT-RTN-END.

This statement will perform all the statements between paragraph-name-1


(BSIT-RTN) and paragraph-name-2 (BSIT-RTN-END).

THRU in some old compilers can be used to replace the reserved word
THROUGH.

2. Conditional – a statement in which the specified action may or may not be taken
depending on the result of the stated condition.
The two characteristics of this statement are,
1. It must make a comparison between two values, the result of which must be true or false.
2. It must contain an action. Whether or not this action is taken is dependent upon the
results of the comparison.

a. PERFORM paragraph-name-1 UNTIL condition.


This statement represents an iteration control structure. It allows a specified paragraph
to be executed as many times as needed. The diagram below graphically illustrates how the
conditional PERFORM statement functions.

71
ENTRY

STATEMENTS

FALSE

TRUE

EXIT
When the PERFORM statement is used with a conditional clause, the condition is
tested before the specified paragraph is executed. If the condition is true, the paragraph
is not executed.

Example:
MAIN-RTN.
PERFORM BSCS-RTN THRU BSCS-RTN-END.
PERFORM BSIT-RTN UNTIL EOFSW=1.
STOP RUN.

BSCS-RTN.
OPEN INPUT INFILE, OUTPUT OUTFILE.
READ INFILE AT END MOVE 1 TO EOFSW GO TO BSCS-
RTN-END.
PERFORM HEADING-RTN.

BSCS-RTN-END.
BSIT-RTN.
READ INFILE AT END MOVE 1 TO EOFSW.
MOVE STUDEN-NO TO PRT-STUDNO.
MOVE STUDEN-NA TO PRT-STUDNA.
WRITE OUTREC FROM DETAILS AFTER 1.
READ INFILE AT END MOVE 1 TO EOFSW.

The two PERFORM statements will be executed immediately as part of the MAIN-
RTN. First the unconditional perform will transfer its control to the BSCS-RTN through
BSCS-RTN-END. Next the second unconditional will be executed. The condition

after the reserved word UNTIL will be tested. If the EOFSW is false, then the specified
paragraph in the PERFORM will be executed. Otherwise if it is true, then the next
statement will be performed, which is STOP RUN. The format below is also a valid
statement.

PERFORM BSIT-RTN THROUGH BSIT-RTN-END UNTIL
EOFSW = 1.
BSIT-RTN.
READ INFILE AT END MOVE 1 TO EOFSW.

72
MOVE STUDEN-NO TO PRT-STUDNO.
MOVE STUDEN-NA TO PRT-STUDNA.
WRITE OUTREC FROM DETAILS AFTER 1.
READ INFILE AT END MOVE 1 TO EOFSW.
BSIT-RTN-END.

b. PERFORM paragraph-name VARYING data-field FROM initial value BY increment


value UNTIL condition(s).

This statement is used in TABLE handling. TABLE is a sequence of data values grouped
together with a common name. Each data item in the table is referred to as an ELEMENT.
The elements in the table are accessed using common table name and a SUBSCRIPT,
which references a specific table element. A subscript is a constant or a data field used
to access a specific element of a table. The syntax used to access the individual element
of a table is

Table-name (subscript)

The table-name is followed by at least one blank space, a left parenthesis, the
subscript, and a right parenthesis.
Tables are defined using OCCURS clause. This clause appears with the data-
name in the FILE SECTION or WORKING-STORAGE SECTION of the DATA DIVISION.
The syntax of the OCCURS clause is

Data-field OCCURS integer TIMES.

The data-field is the name of the table. The integer values define the size, or the number
of elements in the table. The integer indicates the range of the subscripts that can be
used with this table and must be greater than zero.

RULES FOR DEFINING A TABLE AND USING SUBSCRIPTS

1. The integer value in the OCCURS clause indicates the number of elements in the table.
It cannot be zero. The subscript range is from one through a given value.
2. The PIC clause must appear at the elementary data-field level, although the OCCURS
clause can appear either at the elementary or group level.
3. The subscript must follow the table name and be placed in parentheses. There must be
one or more spaces between the table name and the left parenthesis.
4. The subscript must be within the range of the table.
5. The subscript must be and integer. It can be either a constant or a numeric data-field.
6. If a data-field is used as a subscript, the data-field must be defined in the WORKING-
STORAGE SECTION or FILE SECTION as an integer with PIC 9s.

TABLE in COBOL is a one-dimensional array or a multi-dimensional array that holds similar


data items of fixed number of elements. PERFORM statement with VARYING option is a
statement used to execute a specified paragraph repeatedly with different values of a
subscript or index. The execution of this statement proceeds as follows:
1. Move the initial value to data-field.
2. Determine whether the condition is true.
3. If the condition is true, execute the statement following PERFORM statement; if the
condition is false, execute the specified paragraph-name.

73
4. Add the increment-value to data-field.
5. Go to step 2 and continue.

The diagram below illustrates how this PERFORM statement functions.

ENTRY

MOVE INITIAL
VALUE TO
DATA-FIELD

INCREASE
EXECUTE DATA-FIELD
SPECIFIED BY
PARAGRAPH INCREMENT-
VALUE

EXIT FROM
PERFORM
STATEMENT EXIT

Example

PROCEDURE DIVISION.
CCMIT-RTN.
READ STUDENT AT END MOVE 1 TO EOFSW.
PERFORM BSIT-RTN VARYING I FROM 1 BY 1 UNTIL 1 > 5.
COMPUTE AVE = TQ / 5.
MOVE AVE TO PRT-AVE.
WRITE OUTREC FROM DETAILS AFTER 1 LINE.
BSIT-RTN.
MOVE QUIZ (I) TO PRT-QUIZ (I).
ADD QUIZ (I) TO TQ.
Suppose the set of 5 quizzes are stored from the input file student.

DATA DIVISION .
FILE SECTION .
FD STUDENT
LABEL RECORD IS STANDARD
DATA RECORD IS INREC .
01 QUIZ OCCURS 5 TIMES PIC 99

One of the requirements is to print the table in the output file.


01 DETAIL .
02 PRT-Q OCCURS 5 TIMES .
03 PRT QUIZ PIC 99 .
03 FILLER PIC X 5 .

74
The PERFORM statement will execute. Since this is a conditional perform statement with
a VARYING option, a data field will be first initialized and be compared with the limit value. If the
condition is false, specified paragraph will be executed and the control will return to the same
calling statement, with the data −field being incremented as specified in the same statement, and
again, to be compared with the limit value repeatedly. Until the final value exceeds the limit value
for the PERFORM statement to become true, then the control will proceed to the next statement,
thus terminating the PERFORM statement.

The table of quizzes is a one-level table and is used to accumulate total quiz. This is one
of the several uses of the table in many of the programming languages like COBOL. In our
example I is the only subscript to reference the table of QUIZ, which is a one-level table. I is being
initialized and compared, incremented and compared until I exceeds the limit value which is 5.

c. PERFORM paragraph name VARYING data –field-1 FROM initial-value BY increment-value


UNTIL condition (s) AFTER data-field-2 FROM initial-value BY increment-vale UNTIL
condition(s).

The diagram below illustrates graphically how the PERFORM VARYING AFTER
option works.

MOVE INITIAL
VALUE TO
DATA-FIELD-1

MOVE INITIAL
VALUE TO
DATA-FIELD-2

N N EXECUTE ADD
SPECIFIED INCREMENT
PARAGRAPH VALUE TO
Y Y DATA-FIELD-2

EXIT FROM MOVE INITIAL ADD INCREMENT


PERFORM VALUE TO VALUE TO
STATEMENT DATA-FIELD-2 DATA-FIELD-1

This statement is used to process a Two-level table with One-level-table we used only one
subscript and with Two-level table, there must be two subscript that represents the total number
of two-level table. The PERFORM statement with the VARYING AFTER option is used in COBOL
to allow automatic control over two subscript. The first subscript is the major subscript that
represents the total number of rows and the second subscript is the minor subscript that
represents the total number of columns.

75
RULES FOR DETERMINING TWO-LEVEL TABLES
1. In the definition f a two-level table, the OCCURS clause for the row is stated first,
followed by the OCCURS clause for the columns.
2. The PIC clause is listed with the second OCCURS clause.
3. To access an element of two-level table, two subscripts or indexes are needed. There
must be a blank space between the comma that appears after the first subscript and
before the second subscript.

DISPLAY & ACCEPT Statement

DISPLAY Statement is a COBOL statement that is used for printing a low volume of output.

DISPLAY Statement is used to Display the data on Output device. This is a procedure
division statement and very useful to get the required data on output device. Identifier is any
declared variable of the program.

Format:
DISPLAY identifier-1… .
literal-1
DISPLAY identifier-1… LINE integer-value COLUMN integer-value.
literal-1
DISPLAY ( line-no. , column-no ) identifier-1… .
literal-1
Examples:
DISPLAY “ Average Grade is : “ .
DISPLAY AVERAGE .
DISPLAY “ Average Grade is : “ , AVERAGE .
DISPLAY “ Average Grade is : “ , AVERAGE LINE 5 COLUMN 15.
DISPLAY (5, 15) “ Average Grade is : “ , AVERAGE .

ACCEPT Statement is a COBOL statement that request actual data from the keyboard.

ACCEPT statement accepts the data from the outside of the program which may be from input
device or system defined data items. ACCEPT transfers the accepted data to the specified
identifier along with ACCEPT. ACCEPT statement used to receive the data during the program
execution.

Format:
ACCEPT identifier .
ACCEPT identifier LINE integer-value COLUMN integer-value.
ACCEPT (row, column ) identifier.

Example:
ACCEPT NAME.
ACCEPT NAME LINE 7 COLUMN 11 .
ACCEPT (7, 11 ) NAME .

76
Sample Program #1:
* USING ACCEPT & DISPLAY *
* WITHOUT INPUT/OUTPUT FILES *
IDENTIFICATION DIVISION.
PROGRAM-ID. ACPTDSP1.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 VARIABLES.
05 NAME PIC X(25).
05 MIDGRD PIC 9V99.
05 FINGRD PIC 9V99.
05 AVE PIC 9V99.
05 AVE-OUT PIC 9.999.
05 ANS PIC X VALUE 'Y'.
PROCEDURE DIVISION.
MAIN-RTN.
PERFORM PROCESS-RTN THRU PROCESS-END
UNTIL ANS = 'N' OR ANS = 'n'.
STOP RUN.
PROCESS-RTN.
DISPLAY "Enter Name: ".
ACCEPT NAME.
DISPLAY "Enter Midterm Grade: ".
ACCEPT MIDGRD.
DISPLAY "Enter Final Grade: ".
ACCEPT FINGRD.
COMPUTE AVE = (MIDGRD + FINGRD) / 2.
MOVE AVE TO AVE-OUT.
DISPLAY "Average Grade is: ", AVE-OUT.
DISPLAY "ENTER ANOTHER [Y/N]? ".
ACCEPT ANS.
PROCESS-END.

Sample Run 1: Sample Run 2:


Enter Name: Elias Austria Enter Name: Alet Fabregas
Enter Midterm Grade: 1.75 Enter Midterm Grade: 1.75
Enter Final Grade: 1.25 Enter Final Grade: 2.25
Average Grade is: 1.50 Average Grade is: 2.00

ENTER ANOTHER [Y/N]? Y ENTER ANOTHER [Y/N]? N

Sample Program #2:


* USING ACCEPT & DISPLAY *
* WITH OUTPUT FILE *

IDENTIFICATION DIVISION.
PROGRAM-ID. ACPTDSP1.

77
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT GRDFILE ASSIGN TO 'C:\COBOL\GRDFILE.TXT'.
DATA DIVISION.
FILE SECTION.
FD GRDFILE.
01 GRDREC.
05 FILLER PIC X(80).
WORKING-STORAGE SECTION.
* INPUT DATA USING *
* ACCEPT & DISPLAY *
01 VARIABLES.
05 NAME PIC X(25).
05 MIDGRD PIC 9V99.
05 FINGRD PIC 9V99.
05 AVE PIC 9V99.
05 ANS PIC X.
05 VALIDANS PIC 9 VALUE 0.
* OUTPUT FILE *
01 HDG.
05 FILLER PIC X(32) VALUE SPACES.
05 FILLER PIC X(25) VALUE "STUDENT'S GRADE".
05 FILLER PIC X(33) VALUE SPACES.
01 COLHDG.
05 FILLER PIC X(14) VALUE SPACES.
05 FILLER PIC X(12) VALUE "STUDENT NAME".
05 FILLER PIC X(28) VALUE SPACES.
05 FILLER PIC X(12) VALUE "FINAL GRADE ".
01 GRDDATA.
05 FILLER PIC X(14) VALUE SPACES.
05 NAME-OUT PIC X(25).
05 FILLER PIC X(20) VALUE SPACES.
05 AVE-OUT PIC 9.999.
PROCEDURE DIVISION.
MAIN-RTN.
OPEN OUTPUT GRDFILE.
WRITE GRDREC FROM HDG AFTER 1.
WRITE GRDREC FROM COLHDG AFTER 1.
PERFORM PROCESS-RTN THRU PROCESS-END
UNTIL VALIDANS = 1.
CLOSE GRDFILE.
STOP RUN.
PROCESS-RTN.
DISPLAY "Enter Name: ".
ACCEPT NAME.
DISPLAY "Enter Midterm Grade: ".
ACCEPT MIDGRD.
DISPLAY "Enter Final Grade: ".
ACCEPT FINGRD.
COMPUTE AVE = (MIDGRD + FINGRD) / 2.

78
MOVE NAME TO NAME-OUT.
MOVE AVE TO AVE-OUT.
DISPLAY "Average Grade is: ", AVE-OUT.
WRITE GRDREC FROM GRDDATA AFTER 2.
DISPLAY "ENTER ANOTHER [Y/N]? ".
PERFORM ANS-RTN THRU ANS-END.
PROCESS-END.
ANS-RTN.
ACCEPT ANS.
IF ANS = 'Y' OR ANS = 'y'
PERFORM PROCESS-RTN THRU PROCESS-END
ELSE IF ANS = 'N' OR ANS = 'n'
MOVE 1 TO VALIDANS
ELSE
DISPLAY "INVALID INPUT"
DISPLAY "ENTER ANOTHER [Y/N]? "
PERFORM ANS-RTN THRU ANS-END.
ANS-END.
Sample Run 1: Sample Run 2:

Enter Name: Rainah Casandra A. Nayre Enter Name: Mark Jezekiah A. Nayre

Enter Midterm Grade: 1.75 Enter Midterm Grade: 1.75

Enter Final Grade: 1.25 Enter Final Grade: 2.25

Average Grade is: 1.50 Average Grade is: 2.00

ENTER ANOTHER [Y/N]? Y ENTER ANOTHER [Y/N]? Y

Sample Run 3: Sample Run 4:

Enter Name: Angelie Natasha J. Atian Enter Name: Angel J. Atian

Enter Midterm Grade: 3.00 Enter Midterm Grade: 2.25

Enter Final Grade: 3.00 Enter Final Grade: 2.75

Average Grade is: 3.00 Average Grade is: 2.50

ENTER ANOTHER [Y/N]? Y ENTER ANOTHER [Y/N]? X

Sample Run 5: Sample Run 6:

Enter Name: Angel J. Atian Enter Name: Angel J. Atian

Enter Midterm Grade: 2.25 Enter Midterm Grade: 2.25

Enter Final Grade: 2.75 Enter Final Grade: 2.75

Average Grade is: 2.50 Average Grade is: 2.50

ENTER ANOTHER [Y/N]? Z ENTER ANOTHER [Y/N]? N

79
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

Activities/ Assessments:

GIVEN: Input data which are going to be inputted by the user.


INPUT & OUTPUT LAYOUT: (SCREEN LAYOUT)

ENTER EMPLOYEE NO.: X(5)


ENTER EMPLOYEE NAME: X(25)
RATE PER HOUR: 9(3)V99
NO. OF HOURS WORKED: 9(3)
GROSS SALARY: ZZ,999.99 (rate * working hours)
GSIS CONTRIBUTION: 9(4)V99
PHILHEALTH: 9(3)V99
PAG-IBIG: 9(4)V99
WITHOLDING TAX: 9(4)V99
DEDUCTIONS: ZZ,999.99(total deductions)
NET PAY: ZZ,999.99 (gross salary –
deductions)

ENTER ANOTHER [Y / N ]: X

TOTAL NUMBER OF EMPLOYEES: ZZ,ZZ9


TOTAL ACCUMULATED SALARIES: Z,ZZZ,999.99
REQUIRED:

Based on the input data, compute and display the total gross salary and the total deductions for
each employee. Compute also for the total net pay and store it in an output file based on the given
output layout. Output also the Total Number of Employees and Total Accumulated Salaries of the
all the employees.

OUTPUT LAYOUT: (OUTPUT FILE LAYOUT)

POLYTECHNICUNIVERSITY OF THE PHILIPPINES

PAYROLL REPORT

EMPLOYEE EMPLOYEE NET


NUMBER NAME PAY

X(5) X(25) ZZ,999.99

TOTAL NUMBER OF EMPLOYEES: ZZ,ZZ9


80
TOTAL ACCUMULATED SALARIES: Z,ZZZ,999.99
81

You might also like