COBOL programming language

Introduction to COBOL
COBOL (an
acronym for common business-oriented language) is a compiled English-like
computer programming language designed for business use. It is imperative,
procedural and, since 2002, object-oriented. COBOL is primarily used in business,
finance, and administrative systems for companies and governments. COBOL is
still widely used in legacy applications deployed on mainframe computers, such
as large-scale batch and transaction processing jobs. But due to its declining
popularity and the retirement of experienced COBOL programmers, programs are
being migrated to new platforms, rewritten in modern languages or replaced with
software packages. Most programming in COBOL is now purely to maintain existing
applications.

COBOL was
designed in 1959 by CODASYL and was partly based on previous programming
language design work by Grace Hopper, commonly referred to as “the
(grand)mother of COBOL”. It was created as part of a US Department of
Defense effort to create a portable programming language for data processing.
Intended as a stopgap, the Department of Defense promptly forced computer
manufacturers to provide it, resulting in its widespread adoption. It was
standardized in 1968 and has since been revised four times. Expansions include
support for structured and object-oriented programming. The current standard is
ISO/IEC 1989:2014.
COBOL has
an English-like syntax, which was designed to be self-documenting and highly
readable. However, it is verbose and uses over 300 reserved words. In contrast
with modern, succinct syntax like y = x;, COBOL has a more English-like syntax
(in this case, MOVE x TO y). COBOL code is split into four divisions
(identification, environment, data and procedure) containing a rigid hierarchy
of sections, paragraphs and sentences. Lacking a large standard library, the
standard specifies 43 statements, 87 functions and just one class.
Academic
computer scientists were generally uninterested in business applications when
COBOL was created and were not involved in its design; it was (effectively)
designed from the ground up as a computer language for business, with an
emphasis on inputs and outputs, whose only data types were numbers and strings
of text. COBOL has been criticized throughout its life, however, for its verbosity,
design process and poor support for structured programming, which resulted in
monolithic and incomprehensible programs.
History and specification
Background
In the
late 1950s, computer users and manufacturers were becoming concerned about the
rising cost of programming. A 1959 survey had found that in any data processing
installation, the programming cost US$800,000 on average and that translating
programs to run on new hardware would cost $600,000. At a time when new
programming languages were proliferating at an ever-increasing rate, the same
survey suggested that if a common business-oriented language were used,
conversion would be far cheaper and faster.
Caption
Grace
Hopper, the inventor of FLOW-MATIC, a predecessor to COBOL. In April 1959, Mary
K. Hawes called a meeting of representatives from academia, computer users, and
manufacturers at the University of Pennsylvania to organize a formal meeting on
common business languages. Representatives included Grace Hopper, inventor of
the English-like data processing language FLOW-MATIC, Jean Sammet and Saul
Gorn.
The group
asked the Department of Defense (DoD) to sponsor an effort to create a common
business language. The delegation impressed Charles A. Phillips, director of
the Data System Research Staff at the DoD, who thought that they
“thoroughly understood” the DoD’s problems. The DoD operated 225
computers, had a further 175 on order and had spent over $200 million on
implementing programs to run on them. Portable programs would save time, reduce
costs and ease modernization.
Phillips
agreed to sponsor the meeting and tasked the delegation with drafting the
agenda.
Features
Syntax
COBOL has
an English-like syntax, which is used to describe nearly everything in a
program. For example, a condition can be expressed as  x IS GREATER THAN y or more concisely as  x GREATER y 
or  x > y. More complex conditions
can be “abbreviated” by removing repeated conditions and variables.
For example,  a> b AND a > c OR a =
d  can be shortened to a > b AND c OR
= d. As a consequence of this English-like syntax, COBOL has over 300 keywords.
Some of the keywords are simple alternative or pluralized spellings of the same
word, which provides for more English-like statements and clauses; e.g., the IN
and OF keywords can be used interchangeably, as can IS and ARE, and VALUE and
VALUES.
Each
COBOL program is made up of four basic lexical items: words, literals, picture
character-strings (see § PICTURE clause) and separators. Words include reserved
words and user-defined identifiers. They are up to 31 characters long and may
include letters, digits, hyphens and underscores. Literals include numerals
(e.g. 12) and strings (e.g. ‘Hello!’).Separators include the space character
and commas and semi-colons followed by a space.
A COBOL
program is split into four divisions: the identification division, the
environment division, the data division and the procedure division. The
identification division specifies the name and type of the source element and
is where classes and interfaces are specified. The environment division
specifies any program features that depend on the system running it, such as
files and character sets. The data division is used to declare variables and
parameters. The procedure division contains the program’s statements. Each
division is sub-divided into sections, which are made up of paragraphs.
COBOL
2002 also introduced free-format code. Free-format code can be placed in any
column of the file, as in newer programming languages. Comments are specified
using *>, which can be placed anywhere and can also be used in fixed-format
source code. Continuation lines are not present, and the >>PAGE directive
replaces the / indicator.
Identification division
The
identification division identifies the following code entity and contains the
definition of a class or interface.
Object-oriented programming
Classes
and interfaces have been in COBOL since 2002. Classes have factory objects,
containing class methods and variables, and instance objects, containing
instance methods and variables. Inheritance and interfaces provide
polymorphism. Support for generic programming is provided through parameterized
classes, which can be instantiated to use any class or interface. Objects are
stored as references which may be restricted to a certain type. There are two
ways of calling a method: the INVOKE statement, which acts similarly to CALL,
or through inline method invocation, which is analogous to using functions.
*>
These are equivalent.
INVOKE
my-class “foo” RETURNING var
MOVE
my-class::”foo” TO var *> Inline method invocation
COBOL
does not provide a way to hide methods. Class data can be hidden, however, by
declaring it without a PROPERTY clause, which leaves the user with no way to
access it. Method overloading was added in COBOL 2014.
Environment division
The
environment division contains the configuration section and the input-output
section. The configuration section is used to specify variable features such as
currency signs, locales and character sets. The input-output section contains
file-related information.
Files
COBOL
supports three file formats, or organizations: sequential, indexed and
relative. In sequential files, records are contiguous and must be traversed
sequentially, similarly to a linked list. Indexed files have one or more
indexes which allow records to be randomly accessed and which can be sorted on
them. Each record must have a unique key, but other, alternate, record keys
need not be unique. Implementations of indexed files vary between vendors,
although common implementations, such as C
ISAM and VSAM, are based on IBM’s ISAM. Relative files, like indexed
files, have a unique record key, but they do not have alternate keys. A
relative record’s key is its ordinal position; for example, the 10th record has
a key of 10. This means that creating a record with a key of 5 may require the
creation of (empty) preceding records. Relative files also allow for both
sequential and random access.
A common
non-standard extension is the line sequential organization, used to process
text files. Records in a file are terminated by a newline and may be of varying
length.
COBOL DATA TYPES
Data Division is used to define the variables used
in a program. To describe data in COBOL, one must understand the following
terms −
  • Data
    Name
  • Level
    Number
  • Picture
    Clause
  • Value
    Clause
