COBOL Programming
© Tech Mahindra Limited 2007 Tech Mahindra Limited confidential
Objective
At the end of this session, you will be able to…
Know the history of COBOL
List some characteristics of COBOL
Structure of COBOL program in detail
Coding Rules
Various data items used
Level Numbers
Picture Clauses along with symbols
Data Movement Verbs
Interactive Processing Verbs
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 2
Agenda
History of COBOL
Characteristics of COBOL
Structure of COBOL
Coding Rules
Data items used
Level Numbers
Picture Clauses along with symbols
Data Movement Verbs
Interactive Processing Verbs
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 3
Overview of COBOL
Cobol, abbreviated form of
Common Business Oriented Language
The most widespread programming language for commercial
applications
Best suited for handling large volume of data
Considered a very user-friendly language since it is very much
English like
Developed in 1959 by a board of directorate known as CODASYL
(Conference on Data System Language)
The standard version of Cobol was approved by American National
Standards Institute (ANSI) in 1968.
Thus, the first official standard was known as ANSI-68 Cobol
Cobol compilers are available for most computers
The first Cobol compiler became available in 1960
Latest version available is Cobol-II
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 4
Characteristics of COBOL
Powerful file handling
Processing of voluminous business data
Structured language
Compiler based
Fast execution of programs
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 5
Structure of COBOL programs
Program
DIVISIONS
SECTION(S)
PARAGRAPH(S)
SENTENCE(S)
STATEMENT(S)
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 6
COBOL Coding Rules
Almost all COBOL compilers treat a line of COBOL code as if it
contained two distinct areas. These are known as;
Area A and Area B
When a COBOL compiler recognizes these two areas:
All division, section, paragraph names, FD entries and 01 level
numbers must start in Area A
All other sentences must start in Area B
Area A is four characters wide and is followed by Area B
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 7
The COBOL Metalanguage - Syntax Notation
Words in uppercase are reserved words
• When underlined they must be present when the operation of which
they are a part is used
• When they are not underlined the used for readability only and are
optional. If used they must be spelt correctly
Words in mixed case represent names which will be devised by
the programmer
When material is enclosed in braces { } a choice must be made
from the options within the braces
Material is enclosed in square brackets [ ] indicates that the
material is an option which may be included or omitted as
required
The ellipsis symbol ‘...’ indicates that the preceding syntax
element may be repeated at the programmer’s discretion
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 8
The Four Divisions
DIVISIONS are used to identify the principal
components of the program text
There are four DIVISIONS in all
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
Although some of the divisions may be omitted the
sequence in which the DIVISIONS are specified is fixed
and must follow the pattern shown above
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 9
Functions of the Four Divisions
Like other languages COBOL provides a means for specifying
sequence, selection and iteration constructs
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 10
COBOL Program Text Structure
IDENTIFICATION DIVISION.
Program Details
DATA DIVISION.
Data Descriptions
PROCEDURE DIVISION.
Algorithm Description
NOTE : The keyword DIVISION is followed by ‘full-stop’.
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 11
Identification Division
© Tech Mahindra Limited 2007 Tech Mahindra Limited confidential
IDENTIFICATION DIVISION.
The purpose of the IDENTIFICATION DIVISION is to
provide information about the program to the
programmer and to the compiler
Most of the entries in the IDENTIFICATION DIVISION
are directed at the programmer and are treated by the
compiler as comments
An exception to this is the PROGRAM-ID clause
Every COBOL program must have a PROGRAM-ID
It is used to enable the compiler to identify the program
There are several other informational paragraphs in the
IDENTIFICATION DIVISION but we will ignore them for the
moment.
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 13
The IDENTIFICATION DIVISION Syntax
The IDENTIFICATION DIVISION has the following structure
IDENTIFICATION DIVISION.
PROGRAM-ID. NameOfProgram.
[AUTHOR. YourName.]
[INSTALLATION. [comment-entry] . . .]
[DATE-WRITTEN. [comment-entry] . . .]
[DATE-COMPILED. [comment-entry] . . .]
[SECURITY. [comment-entry] . . .]
The keywords IDENTIFICATION DIVISION represent the
division header and signal the commencement of the
program text.
The paragraph name PROGRAM-ID is a keyword. It must be
specified immediately after the division header.
The program name can be up to 30 characters long.
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 14
Environment Division
15
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited
© Tech Mahindra Limited 2007 Tech Mahindra Limited confidential
The Sections Of The Environment Division
The ENVIRONMENT DIVISION is the only machine-
dependent division of a COBOL program
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. computer-name.
OBJECT-COMPUTER. computer-name.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT file-name-1
ASSIGN TO implementor-name-1.
Entries in this division will depend upon:
• the computer system and
• the specific devices or hardware used in the program*
*Interactive programs that use keyed data as input
and display screen output will not need this division
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 16
Configuration Division
17
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited
© Tech Mahindra Limited 2007 Tech Mahindra Limited confidential
THE CONFIGURATION SECTION
Supplies information about the computer on which the
COBOL program will be compiled and executed
y SOURCE-COMPUTER:
• The computer that will be used for compiling the program
y OBJECT-COMPUTER:
• The computer that will be used for executing or running the
program
• SOURCE- COMPUTER and OBJECT-COMPUTER are coded primarily
as documentation entries
The CONFIGURATION SECTION, if coded, will follow the
ENVIRONMENT DIVISION entry in Area A
Example:
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM390.
OBJECT-COMPUTER. IBM390.
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 18
INPUT-OUTPUT SECTION
The INPUT-OUTPUT SECTION optionally follows the
CONFIGURATION SECTION
y It supplies information concerning the input and output devices
used in the program by means of a FILE-CONTROL paragraph
In the FILE-CONTROL paragraph, a file-name is
designated and assigned to a device for each file used in
the program.
The FILE-CONTROL paragraph consists of SELECT
statements
y …each is coded in Area B followed by a period
A SELECT statement
y …defines a file-name
y …assigns a device name to that file*
*A file is the major collection of data for a given application
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 19
INPUT-OUTPUT SECTION
If the output is printed or saved on disk, an output file
must exist in the ENVIRONMENT DIVISION.
If the output is displayed on a screen, then no file
declaration is necessary in the ENVIRONMENT DIVISION
• When this is the case, the ENVIRONMENT DIVISION may be
entirely omitted
The instruction format for the SELECT statement follows:
SELECT file-name-1
ASSIGN TO implementor-name-1
[ORGANIZATION IS SEQUENTIAL]
The implementor-name is a machine-dependent device
specification that is typically provided by the computer
center
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 20
Data Division
21
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited
© Tech Mahindra Limited 2007 Tech Mahindra Limited confidential
The DATA DIVISION
The DATA DIVISION is used to describe most of the data
that a program processes
The DATA DIVISION is divided into two main sections;
y FILE SECTION.
y WORKING-STORAGE SECTION.
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
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 22
DATA DIVISION Syntax
The DATA DIVISION has the following structure
IDENTIFICATION DIVISION.
PROGRAM-ID. Add-Program.
AUTHOR. ABC.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 N1 PIC 9 VALUE ZEROS.
01 N2 PIC 9 VALUE ZEROS.
01 Result PIC 9 VALUE ZEROS.
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 23
FILE SECTION
Defining a File
The FILE SECTION describes the input and output areas
used in the program
A READ instruction, in the PROCEDURE DIVISION, will
transmit one record of the designated file to this input area
Similarly, an output area is primary storage reserved for a
record to be produced in an output file
Each file is described in the FILE SECTION with an FD
sentence that may consist of a series of clauses
After the clauses are specified, the FD sentence ends with a
period
FD is an abbreviation for File Description
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 24
File Description Entries
An example:
SELECT INVENTORY-FILE
ASSIGN TO DISK1.
SELECT ERROR-LIST
ASSIGN TO PRINTER.
*
DATA DIVISION.
FILE SECTION.
FD INVENTORY-FILE
.
.
FD ERROR-LIST
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 25
LABEL RECORD(S) Clause
Format:
LABEL {RECORD IS}{RECORDS ARE}
{OMITTED}{STANDARD}
This clause specifies whether or not the standard header and
trailer labels should be present in the magnetic-tape files
Interpreting Instruction Formats
y Each set of braces {} denotes that one of the enclosed items is
required.
RECORD CONTAINS Clause --(Optional)
Format:
RECORD CONTAINS integer-1
CHARACTERS
The RECORD CONTAINS clause indicates the size of each
record
A print file, for example, may have the following entry:
RECORD CONTAINS 80 CHARACTERS
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 26
BLOCK CONTAINS Clause - (Optional)
Format
BLOCK CONTAINS integer-1 RECORDS
The BLOCK CONTAINS clause is included in the File
Description entry only for files on magnetic media
y A group of logical records is included within one block to
maximize the efficient use of a disk area
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 27
RECORD description entries
Defining a Record
A record is a unit of information consisting of related data
items within a file
• A file consists of records that all have the same length and
format called fixed-length records
Level Numbers
• After a file is described by an FD, the record description entries
for each record format within the file follow
• The record description specifies the format of a record
• Just as the file-name is specified on the FD level,
a record- name is coded on the 01 level
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 28
RECORD description entries (Contd…)
Level Numbers--Example
y A transaction disk file with only one record format may have
the following entries:
FD TRANSACTION-FILE
LABEL RECORDS ARE OMITTED
RECORD CONTAINS 80 CHARACTERS.
01 TRANSACTION-REC-IN.
{Entries of the record}
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 29
RECORD description entries (Contd…)
Level Numbers
Data is grouped in COBOL using the concept of a level
Records are considered the highest level of data in a file,
and are coded on the 01 level
y A record consists of fields
e.g:
The record description entries following the FD are:
01 EMPLOYEE-REC-IN.
05 NAME-IN.
05 ANNUAL-SALARY-IN.
05 JOB-DESCRIPTION-IN.
All fields on the 05 level are subordinate to, or part of, the
01-level entry
All fields coded on the same level are independent items
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 30
RECORD description entries (Contd…)
Fields are defined as either:
Elementary Item Group Item
A field that is not further A field that is further
subdivided subdivided
must be additionally because it is subdivided,
described with a PICTURE needs no further
clause that indicates the specification and ends with
size and type of the field a period
consists of a level number,
data name followed by a
number of optional clauses
terminated by a period.
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 31
RECORD description entries (Contd…)
PICTURE (PIC) Clauses
Group items are defined by a level number and a name,
which is followed by a period
Elementary items or fields not further subdivided must be
described with a PICTURE (or PIC, for short) clause
FUNCTIONS OF THE PICTURE CLAUSE
1. To specify the type of data contained within an
elementary item.
2. To indicate the size of the field.
Value Clauses
The syntax of the Value clause is
VALUE IS literal.
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 32
The Procedure Division
33
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited
© Tech Mahindra Limited 2007 Tech Mahindra Limited confidential
The PROCEDURE DIVISION
The PROCEDURE DIVISION is where all the data described
in the DATA DIVISION is processed and produced.
It is here that the programmer describes his algorithm
It 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
Paragraph and section names are chosen by the
programmer
The names used should reflect the processing being done in
the paragraph or section
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 34
Sections
A section is a block of code made up of 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
A section name consists of a name devised by the
programmer or defined by the language followed by the
word SECTION followed by a full stop
e.g:
SelectUlsterRecords SECTION.
FILE SECTION.
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 35
Paragraphs
Each section consists of one or more 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
The paragraph name consists of a name devised by the
programmer or defined by the language followed by a full
stop
e.g:
PrintFinalTotals.
PROGRAM-ID.
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 36
Sentences and Statements
A paragraph consists of one or more sentences
A sentence consists of one or more statements and is
terminated by a full stop
e.g:
MOVE .21 TO VatRate
COMPUTE VatAmount = ProductCost * VatRate.
DISPLAY "Enter name " WITH NO ADVANCING.
ACCEPT StudentName.
DISPLAY "Name entered was " StudentName.
A statement consists of a COBOL verb and an operand or
operands.
e.g:
SUBTRACT Tax FROM GrossPay GIVING NetPay.
READ StudentFile
AT END SET EndOfFile TO TRUE
END-READ.
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 37
A Full COBOL program
IDENTIFICATION DIVISION.
PROGRAM-ID. AddProgram.
AUTHOR. ABC.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 N1 PIC 9 VALUE ZEROS.
01 N2 PIC 9 VALUE ZEROS.
01 Result PIC 9 VALUE ZEROS.
PROCEDURE DIVISION.
CalculateResult.
ACCEPT N1.
ACCEPT N2.
MULTIPLY N1 BY N2 GIVING Result.
DISPLAY "Result is = ", Result.
STOP RUN.
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 38
The minimum COBOL program
IDENTIFICATION DIVISION.
PROGRAM-ID. MinimalProgram.
PROCEDURE DIVISION.
DisplayPrompt.
DISPLAY "I did it".
STOP RUN.
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 39
Name Construction
All user defined names, such as data names, paragraph
names, section names and mnemonic names, must adhere
to the following rules;
-1 to 30 characters
-Letters, digits and hyphens only
-Should not begin or end with ‘-’
-No embedded blanks are permitted
-Must contain at least one alphabetic character
-Should not be Cobol reserved word like ADD, MOVE etc..
e.g. TotalSal, Gross-Sal, PrintReportTitle, Customer1-Rec
All data-names should describe the data they contain
All paragraph and section names should describe the
function of the paragraph or section
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 40
Describing DATA
There are basically three kinds of data used in COBOL
programs;
Variables
Literals
Figurative Constants
Unlike other programming languages,
COBOL does not support user defined constants
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 41
Data-Names and Variables
A variable is a named location in memory into which a
program can put data and from which it can retrieve data
A data-name or identifier is the name used to identify the
area of memory reserved for the variable
Variables must be described in terms of their type and size
Every variable used in a COBOL program must have a
description in the DATA DIVISION
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 42
Using Variables
01 StudName PIC X(5) VALUE SPACES.
MOVE “AMOL" TO StudName.
DISPLAY "My name is ", StudName.
StudName
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 43
Using Variables
01 StudName PIC X(5) VALUE SPACES.
MOVE “AMOL" TO StudName.
DISPLAY "My name is ", StudName.
StudName
A M O L
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 44
Using Variables
01 StudName PIC X(5) VALUE SPACES.
MOVE “AMOL" TO StudName.
DISPLAY "My name is ", StudName.
StudName
My name is AMOL
A M O L
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 45
COBOL Data Types
COBOL is not a “typed” language and the distinction
between some of the data types available in the language is
a little blurred.
For the time being we will focus on just two data types,
y numeric
y text or string
Data type is important because it determines the operations
which are valid on the type
COBOL is not as rigorous in the application of typing
rules as other languages.
For example, some COBOL “numeric” data items may, from
time to time, have values which are not “numeric”!
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 46
What are “typed” languages?
In “typed” languages simply specifying the type of a
data item provides quite a lot of information about it
The type usually determines the range of values the data
item can store
y For instance a CARDINAL item can store values between
0..65,535 and an INTEGER between -32,768..32,767
From the type of the item the compiler can establish how
much memory to set aside for storing its values
If the type is “REAL” the number of decimal places is
allowed to vary dynamically with each calculation but the
amount of the memory used to store a real number is fixed
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 47
COBOL data description
Because COBOL is not typed it employs a different
mechanism for describing the characteristics of the
data items in the program
COBOL uses what could be described as a “declaration by
example” strategy
In effect, the programmer provides the system with an
example, or template, or PICTURE of what the data item
looks like
From the “picture” the system derives the information
necessary to allocate it
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 48
COBOL ‘PICTURE’ Clause symbols
To create the required ‘picture’ the programmer uses a set
of symbols
The following symbols are used frequently in picture
clauses:
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 49
COBOL ‘PICTURE’ Clause symbols (Cont’d…)
For Example:
PIC PPP999 means the numeric data is of 3 characters in
size and there are 6 positions after the assumed decimal
point.
Suppose the data in memory is 555, the value will be taken
as .000555
Similarly for 999PP will hold 55500.
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 50
COBOL ‘PICTURE’ Clauses
Some examples
PICTURE 999 a three digit (+ive only) integer
PICTURE S999 a three digit (+ive/-ive) integer
PICTURE XXX a three character text item or string
PICTURE 99V99 a +ive ‘real’ in the range 0 to 99.99
If you wish you can use the abbreviation PIC
Numeric values can have a maximum of
18 (eighteen) digits (i.e. 9’s)
The limit on string values is usually system-dependent
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 51
Abbreviating recurring symbols
Recurring symbols can be specified using a ‘repeat’ factor
inside round brackets
y PIC 9(5) is equivalent to PICTURE 99999
y PIC 9(5)V99 is equivalent to PIC 99999V99
y PICTURE X(9) is equivalent to PIC XXXXXXXXX
y PIC S9(3)V9(3) is equivalent to PIC S999V999
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 52
Declaring DATA in COBOL
In COBOL a variable declaration consists of a line containing
the following items;
• A level number
• A data-name or identifier
• A PICTURE clause
We can give a starting value to variables by means of an
extension to the picture clause called the value clause
DATA DATA DIVISION.
Num1 VatRate
Num1 VatRate
StudentName
StudentName
WORKING-STORAGE SECTION.
01 N1 PIC 99 VALUE ZEROS.
00
00 .10
.10 01 Rate PIC V99 VALUE .10.
01 StudName PIC X(5) VALUE SPACES.
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 53
COBOL Literals.
String/Alphanumeric literals are enclosed in quotes and may
consists of alphanumeric characters
e.g. “Sam Joseph", "-111", "123.55"
Numeric literals may consist of numerals, the decimal point
and the plus or minus sign
Numeric literals are not enclosed in quotes
e.g. 111, 123.55, -234, +2345
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 54
Figurative Constants
COBOL provides its own, special constants called
Figurative Constants
SPACE or SPACES =
ZERO or ZEROS or ZEROS = 0
QUOTE or QUOTES = "
HIGH-VALUE or HIGH-VALUES
HIGH-VALUE HIGH-VALUES = Max Value
LOW-VALUE or LOW-VALUES
LOW-VALUE LOW-VALUES = Min Value
ALL literal = Fill With Literal
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 55
Figurative Constants - Examples
01 MyPay PIC 9(5)V99 VALUE 12.5.
ZERO
MOVE ZEROS TO MyPay.
ZEROES
MyPay
0 0 0 1 2 5 0
×
z
01 StudName PIC X(10) VALUE “AMIT".
MOVE ALL "-" TO StudName.
StudentName
A M I T
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 56
Figurative Constants - Examples
01 MyPay PIC 9(5)V99 VALUE 12.5.
ZERO
MOVE ZEROS TO MyPay.
ZEROES
MyPay
0 0 0 0 0 0 0
×
z
01 StudName PIC X(10) VALUE “AMOL".
MOVE ALL "-" TO StudName.
StudName
- - - - - - - - - -
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 57
Group Items/Records
WORKING-STORAGE SECTION.
01 StudDetails PIC X(26).
StudDetails
A B C D E A B C D E 9 4 1 0 1 8 2 L C 1 1 0 1 1 0 M
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 58
Group Items/Records
WORKING-STORAGE SECTION.
01 StudDetails.
02 SName PIC X(10).
02 SId PIC 9(7).
02 CourseCode PIC X(4).
02 Grant PIC 9(4).
02 Gender PIC X.
StudDetails
A BC D EA B C DE 9 4 1 0 1 8 2 L C 1 1 0 1 1 0 M
SName SId CourseCode Grant Gender
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 59
LEVEL Numbers express DATA hierarchy
In COBOL, level numbers are used to decompose a structure
into it’s constituent parts
In this hierarchical structure the higher the level number, the lower
the item is in the hierarchy
At the lowest level the data is completely atomic
The level numbers 01 through 49 are general level numbers
but there are also special level numbers such as 66, 77 and 88
In a hierarchical data description what is important is the
relationship of the level numbers to one another, not the actual
level numbers used
01 StudDetails. 01 StudDetails.
02 SName. 05 SName.
03 Surname PIC X(8). 10 Surname PIC X(8).
03 Initials PIC XX. 10 Initials PIC XX.
02 SId PIC 9(7). = 05 SId PIC 9(7).
02 CourseCode PIC X(4). 05 CourseCode PIC X(4).
02 Grant PIC 9(4). 05 Grant PIC 9(4).
02 Gender PIC X. 05 Gender PIC X.
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 60
Group and elementary items
In COBOL the term “group item” is used to describe a
data item which has been further subdivided
• A Group item is declared using a level number and a data
name. It cannot have a picture clause
• 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
The term “elementary item” is used to describe data
items which are atomic; that is, not further subdivided
An elementary item declaration, consists of;
• a level number,
• a data name
• picture clause
y An elementary item must have a picture clause
Every group or elementary item declaration must be
followed by a full stop.
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 61
PICTUREs for Group Items
Picture clauses are NOT specified for ‘group’ data
items because the size a group item is the sum of the sizes
of its subordinate, elementary items and its type is always
assumed to be PIC X
The type of a group items is always assumed to be PIC X
because group items may have several different data items
and types subordinate to them
An X picture is the only one which could support such
collections
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 62
Assignment in COBOL
In “strongly typed” languages like Modula-2, Pascal or
ADA the assignment operation is simple because
assignment is only allowed between data items with
compatible types
The simplicity of assignment in these languages is
achieved at the “cost” of having a large number of data
types
In COBOL there are basically only three data types,
• Alphabetic (PIC A)
• Alphanumeric (PIC X)
• Numeric (PIC 9)
But this simplicity is achieved only at the cost of having a
very complex assignment statement
In COBOL assignment is achieved using the MOVE
verb
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 63
COBOL Verbs
A statement in Cobol always begins with a Cobol verb
The various verbs available in Cobol are
• Data Movement verb
• Arithmetic verbs
• Sequence Control verb
• Input and Output verbs
• The MOVE verb
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 64
The MOVE Verb
The MOVE copies data from the source identifier or
literal to one or more destination identifiers
The source and destination identifiers can be group or
elementary data items
When the destination item is alphanumeric or alphabetic
(PIC X or A) data is copied into the destination area from left
to right with space filling or truncation on the right
When data is MOVEd into an item, the contents of the item
are completely replaced
If the source data is too small to fill the destination item
entirely the remaining area is zero or space filled
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 65
MOVEing Data
MOVE “RAYS” TO Surname.
MOVE “RITZHATRICK” TO Surname.
01 Surname PIC X(8).
D O U G H L A N
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 66
MOVEing Data
MOVE “RAYS”
“RAYS” TO Surname.
MOVE “FITZPATRICK” TO Surname.
01 Surname PIC X(8).
R A Y S
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 67
MOVEing Data
MOVE “RYAN” TO Surname.
MOVE “FITZPATRICK”
“FITZPATRICK” TO Surname.
01 Surname PIC X(8).
F I T Z P A T R I C K
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 68
MOVEing to a numeric item
When the destination item is numeric, or edited numeric,
then data is aligned along the decimal point with zero filling
or truncation as necessary
When the decimal point is not explicitly specified in
either the source or destination items, the item is treated as
if it had an assumed decimal point immediately after its
rightmost character
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 69
MOVEing to a numeric item (Contd…)
01 PersonalPay PIC 9(4)V99.
GrossPay
MOVE ZEROS TO PersonalPay. 0 0 0 0 0 0
×
z
GrossPay
MOVE 11.5 TO PersonalPay. 0 0 1 1 5 0
×
z
GrossPay
MOVE 123.789 TO PersonalPay. 0 1 2 3 7 8 9
×
z
GrossPay
MOVE 12345.858 TO PersonalPay. 1 2 3 4 5 8 58
×
z
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 70
Processing data interactively
The two verbs used for interactive processing are
y ACCEPT
y DISPLAY
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 71
The DISPLAY Verb
From time to time it may be useful to display messages
and data values on the screen
A simple DISPLAY statement can be used to achieve this
A single DISPLAY can be used to display several data
items or literals or any combination of these
The WITH NO ADVANCING clause suppresses the carriage
return/line feed
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 72
The ACCEPT verb
01 CurrentDate PIC 9(6).
* YYMMDD
01 DayOfYear PIC 9(5).
* YYDDD
01 Day0fWeek PIC 9.
* D (1=Monday)
01 CurrentTime PIC 9(6).
* HHMMSS
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 73
Run of Accept and Display program
IDENTIFICATION DIVISION.
PROGRAM-ID. AcceptAndDisplay.
AUTHOR. ARYAN RAY.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 StudentDetails.
Enter student details using template below 02 StudentName.
03 Surname PIC X(8).
NNNNNNNNNNSSSSSSSCCCCGGGGS 03 Initials PIC XX.
TOUGHLANMM9476532LM511245M 02 StudentId PIC 9(7).
02 CourseCode PIC X(4).
Name is MM TOUGHLAN 02 Grant PIC 9(4).
Date is 04 04 04 02 Gender PIC X.
The time is 22:23 01 CurrentDate.
02 CurrentYear PIC 99.
02 CurrentMonth PIC 99.
02 CurrentDay PIC 99.
01 CurrentTime.
02 CurrentHour PIC 99.
02 CurrentMinute PIC 99.
02 FILLER PIC 9(4).
PROCEDURE DIVISION.
Begin.
DISPLAY "Enter student details using template below".
DISPLAY "NNNNNNNNNNSSSSSSSCCCCGGGGS ".
ACCEPT StudentDetails.
ACCEPT CurrentDate FROM DATE.
ACCEPT CurrentTime FROM TIME.
DISPLAY "Name is ", Initials SPACE Surname.
DISPLAY "Date is " CurrentDay SPACE CurrentMonth SPACE CurrentYear.
DISPLAY "The time is " CurrentHour ":" CurrentMinute.
STOP RUN.
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 74
Summary
In this session, we looked at:
The history of COBOL
Characteristics of COBOL
The Structure of COBOL program
Coding Rules
Various data items used
Level Numbers
Picture Clauses along with symbols
Data Movement Verbs
Interactive Processing Verbs
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 75
References:
COBOL Programming By Roy & Dastidar
Search engine used - Google
CONFIDENTIAL© Copyright 2007 Tech Mahindra Limited 76