Cobol
()
About this ebook
The book is the first edition of the book. The book is carefully designed to suit both new and experienced programmers who want first hand experience of COBOL programming.
It is designed to make you familiar with structural programming. Structural design is the basis of COBOL programming.
First few chapters will introduce you with the core concepts. Once you become familiar then the book brings you a lots of advanced topics.
The book has up to date topics and latest concepts based on Enterprise COBOL 6.2. The book covers modern topics:
RECURSIVE programming
DLL programming
Programming using Language Environment.
Handling JSON and XML data.
Interlanguage communication with C and Java.
z/OS JSON Parser toolkit for parsing JSON data
XML Toolkit for z/OS toolkit for parsing XML data
JZOS Batch Launcher and Toolkit for running Java program in mainframe batch environment
If you want to know what Enterprise COBOL can offer, then this book is a must read book. The book will open a new door for COBOL programming. Enterprise COBOL can work with same capabilities with other high level languages.
The book has over 200 full examples. All the topics have enough examples to make you familiar with the concept. All the programmes are executed in IBM mainframe. However knowledge learned in this book can be used with other variants of COBOL.
Related to Cobol
Related ebooks
COBOL Language Fundamentals Quick Start Rating: 0 out of 5 stars0 ratingsCOBOL Language Fundamentals with VSAM Quick Start Rating: 0 out of 5 stars0 ratingsCOBOL Rating: 0 out of 5 stars0 ratingsCOBOL Basic Training Using VSAM, IMS and DB2 Rating: 5 out of 5 stars5/5COBOL for the Approved Workman Rating: 0 out of 5 stars0 ratingsCOBOL Language Fundamentals with DB2 Quick Start Rating: 0 out of 5 stars0 ratingsIBM System i APIs at Work Rating: 5 out of 5 stars5/5Mastering IBM i: The Complete Resource for Today's IBM i System Rating: 3 out of 5 stars3/5DB2 11 for z/OS: Basic Training for Application Developers Rating: 5 out of 5 stars5/5Mastering Go Programming: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsPLI Basic Training Using VSAM, IMS and DB2 Rating: 1 out of 5 stars1/5DB2 Exam C2090-313 Preparation Guide Rating: 0 out of 5 stars0 ratingsProgramming in RPG IV Rating: 5 out of 5 stars5/5Mastering C: A Comprehensive Guide to Programming Excellence Rating: 0 out of 5 stars0 ratingsDB2 11 for z/OS: SQL Basic Training for Application Developers Rating: 4 out of 5 stars4/5IMS-DB Basic Training For Application Developers Rating: 0 out of 5 stars0 ratingszOS JCL (Job Control Language) Rating: 0 out of 5 stars0 ratingsMVS JCL Utilities Quick Reference, Third Edition Rating: 5 out of 5 stars5/5COBOL Programming Interview Questions: COBOL Job Interview Preparation Rating: 5 out of 5 stars5/5A Guide to Db2 Performance for Application Developers: Code for Performance from the Beginning Rating: 0 out of 5 stars0 ratingsMainframe Interview Cases Rating: 0 out of 5 stars0 ratingsUNIX Shell Programming Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsMvs Jcl in Plain English Rating: 5 out of 5 stars5/5UNIX Shell Scripting Interview Questions, Answers, and Explanations: UNIX Shell Certification Review Rating: 5 out of 5 stars5/5DB2 Exam C2090-320 Preparation Guide Rating: 0 out of 5 stars0 ratingsSubfiles in Free-Format RPG: Rules, Examples, Techniques, and Other Cool Stuff Rating: 5 out of 5 stars5/5The Programmer's Guide to iSeries Navigator Rating: 0 out of 5 stars0 ratingsDB2 11 for z/OS: Intermediate Training for Application Developers Rating: 0 out of 5 stars0 ratingsInterview Questions for IBM Mainframe Developers Rating: 1 out of 5 stars1/5SQL Built-In Functions and Stored Procedures: The i5/iSeries Programmer's Guide Rating: 0 out of 5 stars0 ratings
Programming For You
Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsJavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5HTML in 30 Pages Rating: 5 out of 5 stars5/5C Programming For Beginners: The Simple Guide to Learning C Programming Language Fast! Rating: 5 out of 5 stars5/5Narrative Design for Indies: Getting Started Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Beginning Programming with C++ For Dummies Rating: 4 out of 5 stars4/5Raspberry Pi Electronics Projects for the Evil Genius Rating: 3 out of 5 stars3/5C All-in-One Desk Reference For Dummies Rating: 5 out of 5 stars5/5Coding with JavaScript For Dummies Rating: 0 out of 5 stars0 ratings
Reviews for Cobol
0 ratings0 reviews
Book preview
Cobol - Sumanta Soren
son.
About the Author
I have been working in IT organization for several years. My foray into programming starts with Pascal, C, and Java. Later when I joined my career in IT I was introduced to mainframe. That was a big leap ahead in my career. All my academic I have coded mainly interactive program whereas mainframe is mainly batch. Initially it was a big challenge to my career whether I can survive. Slowly I gathered my pace around batch programming.
Preface
During my mainframe career I have faced several difficulties. There are lots of books available in the market but all these books mainly describe the theory. There are lots of training center that only provide theoretical class. IT companies to cut cost they only provide online training without any hands on.
In this book my main aim is to fill up that gaps. If you are already familiar with the COBOL than you will find this book very useful. Even if you are new to COBOL, there are plenty of theory to boost your knowledge.
In this book I have designed each chapter the way real application works. In first few chapters there are only theoretical concept to make you familiar with COBOL. Later I have introduced the real practical example to each chapter. These example not only give you practical knowledge at the same time give you feel of the real application.
All the examples in this book are very simple and easy to follow. Each example has description about how to code, and explanation of input and how to execute it.
About the Book
1. All the programs are written based on Enterprise COBOL 6.2.
2. All programs are executed in mainframe.
3. All programs except CICS programs are batch programs.
4. Covered vast range of topics so that you do not have to follow multiple books. Once you are become familiar with the topics you can follow more advanced books.
5. All the programs are from my lengthy programming experience.
6. Although program looks lengthy they are simple.
7. Some of the programs look same. Two things to note here, if you can understand one thing, you can easily follow the next topic since both are similar.
8. Programs mainly focused on read and write. My goal is to give you the data you need then apply your business logic and process the data.
9. Most programs have add, update and delete operations together. All the operations are similar and these ways to reduce the number of programs.
10. Program does not have enough error handling.
11. All the programs have been tested for one set of input.
12. All the programs have associated with input and output data. In the examples data have been changed to make it more readable. For example binary and packed decimal data have been presented in decimal form. If you see different out, this is just for above reasons.
13. Some programs might have dead codes, meaning codes that never executed. I might have missed them.
14. All the statements are described with full options. In practice, hardly all of them are used. Optional parameter is coded in square bracket.
15. The book covers vast range of topics but not all chapters are complete. I assume you have some knowledge of C and Java programming.
16. By convention, I have used PDSE as load library.
Contents
Table of Contents
About the Author
Preface
About the Book
Contents
1 Introduction to Mainframe and COBOL
Evolution of mainframe
Features of Mainframe
Reason of Popularity
Security
Reliability
Performance
COBOL
Development of COBOL
Structure of COBOL
2 Data types: Numeric and Characters
Variable or Data name
LEVEL number
Variables or data names
Data Types
Literals
Figurative Constants
Initializing variables
SET statement
POINTER variable
INITIALIZE statement
JUSTIFIED RIGHT clause
USAGE clause
SIGN clause
Sign Character in packed decimal
SYNCHRONIZED clause
BLANK WHEN ZERO clause
Data format conversion
3 Operations: Arithmetic, Conditional, Character
Arithmetic Operations
Numeric Statements
Numeric Operators
Numeric class test
Sign test
NOT keyword
ADD statement
SUBTRACTION statement
MULTIPLY statement
DIVIDE statement
COMPUTE statement
Numeric Intrinsic Functions
Conditional Operations
Operators
IF conditional statement
Null Statement
EVALUATE statement
Defining a switch
Defining a flag
GO TO statement
Character Operations
Class test
STRING statement
UNSTRING statement
EXAMINE statement
INSPECT statement
MOVE statement
DISPLAY statement
FILLER clause
Substring
Intrinsic functions
Special register
LENGTH OF Special Register
4 Tables
Table
Subscript
Index
Difference between subscript and index
OCCURS clause
Format 1
Format 2
OCCURS DEPENDING clause
Initializing a table
Loading a table
Searching a table
SEARCH statement
SEARCH ALL statement
Sorting a table
Processing table
PERFORM statement
Examples
5 Modules: Copy Member and Sub Programs
COPY statement
CALL statement
Types of CALL
Static Call
Dynamic Call
Difference between static call and dynamic call
Parameter Passing
CALL BY REFERENCE
CALL BY CONTENT
CALL BY VALUE
Difference between CALL BY CONTENT and CALL BY VALUE
RETURNING phrase
Program states
INITIAL clause
CANCEL statement
COMMON clause
RECURSIVE clause
Data name scope
GLOBAL clause
EXTERNAL clause
Attributes of subprograms
Subprogram design considerations
6 Preparation: Program Execution and Best Practices
JCL
Syntax
JCL code sheet
Preparing a Program
Compile JCL
RUN JCL
Logical record length and block size
Parameter passing
Severity codes for compiler diagnostic messages
Compiler Options
Compile Options
Conflicting compiler options
Storage and addressability
Design a Program
Best Practices
Practice Examples
Practice Example 1
7 COBOL Playground
Practice Examples
Practice Example 1
Practice Example 2
Practice Example 3
Practice Example 4
Practice Example 5
Practice Example 6
Practice Example 7
Practice Example 8
Practice Example 9
Practice Example 10
Practice Example 11
Practice Example 12
Practice Example 13
Practice Example 14
Practice Example 15
Practice Example 16
Practice Example 17
Practice Example 18
Practice Example 19
Practice Example 20
Practice Example 21
Practice Example 22
Practice Example 23
Practice Example 24
Practice Example 25
Practice Example 26
Practice Example 27
Practice Example 28
Practice Example 29
Practice Example 30
Practice Example 31
Practice Example 32
Practice Example 33
Practice Example 34
Practice Example 35
Practice Example 36
Practice Example 37
Practice Example 38
Practice Example 39
Practice Example 40
Practice Example 41
Practice Example 42
Practice Example 43
8 File Access: Sequential File
Mainframe Files
Record structure
File Processing
File Declaration
File Definition
File structure
Performing file i-o operations
File Status
Variable File
Format 1
Format 2
Practice Examples
Practice Example 1
Practice Example 2
Practice Example 3
Practice Example 4
Practice Example 5
9 File Access: VSAM File
VSAM
Types of VSAM Files
ESDS
KSDS
RRDS
LDS
Summary of access methods and allowed operations
Defining VSAM Files
IDCAMS utility
COBOL syntax for VSAM files
ESDS
KSDS
RRDS
Performing file i-o operations
OPEN statement
CLOSE statement
File Operations
START statement
READ statement
READ NEXT statement
WRITE statement
REWRITE statement
INVALID KEY phrases
Statements to update a VSAM record
File Status
Practice Examples
Practice Example 1
Practice Example 2
Practice Example 3
Practice Example 4
Practice Example 5
Practice Example 6
Practice Example 7
Practice Example 8
Practice Example 9
Practice Example 10
Practice Example 11
Practice Example 12
Practice Example 13
Practice Example 14
10 Sort and Merge
SORT Types
Sorting in COBOL
Defining SORT
Describing SORT
Sorting in COBOL
SORT statement
Format 1
Format 2
RELEASE statement
RETURN statement
MERGE statement
Practice Examples
Practice Example 1
Practice Example 2
Practice Example 3
Practice Example 4
Practice Example 5
Practice Example 6
Practice Example 7
Practice Example 8
Practice Example 9
11 Report Writing
Types of Output Files
Extract file
Text file
Delimited file
Report file
Practice Examples
Practice Example 1
Practice Example 2
Practice Example 3
Practice Example 4
Practice Example 5
12 Database Access
Concept of RDBMS
Data types
DB2 data types
Large Objects (LOBs)
NULL value
DEFAULT value
Naming Standards
Data Processing
Where to run SQL
INSERT statement
SELECT statement
UPDATE statement
DELETE statement
WHERE clause
Joining Columns
Joining Rows
COMMIT statement
ROLLBACK statement
SAVEPOINT statement
CURSOR
DCLGEN
SQL Codes
SQLCODE in COBOL output
DSNTIAR: SQLCODE handling
SQLCA
LOCK
Lock Size
Lock State or Lock Mode
ISOLATION
Cursor Stability
Repeatable Read
Read Stability
Uncommitted Read
Column Functions
Scalar Function
Aggregate Function
String unit specifications
Coding DB2 Program
Steps to write and execute a COBOL DB2 Program
DB2 Program Execution Process
PACKAGE
PLAN
COLLECTION
Compile JCL
Precompiler Options
BIND JCL
Run JCL
Create tables for Practice Example
CREATE Tablespace
Account Table
Customer Table
DROP Indexes
DROP Tables
GRANT Access on Tablespace
DROP Tablespace
Practice Examples
Practice Example 1
Practice Example 2
Practice Example 3
13 CICS
Functions of CICS
Control Tables and Control Programs
Common Terms
Terminal
Keyboard
CICS Concepts
Task
Transaction
Multitasking
Multithreading
Re-entrant Program
Quasi re-entrant
CICS Transactions
Coding Technique
Pseudo Conversational Technique
BMS
BMS Terms
Field
MAP
MAPSET
BMS MACRO
Creating MAP
Physical Map
Symbolic Map
Compiling MAP
Coding CICS Commands in COBOL Program
Programming Consideration
Program Preparation
Compile JCL
CICS Compiler Options
CICS Basic Commands
SEND TEXT
SEND CONTROL
SEND PAGE
LOAD command
RELEASE command
SEND MAP command
RECEIVE MAP
RETURN
CICS Copybooks
DFHAID
DFHEIBLK
DFHCOMMAREA
DFHATTR
Calling Subprogram
COBOL CALL
LINK
XCTL
Program Hierarchy
Passing Data
File Handling Commands
Direct Access
Sequential Access
Execution Steps of a CICS program
Practice Examples
Practice Example 1
Practice Example 2
EMPLOOYEE File
Practice Example 3
Practice Example 4
Practice Example 5
Practice Example 6
14 Application Development Cycle
Category of Source elements
Roles and Responsibilities
Documentations
Projects
Development Steps
Environment
Migration Path
Version Control or Change Control
Retrofit
Batch and Online
Testing
Testing Artifacts
Testing Methods
15 Debugging
Causes of Abend
Using DISPLAY statement
Using Compiler Options
Using Language Environment runtime options
Familiar with the dump
Using Language Environment Callable Service
Using TEST compiler option
Using OPT compiler option
Using Language Environment TERMTHDACT runtime option
Dump DD statements
Practice Examples
Practice Example 1
Practice Example 2
Practice Example 3
16 Language Environment
Usage
Compiler option
Language Environment specific data set
LE Program Management Model
Common Services
Storage Management Service
Condition Handling
Message Handling Service
Date and Time Service
Locale callable service
General callable service
Math Service
Practice Example
Practice Example 1
Practice Example 2
Practice Example 3
Practice Example 4
Practice Example 5
Practice Example 6
Practice Example 7
Practice Example 8
Practice Example 9
Practice Example 10
17 Communication with Java and JZOS
Java
Sample Java
Data types
Array
String
Method
Calling a method
Constructors
class and object
Communicating with Java methods
REPOSITORY paragraph
OBJECT REFERENCE phrase
Invoking a method
Java Native Interface (JNI)
Local and global references
Deleting, saving, and freeing local references
JNI services for local and global references
Creating instances of classes
Sharing data with Java
Interoperable data types for COBOL and Java
Miscellaneous argument types for COBOL and Java
Handling Java array and String
Preparing a COBOL client
compiler options
Linking OO Application
Invoking Java from a batch COBOL program
JZOS
JZOS Capabilities
Dataset IO
Accessing the z/OS data set
Referring z/OS data set
Access file using BPXWDYN callable service
Library functions used in examples
Position parameters used in examples
I-O mode
Data type comparison between COBOL and JZOS
Practice Examples
Practice Example 1
Practice Example 2
Practice Example 3
Practice Example 4
Practice Example 5
Practice Example 6
Practice Example 7
JZOS Batch Launcher
Practice Example 8
Practice Example 9
Practice Example 10
Practice Example 11
Practice Example 12
Practice Example 13
Practice Example 14
Practice Example 15
Practice Example 16
Practice Example 17
Practice Example 18
Practice Example 19
Practice Example 20
Practice Example 21
Practice Example 22
Practice Example 23
18 DLL
Advantages of DLL
Compiling DLL
Compile options for your programs
Linking DLL
Binder options for DLL applications
Invoking DLL
CALLINTERFACE directive
Calling DLL from non-DLLs
Pointers in DLL
Practice Examples
Practice Example 1
Practice Example 2
Practice Example 3
Practice Example 4
Practice Example 5
19 Multithreading
Compiling Options
LOCAL-STORAGE Section
Concepts
Process
Run unit
Thread
Program invocation
Program termination
Coding in multi thread environment
Serialization techniques
Limitations in COBOL
20 JSON and z/OS® JSON Parser
JSON
JSON data types
JSON Schema
Processing JSON input
JSON PARSE statement
JSON-CODE special register
Reason codes for JSON exception conditions
JSON-STATUS special register
Reason codes for JSON nonexception conditions
Parsing JSON names that are not valid COBOL data names
Producing JSON output
JSON GENERATE statement
Practice Examples
Practice Example 1
Practice Example 2
Practice Example 3
Practice Example 4
Practice Example 5
Practice Example 6
Practice Example 7
Practice Example 8
Practice Example 9
Practice Example 10
Practice Example 11
Practice Example 12
Practice Example 13
Practice Example 14
z/OS® JSON Parser
Practice Example 15
Practice Example 16
Practice Example 17
Practice Example 18
21 XML and XML Toolkit for z/OS®
XML
XML tree
Entity References
XML Element
XML Attributes
XML Namespaces
XML schema
Processing XML input
XML PARSE statement
Compiler options
Link-edit consideration
XML Special Registers
XML-CODE special register
Decoding XML-CODE
XML-EVENT special register
XML-INFORMATION special register
XML-NAMESPACE special register
XML-NNAMESPACE special register
XML-NAMESPACE-PREFIX special register
XML-NNAMESPACE-PREFIX special register
XML-NTEXT special register
XML-TEXT special register
XML documents
Preparing XML schema
Encoding XML documents
Handling XML split
Terminating XML parsing
Exceptions Handling
Producing XML output
XML GENERATE statement
Encoding the XML output
Exceptions handling
Practice Examples
Practice Example 1
Practice Example 2
Practice Example 3
Practice Example 4
Practice Example 5
Practice Example 6
Practice Example 7
Practice Example 8
Practice Example 9
Practice Example 10
Validating XML with schema
Practice Example 11
Practice Example 12
Practice Example 13
Practice Example 14
Practice Example 15
Practice Example 16
Practice Example 17
Practice Example 18
Practice Example 19
XML Toolkit for z/OS®
Practice Example 20
Practice Example 21
22 Interlanguage Communication with C
C
Sample program
Data types
Scope terminator
Comment line
Functions
Calling function
Comparison of COBOL and C/C++ data types
Passing data without pragma
Supported data types under pass by value (direct)
Supported data types under pass by indirect (indirect)
Passing data with pragma
Supported data types for pass by value
Supported data types for pass by reference
Passing strings
Handling function return from COBOL
Handling function return from C
Referring z/OS data set
Library function used in examples
Position parameter for flocate()
I-O mode
Practice Examples
Practice Example 1
Practice Example 2
Practice Example 3
Practice Example 4
Practice Example 5
Practice Example 6
Practice Example 7
Practice Example 8
Practice Example 9
Practice Example 10
Practice Example 11
Practice Example 12
Practice Example 13
Practice Example 14
Practice Example 15
Practice Example 16
Practice Example 17
Practice Example 18
Practice Example 19
Practice Example 20
Practice Example 21
Practice Example 22
Practice Example 23
RRDS Record Structure
Practice Example 24
Practice Example 25
Practice Example 26
Practice Example 27
Practice Example 28
Practice Example 29
I Auxiliary Codes
Program: TRAVJSON
Program: TRAVOBJ
Program: TRAVARR
Program: DISPVAL
Program: PRETTY
II Character Encoding
A bit, A byte and a word
Characters Formats
ASCII and EBCDIC Characters Set
III UNIX: Some Useful Commands
IV BPXBATCH Utility
BPXBATCH
Set the variables in JCL
1 Introduction to Mainframe and COBOL
Mainframe computers or simply mainframes are computers that are huge in size and can process a bulk amount of data in no time. It evolved around middle of twentieth century. Early days mainframe was the only computer that was available. It was developed with a goal to process bulk amount of business and scientific data. Over the years mainframe computers have evolved several times. What we know see almost in size of 240 litters refrigerator.
We do not have a direct access to mainframe. Mainframe is a server. There are mainframe terminals available to access it. Most commonly terminal is named 3270. But there are emulators available in market through which you can connect to mainframe from your desktop without a need of terminal.
There are several companies that build mainframe computers. Most famous of them are IBM, Amdahl, Hitachi. Each companies have their own operating system and manufacture different mainframe typed computer. But in this book I will talk about only IBM manufactured mainframe. More specifically only IBM manufactured (huge) computers are called mainframe. Each companies manufacture mainframe computers have different name. It is right to say other companies slowly vanished from mainframe market. Perhaps IBM is the only company that manufacture mainframe typed computer or mainframe. IBM’s mainframe is secure, reliable, scalable and score 100 percent in terms of availability.
There are several components attached to it. Initial days all the components were attached to a frame thus the name mainframe.
Evolution of mainframe
All the big companies and some governments use mainframe. Although you will never realize it. All major banks, ATMs, telephones, healthcare, insurance, electric and power companies, airplane ticket reservation systems, rail ticket reservation systems, supermarket and auto manufacturing companies use it.
Features of Mainframe
Reason of Popularity
There are several reasons behind the huge popularity of mainframe. Apart from the fact there is huge chunk of code that are there. Everyday thousands of new development take place in mainframe. There are no existing systems that have the same capacity and capabilities of mainframe currently exist.
Security
The world’s most trusted servers offer 100% encryption to protect your data from internal and external threats.
Reliability
IBM mainframes deliver record uptime and 99.999% availability.
Performance
Process billions of transactions on a single server with speeds 2.8x faster than competitors.
COBOL
COBOL is an acronym COmmon Business Oriented Language is an English language like programming language. COBOL was designed in 1959 by CODASYL a consortium for developing a computer programming language. Much of COBOL was developed from the idea of Grace Hopper. Hopper was an US marshal navy before she worked as computer scientist. Hopper’s believed that programs should be written in a language that was close English. Academic scientists were not very impressed at the time of its development because of its verbose nature rather than numbers. But COBOL developed for business soon become only language for industry.
Grace Hopper(Dec 9, 1906 – Jan 1, 1992) was an American scientist. She was the pioneer of the development of machine independent programming language. This led the development of COBOL. An English like programming language for business. She was also a part of a team that developed UNIVAC.
COBOL is batch only. That means it support file mode input and output functionality. However COBOL with CICS you can write user interactive program. COBOL does not have user defined function or procedure. All the functions are intrinsic. However you can write subprograms that can be called by other program written by any user.
As popularity emerges lots of companies started offering their own version of COBOL. And ever growing competitions from high level language COBOL also has seen lots of improvements over the years. Lots of variety of COBOL have emerged from non IBM institutions. Now a days you can write and run a COBOL program in Windows.
Since inception many company apart from CODASYL started developing their own version of COBOL. None of these were compatible to each other. In late 1962 both ISO and American national standards institute (ANSI) formed a group to create a standards. Together they first published the standard version of COBOL in 1968.
Development of COBOL
IBM has released COBOL, COBOL II and Enterprise COBOL. Enterprise COBOL supports JSON and XML data, Object Oriented features. All these compilers run on z/OS. Microfocus has variation of COBOL called MicroFocus COBOL or MFCOBOL, Visual COBOL. All of Microfocus compilers run on Windows and Unix. Fujitsu has its variation of COBOL called Fujitsu COBOL and netCOBOL run only on Fujitsu systems.
Structure of COBOL
A COBOL program has English like structure. It has verbs. Verbs form sentences. Sentences form paragraphs. Paragraphs form sections. Sections form divisions. And divisions form program. Each sentence, paragraph, section and division are terminated by full stop.
COBOL sentence starts at column 8. Column 1-6 are used for sequence number. Now a days sequence numbers are no longer used. You may find lots of them in old code though. Column 7 is used for marking. If column 7 is marked with a * [asterisk] entire line will become a comment line. There is no syntax for block commenting or multi lines commenting in COBOL. If you want to comment multiple lines you have to mark column 7 of each line with an asterisk [*].
A ‘hello world’ program in COBOL.
COBOL has four divisions and eight sections. Only two divisions IDENTIFICATION and PROCEDURE divisions are mandatory. I will explain each of these divisions and sections in more details in following sections.
A COBOL program has a name by which it is identified and it must have an indicator that indicates the end of program. During execution when control reaches the end of program marker, control is returned to the higher level. A higher level can be another COBOL program or operating system.
Once a COBOL program is developed it is compiled and linked to create a load module. Load module is stored in load library. Load module is executed by JCL. This is a standard execution process in mainframe environment. I have used PDSE library to store the load module.
COBOL coding sheet
Traditionally COBOL programs were written on coding forms and then punched on to punch cards. This form has particular format for computer to understand the syntax. Although nowadays programs were directly entered into computer. But this practice of formatting is still followed across the COBOL world.
Division
COBOL has four divisions.
IDENTIFICATION DIVISION
ENVIRONMENT DIVISION
DATA DIVISION
PROCEDURE DIVISION
Only IDENTIFICATION and PROCEDURE divisions are mandatory.
Section
COBOL divisions are further divided by SECTION. There are seven sections in COBOL. SECTIONs are optional but if appear they appear in their respective locations.
IDENTIFICATION DIVISION.
PROGRAM-ID.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INOUT-OUTPUT SECTION.
DATA DIVISION.
FILE SECTION.
WORKING STORAGE SECTION.
REPORT SECTION.
LOCAL-STORAGE SECTION.
LINKAGE SECTION.
PROCEDURE DIVISION.
You can write your own SECTION in procedure division. SECTION is not always a preferable way of coding. SECTION in PROCEDURE division makes program structural.
Paragraph
SECTION is further divided into paragraph. There are many paragraph in each section and division. Paragraphs are optional but if appear they appear in their respective locations.
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INOUT-OUTPUT SECTION.
DATA DIVISION.
FILE SECTION.
WORKING STORAGE SECTION.
REPORT SECTION.
LOCAL-STORAGE SECTION.
LINKAGE SECTION.
PROCEDURE DIVISION.
PROGRAM-ID and FILE-CONTROL are paragraph name.
You can write your own paragraph in PROCEDURE division. Paragraph are preferable way to make your code structural.
Sections and paragraphs are further subdivided into sentences, statements, and entries.
Sentences are subdivided into statements, and statements are subdivided into phrases. Entries are subdivided into clauses.
IDENTIFICATION Division
This division is mandatory. This division has PROGRAM-ID that uniquely identify a program.
PROGRAM-ID is where you specify program name. Other programs use this name to call your program. Length can be up to 30 characters and includes alphabetic characters, digits 0 to 9 and hyphen. But limit your program name to 8 characters only in accordance of PDS member name. At least one characters must be alphabetic.
AUTHOR specifies the developer of the program.
INSTALLATION specifies the name and location of developing center.
DATE-WRITTEN species specific date program was written.
DATE-COMPILED specifies specific date program was first compiled. This date appear in source listing.
SECURITY specifies confidentiality level.
Example
ENVIRONMENT Division
This division is optional. It describes the computer environment on which program is compiled and executed. It has two sections CONFIGURATION SECTION and INPUT-OUTPUT SECTION.
You specify the computing environment in CONFIGURATION SECTION.
You specify file name and its characteristic and access mode in INPUT-OUTPUT SECTION.
CONFIGURATION Section
SOURCE-COMPUTER
computer-name is system name.
WITH DEBUGGING MODE activates the debugging statement written in the program. You can write statement that is specific for debugging. Code character D in column 7 for debugging statement.
OBJECT-COMPUTER-NAME specify object computer name.
SPECIAL-NAMES
Relates IBM-specified environment-names to user-defined mnemonic-names.
Relates alphabet-names to character sets or collating sequences.
Specifies symbolic characters.
Specifies one or more currency sign values and defines a picture symbol to represent each currency sign value in PICTURE clauses.
Specifies that the functions of the comma and decimal point are to be interchanged in PICTURE clauses and numeric literals.
Example
INPUT-OUTPUT Section
INPUT-OUTPUT section is further discussed in chapter 8 and 9.
Example
DATA Division
This division is also optional. It has four sections: FILE SECTION, WORKING STORAGE SECTION, LOCAL-STORAGE SECTION and LINKAGE SECTION.
FILE SECTION describes the layout of file structure.
FILE section is discussed further in following chapters.
Example
WORKING-STORAGE SECTION describes temporary or intermediate variables. WORKING-STORAGE SECTION is allocated at the start of program execution and deallocated at the termination of program. All the variables are initialized to their respective value if VALUE clause is specified and persist still the program terminates.
If program is defined with INITIAL clause, each time it is called, WORKING-STORAGE SECTION is initialized.
If program has no INITIAL clause, first time it is in initial state, subsequent call it retains its value till the duration of its execution.
Example
LOCAL-STORAGE SECTION defines temporary or intermediate variables. LOCAL-STORAGE SECTION is allocated each time CALL statement is executed and free the memory on return from subprogram. LOCAL-STORAGE section is mandatory for recursive programming.
Example
LINKAGE SECTION is used by subprogram to receive value passed from calling program.
Example
PROCEDURE Division
This division is mandatory. It has one optional REPORT-WRITING SECTION. REPORT-WRITING SECTION is not covered. It requires precompiler. Enterprise COBOL does not have this section.
PROCEDURE DIVISION is where you specify your program logic.
Example
End of Program
There are several ways to indicate end of a program.
END PROGRAMprogram-name
EXIT PROGRAM
STOP RUN
GOBACK
END PROGRAM
Any programs can end with END PROGRAM. After this statement control returns to the operating system. If issued from main program files are closed, if issued from sub program files state remain unchanged.
program-name is the name of the program in PROGRAM-ID.
You must use END PROGRAM when program is contained for both main program and subprogram.
Example
EXIT PROGRAM
EXIT PROGRAM statement indicates end of sub program. After this statement control goes to the calling program. If issued from main program it has no effect, but if it is issued from sub program control returns to the calling program. It retains the state of files.
Example
STOP RUN
Only location STOP RUN is used in main program. It directs the operating system to terminate the execution. After the STOP RUN operating system takes control of the program. So care must be taken when using it. After this statement OS takes over and all files are closed.
Example
GOBACK
Any program can indicate end of program by GOBACK. After the GOBACK, mainframe takes control of the program, and if there is no main program, operating system takes control of the program. If issued from main program all files are closed, if issues from sub program files state remain unchanged. GOBACK is the preferred method of all to indicate end of a program.
Example
Difference between Section and Paragraph
You can write both section and paragraph in procedure division. Both section and paragraph make a program structural. But some prefer section others prefer paragraph.
2 Data types: Numeric and Characters
To write a program you need a storage area. You need memory to store values. You cannot directly refer a storage in any programming language. What you need is a name. A name symbolically refers a memory. During compilation or run time a storage is allocated to the name which can hold a data value.
Variable or Data name
In COBOL, variable declaration consists of 5 different parts.
A level number
Variable or data name
Picture clause
Data types
Value
Example
LEVEL number
LEVEL number plays very important role in data declaration. Level number has special meaning.
Example
Record variable
In the example, EMPLOYEE-RECORD is a record name.
Group variable
In the example, EMPLOYEE-ADDRESS is a group item.
Rename
RENAME clause regroups data items logically and given an alternate name. Level number 66 is reserved for renaming data names.
In the example ELEMENT2, ELEMENT3, ELEMENT4 are logically grouped to a group
named GROUP2.
Elementary data names
Level number 77 is reserved for elementary data name. Level 77 allows you to
declare an elementary variable name.
Conditional data name
Level number 88 is reserved for conditional data names. SET verb sets value in conditional data names.
Difference between level 01 and level 77
REDEFINES clause
REDEFINES clause defines a storage with a new name.
Example
In the example, both JOINING-DATE and JOINING-DATE-X refers to the same memory area and therefore same data.
Conditional items
During coding one may frequently requires to change the flow of program based on certain conditions. In COBOL, conditions are given a name like a flag or switch. Behaviors of conditions, flag and switches are different than other data items. You need to use SET verb to assign and change value of conditional items.
SET statement
SET statement sets value to 88 level data name. SET verb also increments or decrements the value of table index.
Example
Example
Following example shows use of conditional data names.
Variables or data names
Use letters, the digits 0 through 9 and hyphens only.
Don’t start and end with hyphen.
Use maximum of 30 characters.
Use at least one letter in the name.
White space is not allowed in name.
Don’t use same name as COBOLreserved words.
Data Types
Variables
COBOL term for variables is data names. So I have used variables and data names interchangeably. Variables identify memory location during program execution. A variable is assigned a storage either during program compilation or run times. A variable can be alphanumeric, numeric or numeric edited or alphabetic.
Use PICTURE or PIC clause to define the format of data types.
PIC XXX or X(3) denotes the same thing. Similarly PIC 999 or X(9) denotes the same thing.
By default numeric data are right aligned.
By default characters or string data are left aligned.
Alphanumeric or PIC X can store both numeric and alphabetic values.
COBOL does not have constant type
COBOL Data types
Example
Alphabetic
Alphanumeric
Numeric
Numeric edited
Special characters
There are special meaning of some characters in COBOL. Following table shows the meaning:
Literals
A literal is a value which can be assumed from the data types. You do not have to declare a variable for this. There are two types of literals:
Numeric
Alphanumeric
Example
Numeric
Alphanumeric
Here 12345 is numeric literal and ‘EMPLOYEE IS DEFAULT’ is alphanumeric literals.
Figurative Constants
COBOL does not have a constant type. But there are some commonly used constants and literals available as reserved words are called figurative constant: ZERO, SPACE, HIGH-VALUE, LOW-VALUE, QUOTE, NULL and ALL. These constants if used at data declaration provides initial values. If used at PROCEDURE DIVISION to initialize variables.
Example
At DATA Division
At PROCEDURE Division
Initializing variables
There are various ways to initialize the variables:
At declaration with VALUE clause
With MOVE statement
With SET statement
Using INITIALIZE Verb
VALUE clause
VALUE clause assigns value to data items. You can set initial value or default value.
Format 1
Format 2
Format 3
Sets value initial value in USAGE POINTER, USAGE PROCEDURE-POINTER, USAGE FUNCTION-POINTER, or USAGE OBJECT REFERENCE.
Example
Example
Example
SET statement
SET can be used to
set value to data items
increment or decrement of index
set value to pointer variables
Set value to condition names
Format 1
Format 2
Format 3
Format 4
Format 5
Format 6
The current value of the receiving field is replaced by the address value specified by the sending field. At run time, function-pointers and procedure-pointers can reference the address of the primary entry point of a COBOL program, an alternate entry point in a COBOL program, or