01           
TOTAL-STUDENTS           
PIC9(5)            VALUE ‘125’.
|                   
|                    |                    |
|                   
|                    |                    |
|                    |                    |                    |
Level Number    
Data Name           Picture
Clause       Value Clause
Data
names
Data names must be defined in the
Data Division before using them in the Procedure Division. They must have a
user-defined name; reserved words cannot be used. Data names give reference to
the memory locations where actual data is stored. They can be elementary or
group type.
Examples
The following example shows valid
and invalid data names −
Valid:
WS-NAME
TOTAL-STUDENTS
A100
100B
Invalid:
MOVE           
(Reserved Words)
COMPUTE        
(Reserved Words)
100            
(No Alphabet)
100+B          
(+ is not allowed)
Level
Number
Level number is used to specify
the level of data in a record. They are used to differentiate between
elementary items and group items. Elementary items can be grouped together to
create group items.
Level
Number
Description
01
Record
description entry
02 to
49
Group
and Elementary items
66
Rename
Clause items
77
Items
which cannot be sub-divided
88
Condition
name entry
·       
Elementary items cannot be divided further. Level number, Data
name, Picture clause, and Value clause (optional) are used to describe an
elementary item.
·       
Group items consist of one or more elementary items. Level
number, Data name, and Value clause (optional) are used to describe a group
item. Group level number is always 01.
Example
The following example shows Group
and Elementary items −
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NAME   
PIC X(25).                              
—> ELEMENTARY ITEM
01 WS-CLASS  
PIC 9(2)  VALUE  ’10’.                   —> ELEMENTARY ITEM
01 WS-ADDRESS.                                        
—> GROUP ITEM  
   05
WS-HOUSE-NUMBER    PIC 9(3).                     —> ELEMENTARY ITEM
   05
WS-STREET          PIC X(15).                    —> ELEMENTARY ITEM
   05
WS-CITY            PIC X(15).                    —> ELEMENTARY ITEM
   05
