Вы находитесь на странице: 1из 33

Introduction

Aims To provide a brief introduction to the programming language COBOL. To provide a


context in which its uses might be understood. To introduce the Metalanguage used to
describe syntactic elements of the language. To provide an introduction to the major
structures present in a COBOL program.
Objectives By the end of this unit you should -

1. Know what the acronym COBOL stands for.


2. Be aware of the significance of COBOL in the marketplace.
3. Understand some of the reasons for COBOL's success.
4. Be able to understand COBOL Metalanguage syntax diagrams.
5. Be aware of the COBOL coding rules
6. Understand the structure of COBOL programs
7. Understand the purpose of the IDENTIFICATION, ENVIRONMENT, DATA and
PROCEDURE divisions.

Prerequisites None. This is the first unit in the course.

What is COBOL?
Introduction COBOL is a high-level programming language first developed by the CODASYL
Committee (Conference on Data Systems Languages) in 1960. Since then,
responsibility for developing new COBOL standards has been assumed by the
American National Standards Institute (ANSI).

Three ANSI standards for COBOL have been produced: in 1968, 1974 and 1985. A
new COBOL standard introducing object-oriented programming to COBOL, is due
within the next few years.

The word COBOL is an acronym that stands for COmmon Business Oriented
Language. As the the expanded acronym indicates, COBOL is designed for developing
business, typically file-oriented, applications. It is not designed for writing systems
programs. For instance you would not develop an operating system or a compiler using
COBOL.
How widely used For over four decades COBOL has been the dominant programming language in the
is COBOL? business computing domain. In that time it it has seen off the challenges of a number
of other languages such as PL1, Algol68, Pascal, Modula, Ada, C, C++. All these
languages have found a niche but none has yet displaced COBOL. Two recent
challengers though, Java and Visual Basic, are proving to be serious contenders.

COBOL's dominance in underlined by the reports from the Gartner group.

In 1997 they estimated that there were about 300 billion lines of computer code in use
in the world. Of that they estimated that about 80% (240 billion lines) were in COBOL
and 20% (60 billion lines) were written in all the other computer languages combined
[Brown].

In 1999 they reported that over 50% of all new mission-critical applications were still
being done in COBOL and their recent estimates indicate that through 2004-2005 15%
of all new applications (5 billion lines) will be developed in COBOL while 80% of all
deployed applications will include extensions to existing legacy (usually COBOL)
programs.

Gartner estimates for 2002 are that there are about two million COBOL programmers
world-wide compared to about about one million Java programmers and one million
C++ programmers.

Surprised by People are often surprised when presented with the evidence for COBOL's dominance
COBOL's in the market place. The hype that surrounds some computer languages would
success? persuade you to believe that most of the production business applications in the world
are written in Java, C, C++ or Visual Basic and that only a small percentage are
written in COBOL. In fact, the reverse is actually the case.

One reason for this misconception lies in the difference between the vertical and the
horizontal software markets.

In the vertical software market (sometimes called "bespoke" software) applications


