BOOKS FOR PROFESSIONALS BY PROFESSIONALS®
Coughlan
RELATED
Beginning COBOL for Programmers
Beginning COBOL for Programmers is a comprehensive, sophisticated tutorial and modular
skills reference on the COBOL programming language for established programmers. This
book is for you if you are a developer who would like to—or must—add COBOL to your
repertoire. Perhaps you recognize the opportunities presented by the current COBOL skills
crisis, or are working in a mission critical enterprise which retains legacy COBOL applications.
Whatever your situation, Beginning COBOL for Programmers meets your needs as an
established programmer moving to COBOL.
Beginning COBOL for Programmers includes comprehensive coverage of ANS 85 COBOL
features and techniques, including control structures, condition names, sequential and direct
access files, data redefinition, string handling, decimal arithmetic, subprograms, and the
report writer. The final chapter includes a substantial introduction to object-oriented COBOL.
Benefiting from over one hundred example programs, you’ll receive an extensive
introduction to the core and advanced features of the COBOL language and will learn to apply
these through comprehensive and varied exercises. If you’ve inherited some legacy COBOL,
you’ll be able to grasp the COBOL idioms, understand the constructs, and recognize what’s
happening in the code you’re working with.
Today’s enterprise application developers will find that COBOL skills open new—or old—
doors, and this extensive COBOL reference is the book to help you acquire and develop your
COBOL skills.
What You’ll Learn:
• The basics of COBOL and its control structures
• How to declare and manipulate data, including tabular data
• How to handle sequential, indexed, and relative files
• How to SORT data and use Input and Output procedures
• How to SEARCH tabular data
• How to use contained and external subprograms to create modular programs
• How to use the COBOL Report Writer
• How to write ISO 2002 object-oriented COBOL programs
Shelve in
ISBN 978-1-4302-6253-4
Programming Languages/COBOL
55999
User level:
Beginning–Intermediate
SOURCE CODE ONLINE
9 781430 262534
www.apress.com
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
Contents at a Glance
About the Author ��������������������������������������������������������������������������������������������������������������� xxi
About the Technical Reviewer ����������������������������������������������������������������������������������������� xxiii
Acknowledgments ������������������������������������������������������������������������������������������������������������ xxv
Preface ��������������������������������������������������������������������������������������������������������������������������� xxvii
Chapter 1: Intr
■
oduction to COBOL ��������������������������������������������������������������������������������������1
Chapter 2: COBOL Foundation
■
������������������������������������������������������������������������������������������17
Chapter 3: Data Declar
■
ation in COBOL �����������������������������������������������������������������������������37
Chapter 4: Pr
■
ocedure Division Basics �����������������������������������������������������������������������������55
Chapter 5: Contr
■
ol Structures: Selection �������������������������������������������������������������������������73
Chapter 6: Contr
■
ol Structures: Iteration ������������������������������������������������������������������������109
Chapter 7: Intr
■
oduction to Sequential Files �������������������������������������������������������������������131
Chapter 8: Adv
■
anced Sequential Files ���������������������������������������������������������������������������157
Chapter 9: Edited Pictur
■
es ���������������������������������������������������������������������������������������������181
Chapter 10: Pr
■
ocessing Sequential Files �����������������������������������������������������������������������205
Chapter 11: Cr
■
eating Tabular Data ��������������������������������������������������������������������������������247
Chapter 12: Adv
■
anced Data Declaration ���������������������������������������������
�����������������������������273
Chapter 13: Sear
■
ching Tabular Data ������������������������������������������������������������������������������303
Chapter 14: Sorting and Mer
■
ging ����������������������������������������������������������������������������������327
Chapter 15: String Manipulation
■
�����������������������������������������������������������������������������������361
v
■ Contents at a GlanCe
Chapter 16: Cr
■
eating Large Systems �����������������������������������������������������������������������������399
Chapter 17: Dir
■
ect Access Files �������������������������������������������������������������������������������������435
Chapter 18: The COBOL Report Writer
■
���������������������������������������������������������������������������477
Chapter 19: OO-COBOL
■
���������������������������������������������������������������������������������������������������519
Index ���������������������������������������������������������������������������������������������������������������������������������547
vi
Chapter 1
Introduction to COBOL
When, in 1975, Edsger Dijkstra made his comment that “The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offence,1” he gave voice to, and solidified, the opposition to COBOL in academia.
That opposition has resulted in fewer and fewer academic institutions teaching COBOL so that now it has become difficult to find young programmers to replace the aging COBOL workforce.2-3 This scarcity is leading to an impending COBOL crisis. Despite Dijkstra’s comments and the claims regarding COBOL’s imminent death, COBOL remains
a dominant force in the world of enterprise computing, and attempts to replace legacy COBOL systems have been shown to be difficult, dangerous, and expensive.
In this chapter, I discuss some of the reasons for COBOL’s longevity. You’re introduced to the notion of an application domain and shown the suitability of COBOL for its target domain. COBOL is one of the oldest computer languages, and the chapter gives a brief history of the language and its four official versions. Later, the chapter presents the evidence for COBOL’s dominance in enterprise computing and discusses the enigma of its relatively low profile.
An obvious solution to the scarcity of COBOL programmers is to replace COBOL with a more fashionable
programming language. This chapter exposes the problems with this approach and reveals the benefits of retaining, renovating, and migrating the COBOL code.
Finally, I discuss why learning COBOL and having COBOL on your résumé could be useful additions to your
armory in an increasingly competitive job market.
What Is COBOL?
COBOL is a high-level programming language like C, C#, Java, Pascal, or BASIC, but it is one with a particular focus and a long history.
COBOL’s Target Application Domain
The name COBOL is an acronym that stands for Common Business Oriented Language, and this expanded acronym clearly indicates the target domain of the language. Whereas most other high-level programming languages are general-purpose, domain-independent languages, COBOL is focused on business, or enterprise, computing. You would not use COBOL to write a computer game or a compiler or an operating system. With no low-level access, no dynamic memory allocation, and no recursion, COBOL does not have the constructs that facilitate the creation of these kinds of program. This is one of the reasons most universities do not teach COBOL. Because it cannot be used to create data structures such as linked lists, queues, or stacks or to develop algorithms like Quicksort, some other programming language has to be taught to allow instruction in these computer science concepts. The curriculum is so crowded nowadays that there is often no room to introduce two programming languages, especially when one of them seems to offer little educational benefit.
1
Chapter 1 ■ IntroduCtIon to CoBoL
Although COBOL’s design may preclude it from being used as a general-purpose programming language, it is
well suited for developing long-lived, data-oriented business applications. COBOL’s forte is the processing of data transactions, especially those involving money, and this focus puts it at the heart of the mission-critical systems that run the world. COBOL is found in insurance systems, banking systems, finance systems, stock dealing systems, government systems, military systems, telephony systems, hospital systems, airline systems, traffic systems, and many, many others. It may be only a slight exaggeration to say that the world runs on COBOL.
COBOL’s Fitness for Its Application Domain
What does it mean to say that a language is well suited for developing business applications? What are the requirements of a language working in the business applications domain? In a series of articles on the topic, Professor Robert Glass4-7 concludes that such a programming language should exhibit the following characteristics:
• It should be able to declare and manipulate heterogeneous data. Unlike other application
domains, which mainly manipulate floating-point or integer numbers, business data is a
heterogeneous mix of fixed and variable-length character strings as well as integer, cardinal,
and decimal numbers.
• It should be able to declare and manipulate decimal data as a native data type. In accounting, bank, taxation, and other financial applications, there is a requirement that computed
calculations produce exactly the same result as those produced by manual calculations. The
floating-point calculations commonly used in other application domains often contain minute
rounding errors, which, taken over millions of calculations, give rise to serious accounting
discrepancies.
■ Note the requirement for decimal data, and the problems caused by using floating-point numbers to represent money values, is explored more fully later in this book.
• It should have the capability to conveniently generate reports and create a GUI. Just as
calculating money values correctly is important for a business application, so is outputting
the results in the format normally used for such business output. GUI screens, with their
interactive charts and graphs, although a welcome addition to business applications, have not
entirely eliminated the need for traditional reports
consisting of column headings, columns of
figures, and a hierarchy of subtotals, totals, and final totals.
• It should be able to access and manipulate record-oriented data masses such as files and
databases. An important characteristic of a business application programming language is
that it should have an external, rather than internal, focus. It should concentrate on processing
data held externally in files and databases rather than on manipulating data in memory
through linked lists, trees, stacks, and other sophisticated data structures.
In an analysis of several programming languages with regard to these characteristics, Professor Glass6 finds that COBOL is either strong or adequate in all four of these characteristics, whereas the more fashionable domain-independent languages like Visual Basic, Java, and C++ are not. This finding is hardly a great surprise. With the exception of GUIs and databases, these characteristics were designed into COBOL from the outset.
Advocates of domain-independent languages claim that the inadequacies of such a language for a particular
application domain can be overcome by the use of function or class libraries. This is partly true. But programs written using bolted-on capabilities are never quite as readable, understandable, or maintainable as programs where these capabilities are an intrinsic part of the base language. As an illustration of this, consider the following two programs: one program is written in COBOL (Listing 1-1), and the other is written in Java (Listing 1-2).
2
Chapter 1 ■ IntroduCtIon to CoBoL
Listing 1-1. COBOL Version
IDENTIFICATION DIVISION.
PROGRAM-ID. SalesTax.
WORKING-STORAGE SECTION.
01 beforeTax PIC 999V99 VALUE 123.45.
01 salesTaxRate PIC V999 VALUE .065.
01 afterTax PIC 999.99.
PROCEDURE DIVISION.
Begin.
Michael Coughlan Page 1