WS-COUNTRY         PIC X(15)  VALUE ‘INDIA’.     —> ELEMENTARY ITEM
Picture Clause
Picture clause is used to define
the following items −
·       
Data type can be numeric, alphabetic, or alphanumeric.
Numeric type consists of only digits 0 to 9. Alphabetic type consists of
letters A to Z and spaces. Alphanumeric type consists of digits, letters, and
special characters.
·       
Sign can be used with numeric data. It can be
either + or –.
·       
Decimal point position can be used with numeric
data. Assumed position is the position of decimal point and not included in the
data.
·       
Length defines the number of bytes used by the data
item.
Symbols used in a Picture clause
Symbol
Description
9
Numeric
A
Alphabetic
X
Alphanumeric
V
Implicit
Decimal
S
Sign
P
Assumed
Decimal
Example
The following example shows the use
of PIC clause −
IDENTIFICATION
DIVISION.
PROGRAM-ID.
HELLO.
DATA
DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-NUM1 PIC S9(3)V9(2).
   01 WS-NUM2 PIC PPP999.
   01 WS-NUM3 PIC S9(3)V9(2) VALUE -123.45.
   01 WS-NAME PIC A(6) VALUE ‘ABCDEF’.
   01 WS-ID PIC X(5) VALUE ‘A121$’.
PROCEDURE
DIVISION.
   DISPLAY “WS-NUM1 : “WS-NUM1.
   DISPLAY “WS-NUM2 : “WS-NUM2.
   DISPLAY “WS-NUM3 : “WS-NUM3.
   DISPLAY “WS-NAME : “WS-NAME.
   DISPLAY “WS-ID : “WS-ID.
STOP RUN.
JCL to execute the above COBOL program
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the
above program, it produces the following result −
WS-NUM1 : +000.00
WS-NUM2 : .000000
WS-NUM3 : -123.45
WS-NAME : ABCDEF
WS-ID : A121$
Value Clause
Value clause is an optional
clause which is used to initialize the data items. The values can be numeric
literal, alphanumeric literal, or figurative constant. It can be used with both
group and elementary items.
Example
The following example shows the
use of VALUE clause −
IDENTIFICATION
DIVISION.
PROGRAM-ID.
HELLO.
DATA
DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-NUM1 PIC 99V9 VALUE IS 3.5.
   01 WS-NAME PIC A(6) VALUE ‘ABCD’.
   01 WS-ID PIC 99 VALUE ZERO.
PROCEDURE
DIVISION.
   DISPLAY “WS-NUM1 : “WS-NUM1.
   DISPLAY “WS-NAME : “WS-NAME.
   DISPLAY “WS-ID   : “WS-ID.
STOP RUN.
JCL to execute the above COBOL
program −
//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
When you compile and execute the
above program, it produces the following result −
WS-NUM1 : 03.5
WS-NAME : ABCD
WS-ID   : 00
COBOL STATEMENT
COBOL
statements and national data
You can
use national data with the PROCEDURE DIVISION and compiler-directing
statements shown in the table below.
COBOL statement
Can be national
Comment
ACCEPT
identifier-1identifier-2
identifier-1 is converted from the code page
indicated by the runtime locale only if input is from the terminal.
ADD
All
identifiers can be numeric items that have USAGE NATIONAL. identifier-3 (GIVING)
can be numeric-edited with USAGE NATIONAL.
CALL
identifier-2identifier-3,identifier-4identifier-5literal-2literal-3
COMPUTE
identifier-1 can be numeric or numeric-edited
with USAGE NATIONAL. arithmetic-expression can contain
numeric items that have USAGE NATIONAL.
COPY .
. . REPLACING
operand-1operand-2 of
the REPLACING phrase
DISPLAY
identifier-1
identifier-1 is converted to the code page associated
with the current locale.
DIVIDE
All
identifiers can be numeric items that have USAGE NATIONAL. identifier-3 (GIVING)
and identifier-4 (REMAINDER) can be numeric-edited
with USAGE NATIONAL.
INITIALIZE
identifier-1identifier-2 or literal-1 of
the REPLACINGphrase
If you
specify REPLACING NATIONAL or REPLACING NATIONAL-EDITED,identifier-2 or literal-1 must
be valid as a sending operand in a move to identifier-1.
INSPECT
All
identifiers and literals. (identifier-2, the TALLYINGinteger data
item, can have USAGE NATIONAL.)
If any
of these (other than identifier-2, the TALLYINGidentifier)
have USAGE NATIONAL, all must be national.
INVOKE
Method-name
as identifier-2or literal-1identifier-3 or literal-2 in
the BY VALUEphrase
MERGE
Merge
keys, if you specify NCOLLSEQ(BIN)
The COLLATING
SEQUENCEphrase does not apply.
MOVE
Both
the sender and receiver, or only the receiver
Implicit
conversions are performed for valid MOVEoperands.
MULTIPLY
All
identifiers can be numeric items that have USAGE NATIONAL. identifier-3 (GIVING)
can be numeric-edited with USAGE NATIONAL.
SEARCH
ALL(binary search)
Both
the key data item and its object of comparison
The key
data item and its object of comparison must be compatible according to the
rules of comparison. If the object of comparison is of class national, the
key must be also.
SORT
Sort
keys, if you specify NCOLLSEQ(BIN)
The COLLATING
SEQUENCEphrase does not apply.
STRING
All
identifiers and literals. (identifier-4, the POINTERinteger data
item, can have USAGE NATIONAL.)
If identifier-3,
the receiving data item, is national, all identifiers and literals (other
than identifier-4, the POINTER identifier) must be
national.
SUBTRACT
All
identifiers can be numeric items that have USAGE NATIONAL. identifier-3 (GIVING)
can be numeric-edited with USAGE NATIONAL.
UNSTRING
All identifiers
and literals. (identifier-6 and identifier-7,
the COUNT and TALLYINGinteger data items, respectively, can
have USAGE NATIONAL.)
If identifier-4,
a receiving data item, has USAGE NATIONAL, the sending data item and
each delimiter must have USAGE NATIONAL, and each literal must be
national.
XML
GENERATE
identifier-1 (the generated XML document); identifier-2(the
source field or fields)
XML
PARSE
identifier-1 (the XML document)
The XML-NTEXT special
register contains national character document fragments during parsing. 
COBOL SAMPLE PROGRAMS
Sample COBOL program
This sample COBOL program is adapted to run under Fujitsu COBOL It uses
the input file SENIOR.DAT the disk that accompanies the text.
000010           IDENTIFICATION DIVISION.
000020           PROGRAM-ID.       SAMPLE.
000030           AUTHOR.           J.P.E. HODGSON.
00004 0 DATE-WRITTEN.     4 February 2000
000041
000042*         A sample program just to show the form.
000043*         The program copies its input to the
output,
000044*         and counts the number of records.
000045*         At the end this number is printed.
000046
000050           ENVIRONMENT DIVISION.
000060           INPUT-OUTPUT SECTION.
000070           FILE-CONTROL.
000080           SELECT
STUDENT-FILE     ASSIGN TO SYSIN
000090       ORGANIZATION IS LINE SEQUENTIAL.
000100           SELECT
PRINT-FILE       ASSIGN TO SYSOUT
000110        ORGANIZATION IS LINE SEQUENTIAL.
000120
000130           DATA DIVISION.
000140           FILE SECTION.
000150           FD 
STUDENT-FILE
000160            RECORD CONTAINS 43 CHARACTERS
000170            DATA RECORD IS STUDENT-IN.
000180 01
     STUDENT-IN              PIC X(43).