cost many millions of dollars to produce, are tailored to a specified company,
encapsulate the business rules of that company, and only a limited number of copies of
the software may be in use. A good example of this kind of application is the DoD
MRP II system. This system is "used to manage almost 550,000 spare and repair parts
and equipment items with an inventory value of $28 billion. The system runs on
Amdahl mainframes at multiple locations throughout the U.S. and contains over
4,000,000 lines of COBOL code."
[http://www.uppermohawkinc.com/corporate_capabilities.htm]

In the horizontal software market, applications may still cost millions of dollars to
produce but thousands, and in some cases millions, of copies of the software are in use
As a result, these applications often have a very high profile, a short life span, and a
relatively low per-copy replacement cost. The Microsoft Office suite (Word, Excel,
Access) is an example of an application in the horizontal software market. Because of
the highly competitive nature of this marketplace considerations of speed, size and
efficiency often make languages like C or C++ the language of choice for creating
these applications.

Applications written for the vertical market, on the other hand, often have a low profile
(because they are usually written for use in one particular company), a very high per-
copy replacement cost, and consequently, a very long life span. For example, the cost
of replacing COBOL code has been estimated at approximately twenty five dollars
($25) per line of code. At this rate, the cost of replacing the DoD MRP II system
mentioned above, with a system written in some other language, would be some one
hundred million dollars ($100,000,000). The importance of ease of maintenance often
makes COBOL the language of choice for these applications.

The high visibility of horizontal applications like Microsoft Word or Excel persuades
people that the languages used to write these applications are the market leaders. But
however many copies of Excel are sold, it is just a single application produced by a
limited number of programmers. Many more programmers are involved in coding or
maintaining one off, "bespoke", applications. And these programmers generally write
their programs in COBOL.

Some As exemplified by the DoD MRP II example above, COBOL applications are often
characteristics of very large. Many COBOL applications consist of more than 1,000,000 lines of code -
COBOL with 6,000,000+ line applications not considered unusually large in many shops.
applications
COBOL applications are also very long-lived. The huge investment in creating a
software application consisting of some millions of lines of COBOL code means that
the application cannot simply be discarded when some new programming language or
technology appears. As a consequence business applications between 10 and 30 years-
old are common. This accounts for the predominance of COBOL programs in the year
2000 problem (12,000,000 COBOL applications vs 375,000 C and C++ applications in
the US alone - [Capers Jones]). Twenty years ago when programmers were writing these
applications they just didn't anticipate that they would last into this millennium.

COBOL applications often run in critical areas of business. For instance, over 95% of
finance–insurance data is processed with COBOL [In Cobol’s Defense]. The serious
financial and legal consequences that can result from an application failure is one
reason for the near panic over the year 2000 problem.

COBOL applications often deal with enormous volumes of data. Single production
files and databases measured in terabytes are not uncommon.
Some COBOL is self-documenting
characteristics One of the design goals for COBOL was to make it possible for non-programmers
that contribute to such as supervisors, managers and users, to read and understand COBOL code. As a
COBOL's result, COBOL contains such English-like structural elements as verbs, clauses,
success 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 is very important, to dismiss the
advantage that 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 programs 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

Readers who are familiar with C or C++ or Java might want to consider how difficult
it becomes to maintain programs written in these languages. C programs that you have
written yourself are difficult enough to understand when you come back to them six
months later. Consider how much more difficult it would be to understand a program
that had been written fifteen years previously, by someone else, and which had since
been amended and added to by so many others that the documentation no longer
accurately reflects the program code. This is a nightmare still awaiting maintenance
programmers of the future

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 -
References and In this semi-formal document I have not been fastidious in referencing all the souces I
further reading have used. Because of that I would like to acknowledge that most of factual material
and some of the commentary was gleaned from the following sources. These may also
serve as further reading.

Most of this material is available on the web but as the web is dynamic and links are
all to easily broken I won't give the URL's here. You will have to search for them.

Resources

Ankrum,T. Scott - COBOL -- A Best Practice (Sept, 2001)- COBOLReport.com

Arranga,Edmund C. - The Viagrazation of COBOL - COBOLwebler.com

Arranga, Edmund C. & Price, Wilson - Fresh from Y2K, What's next for COBOL?
(March/April 2000) - IEEE Software

Arranga et al - In COBOL's Defense : Roundtable Discussion (March/April 2000) -


IEEE Software

Badower, Justin - COBOL: Foundation of the future - COBOLwebler.com

Brown, Gary DeWard - COBOL: The failure that wasn't - COBOLReport.com

Burger,Thomas Wolfgang - COBOL in an open source future (May 2000) - IBM


developerWorks : Linux : Linux articles

Carr, Donald & Kizior, Ronald J. - The Case for Continued COBOL Education
(March/April 2000) - IEEE Software

Feiman, J. - The Gartner Programming Language Survey (October 2001) - Gartner


Advisory

Glass, Robert L. - Cobol - A Contradiction and an Enigma - COMMUNICATIONS


OF THE ACM September 1997/Vol. 40, No. 9

Jones, Capers - The global economic impact of the year 2000 software problem (Jan,
1997)

Kappelman, Leon A. - Some Strategic Y2K Blessings (March/April 2000) - IEEE


Software

Kizior, Dr. Ronald J. & Carr, Donald & Halpern, Dr. Paul - What Professionals think
of the Future of COBOL?

Murach, Mike - Is COBOL Dying ... or Thriving? (February 2001) - The Cobol
Newswire
Introduction to Programming
Introduction In this section a gentle introduction to programing in general, and to programming in
COBOL in particular, is provided. This is done by writing some simple COBOL
programs that use the three main programming constructs - Sequence, Iteration and
Selection.

Don't worry if you don't understand these programs at this point. The main purpose of
this section is to give you a first look at some simple COBOL programs.

Let's write a A program is a collection of statements written in a language the computer


program understands.

A computer executes program statements one after another in sequence until it reaches
the end of the program unless some statement in the program alters the order of
execution.

Computer Scientists have shown that any program can be written using the three main
programming constructs;

Sequence
Selection
Iteration

This section introduces COBOL programming by writing some simple COBOL


programs using these constructs.

Sequence We want to write a program which will accept two numbers from the users keyboard,
Program multiply them together and display the result on the computer screen.
Specification
Any program consists of three main things;

1. The computer statements needed to do the job


2. Declarations for the data items that the computer statements need.
3. A plan, or algorithm, that arranges the computer statements in the program so
that the computer executes them in the correct order.
Program What COBOL program statements will we need to do the job specified above and wha
Statements and data items will we need to access?
Data items
We will need a statement to take in the first number and store it in the named memory
location (a variable) - Num1
ACCEPT Num1.

We will need a statement to take in the second number and store it in the named
memory location - Num2
ACCEPT Num2.

We will need a statement to multiply the two numbers together and to store the result
in the named location - Result
MULTIPLY Num1 BY Num2 GIVING Result.

We will need a statement to display the value in the named memory location "Result"
on the computer screen -
DISPLAY "Result is = ", Result.

Getting the Now all we need to do to have a working program is to declare the items needed to
Algorithm right store the data and to place the statements shown above in the correct order.

Click on these animations to see our attempts to write a program that produces the
correct answer. These attempts illustrate importance of arranging the statements in a
program so that they are executed in the correct order.

Attempt 1 Attempt 2 Attempt 3

Because this program is short, simple and easy to understand, you may think that
programming mistakes like these could never happen. But don't be deceived - when
writing a larger program it is all to easy to make the mistake of trying to use, or output
the contents of a data item before you have assigned it a value.
An annotated Click on the animation below for an annotated version of the program. Click anywhere
look at the in the animation to see the first and subsequent annotations.
COBOL program
Annotated Program

Selection Write a program that accepts two numbers and an operator from the user and then
Program performs the appropriate calculation for that operator. The operator must be either the
Specification addition (+) or the multiplication (*) operator.

In this example run it is assumed that the user enters an addition sign (+) as the
operator
Note that these example
programs are not meant Selection Program
to be realistic. For
instance, at the very
least, a program
operating in the real
world would have to
ensure that the input data
was validated before it
was used.
Iteration Program Write a program that accepts two numbers and an operator from the user and then
Specification performs the appropriate calculation for that operator. The operator must be either the
addition (+) or the multiplication (*) operator.

This is only a partial example run. It follows the flow of control through the program
for only one and a half iterations.

Iteration Program

COBOL basics
Introduction This section presents the fundamentals of constructing COBOL programs. It explains
the notation used in COBOL syntax diagrams and enumerates the COBOL coding
rules. It shows how user-defined names are constructed and examines the structure of
COBOL programs.

COBOL COBOL is one of the oldest programming languages in use. As a result it has some
idiosyncrasies idiosyncrasies which programmers used to other languages may find irritating.

When COBOL was developed (around the end of the 1950's) one of the design goals
was to make it as English-like as possible. As a result, COBOL uses structural
concepts normally associated with English prose such as section, paragraph and
sentence. It also has an extensive reserved word list with over 300 entries and the
reserved words themselves, tend to be long. COBOL programs tend to be verbose
especially when compared to languages like C.

When COBOL was designed, programs were written on coding forms (see below) ,
punched on to punch cards, and loaded into the computer using a punch card reader.
These media (coding forms and punch cards) required adherence to a number
formatting restrictions that some COBOL implementations still enforce today, long
after the need for them has gone.

Although modern COBOL (COBOL 85 and OO-COBOL) has introduced many of the
constructs required to write well structured programs it also still retains elements
which, if used, make it difficult, and in some cases impossible, to write good
programs.
COBOL syntax COBOL syntax is defined using particular notation sometimes called the COBOL
MetaLanguage.

In this notation, words in uppercase are reserved words. When underlined they are
mandatory. When not underlined they are "noise" words, used for readability only, and
are optional. Because COBOL statements are supposed to read like English sentences
there are a lot of these "noise" words.

Words in mixed case represent names that must be devised by the programmer (like
data item names).

When material is enclosed in curly braces { }, a choice must be made from the options
within the braces. If there is only one option then that item in mandatory.

Material enclosed in square brackets [ ], indicates that the material is optional, and may
be included or omitted as required.

The ellipsis symbol ... (three dots), indicates that the preceding syntax element may be
repeated at the programmer's discretion.

Some notes on To simplify the syntax diagrams and reduce the number of rules that must be
syntax diagrams explained, in some diagrams special operand endings have been used (note that this is
my own extension - it is not standard COBOL).

These special operand endings have the following meanings:

$i uses an alphanumeric data-item


$il uses an alphanumeric data-item or a string literal
#i uses a numeric data-item
#il uses a numeric data-item or numeric literal
$#i uses a numeric or an alphanumeric data-item
An example In COBOL, evaluating an arithmetic expression and assigning the result to a data item
syntax diagram is achieved by means of the COMPUTE statement. The syntax diagram for the
COMPUTE is shown below.

This syntax diagram may be interpreted as follows;

We must start a COMPUTE statement with the keyword COMPUTE.

We must follow the keyword with the name(s) of the numeric data item (or items -
note the ellipsis symbol (...)) to be used to receive the result of the expression. The #i
suffix at the end of word Result tells us that a numeric identifier/data item must be
used.

Since the ellipsis symbol is placed outside the curly brackets we can interpret this to
mean that each result field can have its own ROUNDED phase. In other words we could
have a COMPUTE statement like -
Note that for clarity data
items may be separated COMPUTE Result1 ROUNDED, Result2 = ((9*9)+8)/5
from one another by
means of an optional
comma. where Result1 would be assigned a value of 18 and Result2 would be assigned a value
This has been done in of 17.8.
the COMPUTE
statement opposite
The square brackets after the Arithmetic Expression indicate that the next items are
optional but if used we must choose between the ON SIZE ERROR or NOT ON SIZE
ERROR phrases.

Because the END-COMPUTE is contained within the square brackets it must only be
used when a SIZE ERROR or NOT SIZE ERROR phrase is used.
COBOL coding Traditionally, COBOL programs were written on coding forms and then punched on to
rules punch cards. Although nowadays most programs are entered directly into a computer,
some COBOL formatting conventions remain that derive from its ancient punch-card
history.

On coding forms, the first six character positions are reserved for sequence numbers.
The seventh character position is reserved for the continuation character, or for an
asterisk that denotes a comment line.

The actual program text starts in column 8. The four positions from 8 to 11 are known
as Area A, and positions from 12 to 72 are Area B.

Although many COBOL compilers ignore some of these formatting restrictions, most
still retain the distinction between Area A and Area B.

When a COBOL compiler recognizes the two areas, all division names, section names,
paragraph names, FD entries and 01 level numbers must start in Area A. All other
sentences must start in Area B.

In our example programs we use the compiler directive (available with the NetExpress
COBOL compiler) - $ SET SOURCEFORMAT"FREE" - to free us from these formatting
restrictions.

Ancient COBOL coding form


Name All user-defined names, such as data names, paragraph names, section names condition
construction names and mnemonic names, must adhere to the following rules:

1. They must contain at least one character, but not more than 30 characters.
2. They must contain at least one alphabetic character.
3. They must not begin or end with a hyphen.
4. They must be constructed from the characters A to Z, the numbers 0 to 9, and
the hyphen.
5. They must not contain spaces.
6. Names are not case-sensitive: TotalPay is the same as totalpay, Totalpay or
TOTALPAY.

The structure of COBOL programs are hierarchical in structure. Each element of the hierarchy consists
COBOL of one or more subordinate elements.
programs
The hierarchy consists of Divisions, Sections, Paragraphs, Sentences and Statements.

A Division may contain one or more Sections, a Section one or more Paragraphs, a
Paragraph one or more Sentences and a Sentence one or more Statements.

We can represent the COBOL hierarchy using the COBOL metalanguage as follows;

Divisions
A division is a block of code, usually containing one or more sections, that starts
where the division name is encountered and ends with the beginning of the next
division or with the end of the program text.

Sections
A section is a block of code usually containing one or more paragraphs. A section
begins with the section name and ends where the next section name is encountered or
where the program text ends.

Section names are devised by the programmer, or defined by the language. A section
name is followed by the word SECTION and a period.
See the two example names below -

SelectUnpaidBills SECTION.
FILE SECTION.

Paragraphs
A paragraph is a block of code made up of one or more sentences. A paragraph begins
with the paragraph name and ends with the next paragraph or section name or the end
of the program text.

A paragraph name is devised by the programmer or defined by the language, and is


followed by a period.
See the two example names below -

PrintFinalTotals.
PROGRAM-ID.

Sentences and statements


A sentence consists of one or more statements and is terminated by a period.
For example:

MOVE .21 TO VatRate


MOVE 1235.76 TO ProductCost
COMPUTE VatAmount = ProductCost * VatRate.

A statement consists of a COBOL verb and an operand or operands.


For example:

SUBTRACT Tax FROM GrossPay GIVING NetPay


The Four Divisions
Introduction At the top of the COBOL hierarchy are the four divisions. These divide the program
into distinct structural elements. Although some of the divisions may be omitted, the
sequence in which they are specified is fixed, and must follow the order below.

IDENTIFICATION DIVISION.
Contains program information

ENVIRONMENT DIVISION.
Contains environment information

DATA DIVISION.
Contains data descriptions

PROCEDURE DIVISION.
Contains the program algorithms
The The IDENTIFICATION DIVISION supplies information about the program to the
IDENTIFICATION programmer and the compiler.
DIVISION
Most entries in the IDENTIFICATION DIVISION are directed at the programmer. The
compiler treats them as comments.

The PROGRAM-ID clause is an exception to this rule. Every COBOL program must
have a PROGRAM-ID because the name specified after this clause is used by the linker
when linking a number of subprograms into one run unit, and by the CALL statement
when transferring control to a subprogram.

The IDENTIFICATION DIVISION has the following structure:

IDENTIFICATION DIVISION
PROGRAM-ID. NameOfProgram.
[AUTHOR. YourName.]
other entries here

The keywords - IDENTIFICATION DIVISION - represent the division header, and signal
the commencement of the program text.

PROGRAM-ID is a paragraph name that must be specified immediately after the division
header.

NameOfProgram is a name devised by the programmer, and must satisfy the rules for
user-defined names.

Here's a typical program fragment:

IDENTIFICATION DIVISION.
PROGRAM-ID. SequenceProgram.
AUTHOR. Michael Coughlan.
The The ENVIRONMENT DIVISION is used to describe the environment in which the
ENVIRONMENT program will run.
DIVISION
The purpose of the ENVIRONMENT DIVISION is to isolate in one place all aspects of the
program that are dependant upon a specific computer, device or encoding sequence.

The idea behind this is to make it easy to change the program when it has to run on a
different computer or one with different peripheral devices.

In the ENVIRONMENT DIVISION, aliases are assigned to external devices, files or


command sequences. Other environment details, such as the collating sequence, the
currency symbol and the decimal point symbol may also be defined here.
The DATA As the name suggests, the DATA DIVISION provides descriptions of the data-items
DIVISION processed by the program.

The DATA DIVISION has two main sections: the FILE SECTION and the WORKING-
STORAGE SECTION. Additional sections, such as the LINKAGE SECTION (used in
subprograms) and the REPORT SECTION (used in Report Writer based programs) may
also be required.

The FILE SECTION is used to describe most of the data that is sent to, or comes from,
the computer's peripherals.

The WORKING-STORAGE SECTION is used to describe the general variables used in the
program.

The DATA DIVISION has the following structure and syntax:

Below is a sample program fragment -

IDENTIFICATION DIVISION.
PROGRAM-ID. SequenceProgram.
AUTHOR. Michael Coughlan.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 Num1 PIC 9 VALUE ZEROS.
01 Num2 PIC 9 VALUE ZEROS.
01 Result PIC 99 VALUE ZEROS.
The The PROCEDURE DIVISION contains the code used to manipulate the data described in
PROCEDURE the DATA DIVISION. It is here that the programmer describes his algorithm.
DIVISION
The PROCEDURE DIVISION is hierarchical in structure and consists of sections,
paragraphs, sentences and statements.

Only the section is optional. There must be at least one paragraph, sentence and
statement in the PROCEDURE DIVISION.

Paragraph and section names in the PROCEDURE DIVISION are chosen by the
programmer and must conform to the rules for user-defined names.

Sample Program

IDENTIFICATION DIVISION.
PROGRAM-ID. SequenceProgram.
AUTHOR. Michael Coughlan.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Num1 PIC 9 VALUE ZEROS.
01 Num2 PIC 9 VALUE ZEROS.
01 Result PIC 99 VALUE ZEROS.

PROCEDURE DIVISION.
CalculateResult.
ACCEPT Num1.
ACCEPT Num2.
MULTIPLY Num1 BY Num2 GIVING Result.
DISPLAY "Result is = ", Result.
STOP RUN.

Some COBOL compilers require that all the divisions be present in a program while
others only require the IDENTIFICATION DIVISION and the PROCEDURE DIVISION. For
instance the program shown below is perfectly valid when compiled with the
Microfocus NetExpress compiler.

Minimum COBOL program


IDENTIFICATION DIVISION.
PROGRAM-ID. SmallestProgram.
PROCEDURE DIVISION.
DisplayGreeting.
DISPLAY "Hello world".
STOP RUN.

Copyright Notice

These COBOL course materials are the copyright property of Michael Coughlan.

All rights reserved. No part of these course materials may be reproduced in any form or by any means - graphic, electronic,
mechanical, photocopying, printing, recording, taping or stored in an information storage and retrieval system - without the written
permission of the author.

(c) Michael Coughlan

Last updated : April 2002


e-mail : CSISwebeditor@ul.ie

Declaring Data in COBOL


Introduction
Unit aims, objectives, prerequisites and further reading.

Categories of COBOL data


This section introduces the three categories of COBOL data - Literals, Variables and Figurative Constants.

Declaring Data-Items in COBOL


In this section we show how variables are declared in COBOL using the PICTURE clause.

Group and Elementary Data-Items


This section demonstrates how record structures can be specified using an appropriate arrangement of level numbers.

Introduction
Aims The aim of this unit is give you an understanding of the different categories of data
used in a COBOL program and to demonstrate how items of each category may be
created and used.

Objectives By the end of this unit you should -

1. Know how to use and create literals, variables and Figurative Constants.
2. Understand how to declare numeric, alphabetic and alphanumeric data-
items.
3. Be able to create a record structure by assigning the appropriate level
numbers to its data-items.
4. Understand the difference between group and an elementary data-items.
5. Be able to assign an initial value to a variable.

Prerequisites Introduction to COBOL.

but more information on declaring data items in COBOL can be found in the units
covering

• Edited Pictures
• Sequential Files

• The USAGE clause


Further reading More information on declaring data items in COBOL can be found in the units
covering -

• Edited Pictures
• Sequential Files

• The USAGE clause

Categories of COBOL data


Introduction There are three categories of data item used in COBOL programs:

o Variables.
o Literals.
o Figurative Constants.

Variables A data-name or identifier is the name used to identify the area of memory reserved
for a variable. A variable is a named location in memory into which a program can
put data, and from which it can retrieve data.

Every variable used in a COBOL program must be described in the DATA DIVISION.

In addition to the data-name, a variable declaration also defines the type of data to
be stored in the variable. This is known as the variable's data type.
Variable Data types Some languages like Modula-2,Pascal or Ada are described as being strongly typed.
In these languages there are a large number of different data types and the
distinction between them is rigorously enforced by the compiler. For instance, the
compiler will reject a statement that attempts to assign character value to an integer
data item.

In COBOL, there are really only three data types -

• numeric
• alphanumeric (text/string)
• alphabetic

The distinction between these data types is a little blurred and only weakly enforced
by the compiler. For instance, it is perfectly possible to assign a non-numeric value
to a data item that has been declared to be numeric.
Attempting to perform
computations on numeric The problem with this lax approach to data typing is that, since COBOL programs
data items that contain non- crash (halt unexpectedly) if they attempt to do computations on items that contain
numeric data is a frequent non-numeric data, it is up to the programmer to make sure this never happens.
cause of program crashes
for beginning COBOL
programmers. COBOL programmers must make sure that non-numeric data is never assigned to
numeric items intended for use in calculations. Programmers who use strongly
typed languages don't need this level of discipline because the compiler ensures that
a variable of a particular types can only be assigned appropriate values.

Literals A literal is a data-item that consists only of the data-item value itself. It cannot be
referred to by a name. By definition, literals are constant data-items.

There are two types of literal -

• String/Alphanumeric Literals

• Numeric Literals
String Literals String/Alphanumeric literals are enclosed in quotes and consist of alphanumeric
characters.

For example: "Michael Ryan", "-123", "123.45"

Numeric Literals Numeric literals may consist of numerals, the decimal point, and the plus or minus
sign. Numeric literals are not enclosed in quotes.

For example: 123, 123.45, -256, +2987

Figurative Unlike most other programming languages COBOL does not provide a mechanism
Constants for creating user-defined constants but it does provide a set of special constants
called Figurative Constants.

A Figurative Constant may be used wherever it is legal to use a literal but unlike
literals, when a Figurative Constant is assigned to a data-item it fills the whole item
overwriting everything in it.

Actually COBOL does allow The Figurative Constants are:


you to set up single
character user-defined
Figurative Constants. These SPACE or SPACES Acts like one or more spaces
can be useful if you need to
use the non-printable ASCII ZERO or ZEROS or ZEROES Acts like one or more zeros
characters such as ESC or
FormFeed. QUOTE or QUOTES Used instead of a quotation mark
User-defined Figurative HIGH-VALUE or HIGH- Uses the maximum value possible
Constants are declared in VALUES
the SYMBOLIC CHARACTERS
clause of the ENVIRONMENT LOW-VALUE or LOW-VALUES Uses the minimum value possible
DIVISION.
ALL literal Allows a ordinary literal to act as
In an extension that Figurative Constant
previews the new COBOL
specification, NetExpress
does allow user-defined Figurative Constant Notes
constants. It uses the level
78 for this purpose.
• When the ALL Figurative Constant is used, it must be followed by a one
character literal. The designated literal then acts like the standard Figurative
Constants.
• ZERO, ZEROS and ZEROES are synonyms, not separate Figurative Constants.
The same applies to SPACE and SPACES, QUOTE and QUOTES, HIGH-VALUE
and HIGH-VALUES, LOW-VALUES and LOW-VALUES.

Using COBOLdata The animated program fragments below demonstrate how variables, literals and
------ examples ------
Figurative Constants may be created and used.

Declaring Data-Items in COBOL


Introduction Because COBOL is not a typed language like Modula-2 or C it employs a different
mechanism for describing the characteristics of the data-items in a program.

Rather than using types, as these languages do, COBOL uses a kind of "declaration
by example" strategy. The programmer provides the system with an example, or
template, or PICture of the storage required for the data-item.

In COBOL, a variable declaration consists of a line in the DATA DIVISION that


contains the following items:

• A level number.
• A data-name or identifier.
• A Picture clause.

COBOL picture To create the required 'picture' the programmer uses a set of symbols. The most
clauses common symbols used in standard picture clauses are:

9 The digit nine is used to indicate the occurrence of a digit at the


corresponding position in the picture.

X The character X is used to indicate the occurrence of any character


from the character set at the corresponding position in the picture.

A The character A is used to indicate the occurrence of any alphabetic


There are actually many character (A to Z plus blank) at the corresponding position in the
more picture symbols than picture.
these. Most of these will be
introduced when we cover V The character V is used to indicate the position of the decimal point
Edited Pictures. in a numeric value. It is often referred to as the "assumed decimal
point". It is called that because, although the actual decimal point is
not stored, values are treated as if they had a decimal point in that
position.
S The character S indicates the presence of a sign and can only appear
at the beginning of a picture.

Picture clause Although the word PICTURE can be used when defining a picture clause it is normal
notes to use the abbreviation PIC.

Recurring symbols may be specified by using a 'repeat' factor inside brackets. For
instance:

PIC 9(6) is equivalent to PICTURE 999999


PIC 9(6)V99 is equivalent to PIC 999999V99
PICTURE X(10) is equivalent to PIC XXXXXXXXXX
PIC S9(4)V9(4) is equivalent to PIC S9999V9999
PIC 9(18) is equivalent to PIC 999999999999999999

Numeric values can have a maximum of 18 (eighteen) digits.

The limit on string values is usually system dependent.

Group and Elementary data-items


Introduction Although we stated above that each variable declaration consists of a level number,
an identifying name and a picture clause, that definition only applies to elementary
data-items. Group items are defined using only a level-number and an identifying
name; no picture clause is required or allowed.

Which begs the question - what is a group item and what is an elementary item?
Elementary items An "elementary item" is the name we use in COBOL to describe a data-item that
has not been further subdivided. Other languages might describe these as ordinary
variables.

Elementary items must have a picture clause because they actually reserve the
storage required for the item. The amount of storage reserved is specified by the
item's picture clause.

An elementary item declaration consists of;


A variable declaration may
also have a number of other • a level number
clauses such as USAGE or
BLANK WHEN ZERO • a data name
• a picture clause

A starting value may be assigned to a variable by means of an extension to the


PICTURE clause called the VALUE clause.

Some examples:

01 GrossPay PIC 9(5)V99 VALUE ZEROS.

01 NetPay PIC 9(5)V99 VALUE ZEROS.

01 CustomerName PIC X(20) VALUE SPACES.

01 CustDiscount PIC V99 VALUE .25.

Group items Sometimes when we are manipulating data it is convenient to treat a collection of
elementary items as a single group. For instance, we may want to group the data-
items YearofBirth, MonthofBirth, DayOfBirth under the group name - DateOfBirth
If we are recording information about students we may want to subdivide
StudentName into FirstName, MiddleInitial and Surname. And we may want to use
both these group items and the elementary items StudentId and CourseCode in a
student record description.

We can create groups like these in COBOL using group items. A "group item" is
the term used in COBOL to describe a data-item - like DateOfBirth or StudentName
- that has been further subdivided. In other languages group items might be
described as "structures".

A group item consists of subordinate items. The items subordinate to a group item
may be elementary items or other group items. But ultimately every group item
must be defined in terms of its subordinate elementary items.

In a group item, the hierarchical relationship between the various subordinate items
of the group is expressed using level numbers. The higher the level number, the
lower the item is in the hierarchy. Where a group item is the highest item in a data
hierarchy it is referred to as a "record" and uses the level number 01.

Group items are declared using a level number and a data name only. A group item
cannot have a picture clause because it does not actually reserve any storage. It is
merely a name given to a collection of (ultimately) elementary items which do
reserve the storage.

Therefore, the size of a group item is the sum of the sizes of its subordinate
elementary items.

The type of a group item is always assumed to be PIC X because a group item may
have several different data items and types subordinate to it and an X picture is the
only one which could support such collections.
Level Numbers Level numbers are used to express data hierarchy. The higher the level number, the
lower the item is in the hierarchy. At the lowest level the data is completely atomic.

What is important in a structure defined with level numbers is the relationship of


the level numbers to one another, not the actual level numbers used. For instance,
the record descriptions shown below are equivalent.

Although level numbers Record-A


specify the actual data 01 StudentDetails.
hierarchy you should use 02 StudentId PIC 9(7).
indentation to provide a 02 StudentName.
graphic representation of it. 03 FirstName PIC X(10).
This will make your 03 MiddleInitial PIC X.
programs easier to read. For 03 Surname PIC X(15).
instance, indentation makes 02 DateOfBirth.
it obvious that DayOfBirth, 03 DayOfBirth PIC 99.
MonthOfBirth and 03 MonthOfBirth PIC 99.
YearOfBirth are subordinate 03 YearOfBirth PIC 9(4).
items of DateOfBirth, while 02 CourseCode PIC X(4).
CourseCode is not.

Record-B
01 StudentDetails.
05 StudentId PIC 9(7).
05 StudentName.
07 FirstName PIC X(10).
07 MiddleInitial PIC X.
07 Surname PIC X(15).
05 DateOfBirth.
07 DayOfBirth PIC 99.
07 MonthOfBirth PIC 99.
07 YearOfBirth PIC 9(4).
05 CourseCode PIC X(4).

Some observations It is useful to examine Record-A above and to answer the following questions:
on Record-A
Q1. What is the size (in characters) of Record-A?
Q2. What is the size of the data-item StudentName?
Q3. What is the size of DateOfBirth?
Q4. What is the data type of DateOfBirth? Is it numeric, alphabetic
or alphanumeric.

A1. The size of Record-A is the sum of the sizes of all the
elementary items subordinate to it (7+10+1+15+2+2+4+4 = 45
characters).
A2. The size of StudentName is the sum of the sizes of FirstName,
MiddleInitial and Surname. So StudentName is 26 characters in
size (10+1+15).
A3. DateOfBirth is 8 characters in size (2+2+4).
A4. The data type of DateOfBirth is alphanumeric (i.e. PIC X) even
though all its subordinate items are numeric because the type of
a group item is always alphanumeric.
Level number notes The level numbers 01 through 49 are general level numbers but there are also
special level numbers such as 66, 77 and 88.

• Level 77's can only be used to define individual elementary items. The use
of 77's is banned in some shops who take the view that instead of declaring
large numbers of indistinguishable 77's it is better to collect the individual
items into groups.
• Level 88's are used to define Condition Names.
• Level 66's (RENAMES clause) are used to apply a new name to an identifier
or group of identifiers. It is not generally used in modern COBOL programs

Building a record In the animation below we show how level numbers can be used to define a record
structure structure as hierarchy of data-items.

Copyright Notice

These COBOL course materials are the copyright property of Michael Coughlan.

All rights reserved. No part of these course materials may be reproduced in any form or by any means - graphic, electronic,
mechanical, photocopying, printing, recording, taping or stored in an information storage and retrieval system - without the written
permission of the author.

(c) Michael Coughlan

Last updated : March 1999


e-mail : CSISwebeditor@ul.ie

Starting NetExpress
We will start by loading an existing COBOL program into the development environment
and then compiling and running the program.

The development environment we are using for this course is Microfocus NetExpress.
NetExpress is an Integrated Development Environment (IDE) supporting an Object
Oriented version of COBOL. The OO-COBOL supported by NetExpress is a preview of
the coming standard.

To load the program, click on the GettingStarted.Cbl link. This will allow you
(depending on how your browser is set up) to either download the file to your local hard
disk or load it directly into the NetExpress environment.

If you download the program you should save it to the \WorkArea directory on the drive
D:. When the file has been downloaded, start NetExpress and load the program using
the File option in the main menu bar.

If you load it directly into the NetExpress environment you should use the Save As option
in the File menu to save it as GettingStarted.Cbl to the \WorkArea directory.

The NetExpress programming environment should now be running with the


GettingStarted.Cbl file loaded.

Configuring the NetExpress environment


The default NetExpress Environment makes it difficult to write programs the way we
would like so we need to make some changes to it.

From the NetExpress Options menu select Edit. This brings up a dialogue box with a
number of tabs. Make sure the Profile tab has the focus.

In the Margins option change the left margin to 0.

Now select the Blocks/Clipboard tab.

In the "Selecting line blocks and column blocks" option select "Select column blocks
when dragging, except when in prefix area".

Select OK to close the dialogue box and observe the results.

Compiling the program


Click on the tick icon in the NetExpress toolbar. This compiles the program.

Starting to run the program


For the time being we will run our COBOL programs from within the NetExpress
Animator. Later in the course we will create stand alone programs (.EXE's).

Position the cursor over program statement "ACCEPT Num1.". Click the right mouse
button. From the menu which appears select Set Breakpoint.

Now select "Start Animating" from within the Animate menu option and chose OK from
the dialogue box which appears.

The program starts to run, the first statement in the Procedure Division is highlighted but
is not executed.

Using the Animator


Before we continue with running the program let's have a look at some of the facilities
the Animator provides.

Right click on the variable Num1 in the program statement "MULTIPLY Num1 BY
Num2 GIVING Result. " Take note of all the options that appear in the right-click menu.

Let's suppose that we want to find out the size and type of Num1.

To do this select Locate "Num1" from the right-click menu.

The cursor jumps up to the Num1 declaration. Now right-click on a blank part of the
program text and select "Return" from the right-click menu.

The cursor jumps back to where it came from.

Now let's examine two ways of monitoring the contents of variables.

In the program statement "MULTIPLY Num1 BY Num2 GIVING Result." right click on
the variable Num1.

Select "Examine Num1" from the right-click menu. From the "Examine List" dialogue
that appears select the Monitor option. This creates a little window which is used to
monitor the contents of the variable Num1.
Right-click on Num2 and select "Examine Num2" from the right-click menu. From the
dialogue that appears select the "Add to List" option. This creates a "Watch list" window
and adds Num2 to the watch list.

Do the same for the Result variable.

The watch list window should now have two entries - Num2 and Result.

Continuing with the program run


Let's go back to running the program.

From the NetExpress toolbar select the Step icon (if you position the cursor over each
icon for a short time the computer will display a label for it). You can also select step
from the Animate menu or by pressing Ctrl-S.

The first Procedure Division statement should now be executed. A console window is
created and has the focus and the program waits for the user (that's you) to enter the first
number. Enter the value 3 and press return.

The focus returns to the NetExpress environment.

Look at the monitoring window for the Num1 variable. Note the contents.

Click on the Step icon again to execute the Multiply instruction.

Note that the contents of the Result variable in the watch list window do not change
(3*0=0).

Click on the step icon again and this time enter 5 in the console window and press return.

Note the value that has been assigned to Num2 in the watch list window.

Click on the step icon again to execute the Display instruction.

Note that this time the console window does not take the focus. You have to click on the
console window to give it the focus.

Note that the program produces the wrong answer.

Click on the step icon again to execute the STOP-RUN.

Select "Stop Animating" from the Animate menu.


Select "NO" in response to the question "Do you want to keep the project created for the
animate session".

Fixing the Program


Change the program so that it prompts the user for input (hint DISPLAY … WITH NO
ADVANCING) and produces the correct answer.

Conclusion
See if you can replace the MULTIPLY statement by the equivalent COMPUTE
statement.

The format for the COMPUTE is -

COMPUTE result = operand operator operand

Вам также может понравиться