000190
000200           FD 
PRINT-FILE
000210            RECORD CONTAINS 80 CHARACTERS
000220           DATA
RECORD IS PRINT-LINE.
000230 01      
PRINT-LINE              PIC X(80).
000240
000250           WORKING-STORAGE SECTION.
000260
01  DATA-REMAINS-SWITCH     PIC X(2)      VALUE SPACES.
000261
01  RECORDS-WRITTEN         PIC 99.
000270
000280
01  DETAIL-LINE.
000290     05 
FILLER              PIC X(7)      VALUE SPACES.
000300     05 
RECORD-IMAGE        PIC X(43).
000310     05 
FILLER              PIC X(30)    
VALUE SPACES.
000311
000312
01  SUMMARY-LINE.
00031305  FILLER              PIC X(7)      VALUE SPACES.
000314     05 
TOTAL-READ          PIC 99.
000315     05 
FILLER              PIC X         VALUE SPACE.
000316     05 
FILLER              PIC X(17)    
000317                 VALUE  ‘Records were read’.
000318     05 
FILLER              PIC X(53)     VALUE SPACES.
000319
000320           PROCEDURE DIVISION.
000321
000330           PREPARE-SENIOR-REPORT.
000340     OPEN INPUT 
STUDENT-FILE
000350          OUTPUT PRINT-FILE.
000351            MOVE ZERO TO RECORDS-WRITTEN.
000360             READ STUDENT-FILE
000370         AT END MOVE ‘NO’ TO
DATA-REMAINS-SWITCH
000380            END-READ.
000390            PERFORM PROCESS-RECORDS
000410        UNTIL DATA-REMAINS-SWITCH = ‘NO’.
000411           PERFORM
PRINT-SUMMARY.
000420           CLOSE
STUDENT-FILE
000430           PRINT-FILE.
000440             STOP RUN.
000450
000460           PROCESS-RECORDS.
000470     MOVE STUDENT-IN TO RECORD-IMAGE.
000480     MOVE DETAIL-LINE TO PRINT-LINE.
000490     WRITE PRINT-LINE.
000500     ADD 1 TO RECORDS-WRITTEN.
000510     READ STUDENT-FILE
000520         AT END MOVE ‘NO’ TO
DATA-REMAINS-SWITCH
000530     END-READ.
000540
000550           PRINT-SUMMARY.
000560     MOVE RECORDS-WRITTEN TO TOTAL-READ.
000570     MOVE SUMMARY-LINE TO PRINT-LINE.
000571     WRITE PRINT-LINE.
000572
000580     
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x