Date post: | 12-Nov-2014 |
Category: |
Documents |
Upload: | quasar-chunawalla |
View: | 1,826 times |
Download: | 3 times |
A Quick COBOL Hands on – Quasar Chunawalla 1
A Quick COBOL Hands ON programming guide Do-it-yourself approach
Quasar S. Chunawalla,
Tata Consultancy Services
A Quick COBOL Hands on – Quasar Chunawalla 2
No. Topic Pages
1 IDENTIFICATION and ENVIRONMENT Division 3
2 DATA DIVISION 8
A Quick COBOL Hands on – Quasar Chunawalla 3
1 IDENTIFICATION and ENVIRONMENT DIVISION Introduction
COBOL stands for Common Business Oriented Language. COBOL
Programs are used for commercial data processing. COBOL is
an English-like language. Writing a COBOL program is just
like writing a passage in English.
A COBOL Program consists of 4 main divisions :
IDENTIFICATION DIVISION, ENVIRONMENT DIVISION, DATA
DIVISION and PROCEDURE DIVISION. IDENTIFICATION DIVISION is
used to identify the COBOL Program to the system. It tells
the system, what’s the name of the program, who has written
it, when the COBOL Program was written, when it was
compiled etc. ENVIRONMENT DIVISION tells the system, about
the environment, the computer equipment used to run the
COBOL Program. It gives information about the input and
output file-names used in the COBOL Program. DATA DIVISION
declares any input-output areas and temporary work-areas of
the program. PROCEDURE DIVISION is the starting point of
the COBOL Program, where you will code the real COBOL
instructions.
In COBOL, Divisions are divided into Sections, Sections
into Paragraphs, and paragraphs into sentences. Every
sentence must end with a period. Division names, Section
Names and Paragraph Names must also be followed by a
period. Each sentence contains are series of statements. A
COBOL Statement has a COBOL Verb(operator) followed by 1 or
more operands.
Basic Skeleton of a COBOL Program -
IDENTIFICATION DIVISION.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
COBOL Alignment Rules
Just like paragraphs in English, COBOL Divisions and
Paragraphs must follow strict alignment rules. The way we
indent paragraphs in English, you must indent different
Divisions, sections and paragraphs in a COBOL Program. When
A Quick COBOL Hands on – Quasar Chunawalla 4
you write COBOL on the mainframe terminal, or on a coding
sheet, the lines are divided into 80 columns. The column
8 is called column A. Column 12 is called column B. The
area spanning from columns 8-11 is called Area A. The area
spanning from columns 12-72 is called Area B.
A Quick COBOL Hands on – Quasar Chunawalla 5
All division, section, paragraph names, FD entries and 01-
level entries must start in Area A. This means they must
start from any column 8,9,10 or 11. The rest of the
sentences should start from Area B.
All division and section names must begin on a separate
line. There should be atleast one space between paragraph
name and its sentences.
Column 7 is used as a special indicator. If you want to
write COBOL comments to explain the meaning of a Line of
Code in COBOL, you must put an asterisk (*) in the column
7.
IDENTIFICATION DIVISION
The IDENTIFICATION DIVISION is used to identify the COBOL
Program to the system. It has the following paragraphs :
PROGRAM-ID.
AUTHOR.
INSTALLATION.
DATE-WRITTEN.
DATE-COMPILED.
SECURITY.
The PROGRAM-ID gives the identity or name of the COBOL
Program. The INSTALLATION would be the name of the company.
DATE-WRITTEN gives the date when the COBOL Program is
written. DATE-COMPILED gives the date when the COBOL
Program was compiled. SECURITY gives the level of security.
Remember, that only the PROGRAM-ID paragraph is mandatory,
all the other entries are optional.
A Quick COBOL Hands on – Quasar Chunawalla 6
To properly highlight the COBOL program, you can type HI
COBOL in ISPF.
PROCEDURE DIVISION.
The PROCEDURE DIVISION indicates the entry-point or
starting point of the COBOL Program. This is where the
computer system begins to run(execute the COBOL
instructions). Hence, all your instructions/programming
logic must be written inside this PROCEDURE DIVISION. I
will now show you a simple Hello World Program in COBOL to
illustrate the process of writing a COBOL Program,
compiling it, linking it and running the program.
In the COBOL Program, we want display output, or print some
text. When we want to display output, or print text, we use
the DISPLAY instruction/verb. The syntax of DISPLAY is as
follows :
DISPLAY text
The text must be enclosed in single quotes. For example, to
print Hello World, we must write the instruction :
DISPLAY ‘HELLO WORLD’.
A Quick COBOL Hands on – Quasar Chunawalla 7
The PROCEDURE DIVISION of a COBOL Program can have many
instructions. We can also divide these instructions into
small groups/paragraphs. Thus, the PROCEDURE DIVISION has
the following format -
PROCEDURE DIVISION.
Instruction-1
Instruction-2
Instruction-3
Instruction-4
...
Instruction-N
STOP RUN.
Every COBOL Program has a statement/instruction STOP RUN.
The STOP RUN instruction tells the mainframe system, that
this is the end of the COBOL Program. So, STOP RUN is the
point in the COBOL Program, where program execution
stops/halts – the control exits out of the COBOL Program.
Given below is a simple Program that prints HELLO WORLD to
the Output.
A Quick COBOL Hands on – Quasar Chunawalla 8
Compiling a COBOL Program
COBOL is a high-level language. The Mainframe Computer
System only understands binary Machine Language 0 and 1.
Thus, your COBOL Program has to be translated from COBOL
Language to Binary Machine Language.
So, you give the Program in COBOL Language as Input to the
Translator(Compiler), and you get the Output Program in
Binary Machine Language. The original COBOL Program is
called Source Module and it is present as a dataset(file)
inside a PDS which we refer to as Source Library. The
output of COBOL Comiler – your program in Binary Machine
Language is called OBJECT Module.
Linking a COBOL Program
Linking is the process of tying up/bundling up OBJECT
MODULES together. You see, you Program may sometimes
use(call) your friend’s Program for some task(like finding
Square Root). Thus, your Program and your friend’s Program
need to be Linked(Connected). Combining Object Modules
together, so that they can find each other is called
Linking, and this is performed by the Linker. The Linker
takes OBJECT MODULE(s) as Input, puts them together and
wraps them up as a single package, and gives this gift-
package as the Output. This single package is called LOAD
MODULE, and it is stored as a member of a PDS – we call
LOAD LIBRARY.
Running a COBOL Program
To run a COBOL Program, you need to write a JOB Stream/JCL,
which executes your COBOL Program(by specifying your LOAD
MODULE in the EXEC PGM Job step), and providing the
necessary DD Cards for Input Files, Output Files, Error
Files etc.
A Quick COBOL Hands on – Quasar Chunawalla 9
Sample JCL to compile and link a COBOL Program
A Quick COBOL Hands on – Quasar Chunawalla 10
Sample JCL to Run a COBOL Program
ENVIRONMENT DIVISION
The ENVIRONMENT DIVISION provides information about the
computer equipment/configuration upon which the COBOL
program will run. It also gives the name of the input and
output files used in the program. It assigns the file-names
A Quick COBOL Hands on – Quasar Chunawalla 11
to ddnames(DD Statements in the Run JCL indicate the input
and output datasets).
There are two main sections in the ENVIRONMENT DIVISION –
CONFIGURATION SECTION and INPUT-OUTPUT SECTION.
CONFIGURATION SECTION mainly tells the model of the
computer on which the COBOL Source code and the COBOL
Object code is stored. It has two paragraphs – SOURCE-
COMPUTER and OBJECT-COMPUTER.
ENVIRONMENT DIVISION.
SOURCE-COMPUTER. DELL.
OBJECT-COMPUTER. DELL.
INPUT-OUTPUT SECTION contains the FILE CONTROL paragraph.
The FILE CONTROL paragraph assigns file names to specific
ddnames. Let us see how it is done. The syntax of the
SELECT clause is :
SELECT file-name ASSIGN TO DD-name
Suppose we are reading data in the COBOL program from input
file INPUT-FILE, peforming some processing(operations) on
data and writing the results one-by-one to the output file
OUTPUT-FILE, then we will code as follows :
In the above COBOL program, the DD-Names INDD and OUTDD
will be set to point to actual datasets in the JCL/Batch
A Quick COBOL Hands on – Quasar Chunawalla 12
JOB while running the COBOL Program LOAD MODULE, as pointer
to the actual Input Dataset and Output Dataset.
A Quick COBOL Hands on – Quasar Chunawalla 13
2 DATA DIVISION All application/systems are of 2 types – (1) Online
Systems(Transaction Processing Systems) (ii) Batch
Processing Systems.
When you run a program like CMD Command prompt, you first
type in some input commands, you get some output, then you
type in some more input, you get some output, and so on...
this cycle continues till you get the final results. The
ongoing interactive input-process-output, input-process-
output ... mode of processing is called Online
Processing/Transaction Processing mode. It is called online
processing, because it is an interactive mode of processing
data. Here, you type in the input, you immediate get
output. Response times are very quick.
On the other hand, when the volume of input data is very
huge, and processing can take long time, you will supply
all input data, upfront, at the outset, right at the
beginning of the Program. Then, you start the program, and
wait for the program to process the huge volume of data,
and print outputs. This mode of processing is Batch
Processing Mode.
In Batch processing, the input data to COBOL Programs and
output results of the program will be stored in Input
Dataset and Output Dataset. The Input and Output datasets will be sequential PS files,
containing several records. Every record has a structure or
format. You must specify the input record format and the
output record format. This is done using the FILE SECTION
paragraph of the DATA DIVISION.
FILE SECTION The FILE SECTION of the DATA DIVISION must describe the
record formats corresponding to the input and output files.
For every file, you must include a File descriptor FD
paragraph in the FILE SECTION.
A Quick COBOL Hands on – Quasar Chunawalla 14
Basic Data-types in COBOL
There are three main data-types or classes in COBOL. They
are :
NUMERIC Symbol -> 9
ALPHABETIC Symbol -> A
ALPHANUMERIC Symbol -> X
To store data in storage area, or describe record format,
we must use data-name followed by proper data-type.
The rules for valid data-names are :
1-30 characters
Alphabets, digits and hyphens allowed.
Blanks not allowed
Should not begin or end with hyphen
Should not be a COBOL reserved keyword
Any data-item in the DATA DIVISION looks like this :
01 WS-EMP-NAME PIC X(10) VALUE ‘QUASAR’
Level No. Data-name Data-type and size Initial value
Level nos. are used to specify hierarchy. A file contains
records, records contains fields. 01 level is used for
records and independent items. 02-49 levels are used for
A Quick COBOL Hands on – Quasar Chunawalla 15
fields within the record. 66 is used for rename clause. 77
is used for independent data-item. 88 level is used for
condition names.
PICTURE Clause : It gives the data-type and the size of the
data-item. This specifies, whether, the storage location
stores numbers, alphabets, or alpha-numeric data. It also
shows, how much is the width of the data-type.
Examples
PICTURE 999 -> Stores a 3 digit +ve no.
PICTURE S999 -> Stores a 3 digit +ve/-ve no.
PICTURE XXXX -> Stores a string of 4 characters
PICTURE 99V99 -> Stores a +ve real from 0-99.99
PICTURE S9V9 -> Stores a +ve/-ve real from –9.9 to
+9.9
Shorthand Notation : You can abbreviate 9999 as 9(4), XXXX
as X(4), 999V99 as 9(3)V9(2), S99999 as S9(5) etc.
VALUE Clause : We can assign an initial value to the
elementary data-items/variables by using the VALUE clause.
It is an optional clause.
01 WS-EMP-SALARY PIC 9(4)V99 VALUE 1000.52
01 WS-EMP-JDATE PIC X(10) VALUE ’26-06-09’
Literals and Figurative Constants
Literals and Figurative constants are data-items whose
value remains fixed/constant. It does not change in a
program.
ZERO(S) or ZEROES Its value is 0.
SPACE(S) It is used for whitespaces.
HIGH-VALUE(S) Represents the highest value
LOW-VALUE(S) Represents the lowest value
QUOTE(S) Represents single or double quotes
ALL literal Fill with literal
Example of Figurative Constants
01 GROSS-PAY PIC9(5)V99 VALUE 13.5
MOVE ZEROS TO GROSS-PAY.
Before : 00013|50
After : 00000|00
01 STUDENT-NAME PIC X(10) VALUE ‘MIKE’
A Quick COBOL Hands on – Quasar Chunawalla 16
MOVE ALL ‘-‘ TO STUDENT-NAME
Before : MIKEBBBBBB
After : ----------
Group and Elementary Items
In COBOL, one or more elementary data-items can be grouped
together. For example, we can group EMP-NAME, EMP-SALARY
and EMP-JDATE as EMPLOYEE-RECORD.
In COBOL, we say that a group item is a data item, which
has several low-level(elementary) data-items. A group item
is declared using a level number and a data name. It does
not have a PICTURE clause.
A group-item may also contain other group-items or a
combination of a group-item and an elementary data-item.
So, in COBOL, you can form a hierarchy(tree) of data-items.
Let me give you a simple example.
Let’s say we wanted to store details of an Employee.
Suppose the length of the EMP-DETAILS records is 49, and
the data is alpha-numeric. So we can declare this data-item
as :
01 EMPLOYEE-RECORD PIC X(49).
However, it is observed that every Employee has a name,
address, date-of-joining and salary. So, we can break up
EMPLOYEE-RECORD into EMP-NAME, EMP-DOJ, and EMP-SALARY.
01 EMPLOYEE-RECORD.
02 EMP-NAME PIC X(30).
02 EMP-DOJ PIC X(10).
02 EMP-SALARY S9(5)V99.
However, upon further analysis, it is found that name
consists of first-name, middle-name and then last-name.
Also, EMP-DOJ consists of Date DD, Month MM and year YYYY.
So, we can break up EMP-NAME as :
01 EMPLOYEE-RECORD.
02 EMP-NAME.
03 EMP-FIRST-NAME PIC X(10).
03 EMP-MIDDLE-NAME PIC X(10).
03 EMP-LAST-NAME PIC X(10).
02 EMP-DOJ.
A Quick COBOL Hands on – Quasar Chunawalla 17
03 EMP-DD PIC X(2).
03 EMP-MM PIC X(2).
03 EMP-YY PIC X(2).
02 EMP-SALARY S9(5)V99.
EMPLOYEE-RECORD(01)
|
|
-----------------------------------------------------------
| | |
EMP-NAME(02) EMP-DOJ(02) EMP-SALARY(02)
| |
------------------ ----------------------
| | | | | |
FNAME MNAME LNAME(03) EMP-DD EMP-MM EMP-YYYY
The top level group entry has level no. 01. Then lower
levels data-items have level 02, 03... and so on.
Using these group items and level nos. to form a hierarchy,
is to describe the structure(format) of the record. It
applies the principle of nesting. One elementary or group
item can be nested inside another group-item. Higher the
level no., higher is the depth of the data-item.
Some Important Rules :
1. All the data-items at the same level in the tree, must
bear the same level no. If EMP-NAME and EMP-DOJ have
different level nos., it is not allowed.
2. All 01-LEVEL entries must be started in AREA-A.
3. Only atomic items(leaf nodes) will have a PICTURE
Clause. Higher level data-items never have a PIC Clause.
Consider the tree as a family tree with EMPLOYEE-RECORD as
parent and its children as EMP-NAME, EMP-DOJ and EMP-
SALARY. Those data-items that have no children e.g.
EMP-FIRST-NAME, EMP-MIDDLE-NAME or EMP-SALARY are called
leaf-nodes. Leaf-nodes are always atomic.
More on FILE SECTION
As mentioned before, we must describe the input file record
format and output file record format in the File Descriptor
FD paragraph under the FILE SECTION. Remember, that FD
paragraph is always coded in AREA A.
A Quick COBOL Hands on – Quasar Chunawalla 18
Suppose the file from which we are reading contains
EMPLOYEE Details, with every employee having name, address
and phone. Names consist of firstname, middle name and last
name. Address has a street, city and pin-code. Phone
consists of two phone nos. home-phone and work-phone.
This is how the FD Paragraph looks like :
Note : When you are reading input data from a Sequential PS
File, one-by-one each record will be stored in the Input
Record area i.e. EMPLOYEE-RECORD will store the details of
the first employee, then the next employee and so on...
Since, the values stored in input-output areas(like
EMPLOYEE-RECORD) will be supplied by the input
file/dataset, we should not use VALUE clause to initialize
these fields. VALUE clause cannot be used for any records
described in the FILE SECTION.
WORKING-STORAGE Section of the DATA DIVISION
The WORKING-STORAGE SECTION is used to declare any fields
or records that are not going to be used in input files, or
output files, but they are used to store the intermediate
A Quick COBOL Hands on – Quasar Chunawalla 19
results of processing in a temporary storage area. WORKING
STORAGE SECTION will always follow the FILE SECTION. Here
too, we can describe the records in the same as we do in
the FILE SECTION.
WORKING-STORAGE SECTION can contain group-items and
elementary data-items also, just like the FILE SECTION
Paragraph. An initial value may also be assigned to the
data-items by using the VALUE Clause.
The storage for WORKING-STORAGE items is allocated, when
you start running the COBOL Program.
Given below is a simple program that stores some data in
Working Storage Variables, and then displays them on the
Spool Output SYSOUT Dataset.
A Quick COBOL Hands on – Quasar Chunawalla 20
Upon running the above COBOL Program, we get the following
output in the SYSOUT Dataset.
A Quick COBOL Hands on – Quasar Chunawalla 21
MOVE Verb
Suppose we have two WORKING STORAGE Data-items A and B.
Assume that A is a numeric data-item containing the value
2. B is also a numeric data-item containing the value 3.
What happens when we write the instruction :
MOVE A TO B.
This instruction copies the value of A into B. Note that
the variable B can only hold 1 value at a time. So, the old
data value 3 stores in B, is now over-written by a new
data-value 2.
A is called the sending field, B is called the receiving
field. We can also move literals/figurative constants into
a variable. For example,
MOVE 123 TO A.
However, we cannot just move any value into a destination
variable. There are some rules which apply.
1. MOVE can copy elementary or group data-items.
2. In MOVE Operation, if the source data-item is too small
to fit into the destination data-item fully, the remaining
area is blanked(B) out (if alphanumeric move) or zeroed
out(if numeric move).
3. When moving to a numeric data-item, the contents are
always right-justified, for digits before the decimal
point(to the left), and left-justified for digits after the
decimal point.
4. When moving to an alpha-numeric data-item, the contents
are always left-justified.
5. You can also move one group data-item or record to
another group data-item. These are always treated as alpha-
numeric moves.
A Quick COBOL Hands on – Quasar Chunawalla 22
1) MOVING to an Alpha-numeric item
Let’s take a simple program that’ll help us to understand
the above rules. Suppose we have a EMP-NAME Independent
data-item which is alphanumeric of size 8. It initially
stores the value JAYAPRADA.
Now, we move the literal ‘QUASAR’ to EMP-NAME. Firstly, we
need to understand, that when we move data to a field, this
old contents of the receiving field will be completely
replaced. So, ‘QUASAR’ has a length=6. Thus, EMP-NAME is
large enough to hold the literal ‘QUASAR’. What happens to
the remaining 2 places in EMP-NAME? They will contain
blanks. So, in the output, EMP-NAME is displayed as
QUASARBB
I’ve explicitly written BB to indicated two blanks. Blanks
are simply displayed as whitespace in Output.
Now, when we move the literal ‘CHUNAWALLA’ to EMP-NAME,
‘CHUNAWALLA’ has length 10, so its too large to fit into
A Quick COBOL Hands on – Quasar Chunawalla 23
EMP-NAME. Hence, a small part of ‘CHUNAWALLA’ will be
stripped off, or cut off. Since, EMP-NAME is alpha-numeric,
the contents will be left-justified. Hence, EMP-NAME will
contain CHUNAWAL, and the last two characters LA are cut-
off.
Upon running the above COBOL Program, we get the following
Output.
2) MOVING to a Numeric Item
When the destination is a numeric data-item, we consider it
to be a numeric move. In Numeric moves, the data is aligned
along the decimal point. For the integer part of the
number, the movement is from right to left. For the decimal
portion of the number, the movement is from left to right.
Let me show you a simple example that will be an aid to
understand the above concept with great ease.
A Quick COBOL Hands on – Quasar Chunawalla 24
Upon compiling and running the above COBOL Program LOAD, we
get the following output –
A Quick COBOL Hands on – Quasar Chunawalla 25
In the first MOVE, we store the value 1234.56 in SALARY
Field which 9(4)V9(2). Thus, it fits exactly into SALARY.
In the second MOVE, we store 12345.6 in SALARY. First, the
SALARY Field is zeroed out so it contains 0000|00. Next,
12345 in SALARY, from right-to-left. 1 is truncated, or
stripped off. The 6 is stored in the decimal part from
left-to-right. So, SALARY field contains 2345|60. In the
third move, we store 123.456 in SALARY Field. In this case,
the integer part contains 0123 and the decimal part stores
45, with 6 being cut-off.
Editing Symbols
A COBOL Program takes input data in the form of records,
performs processing or operations on the Input Data
records, to produce resultant Output Records. Very often,
we need to customize or re-format the way, certain fields
are displayed in the Output report.
For example, we may have to add leading blank spaces,
suppress leading zeroes, or trailing zeros to the rhs of
the decimal point, insert $, CR and DB symbols, show sign(+
or -) of the number, display decimal point etc. These are
called as Editing symbols.
Given below is simple COBOL Program, which demonstrates how
to use various Edit symbols.
Note : If the Load Library does not have BLKSIZE as a
A Quick COBOL Hands on – Quasar Chunawalla 26
multiple of Record Length, we get ABEND S U0013.
A Quick COBOL Hands on – Quasar Chunawalla 27
A Quick COBOL Hands on – Quasar Chunawalla 28
Upon running the above COBOL Program LOAD, we get the
following output -
REDEFINES Clause
REDFINES Clause can be used to define two group data-items
which point to the same storage area. You can use both the
data-items in the same COBOL Program.
Suppose, we have a storage area defined in the Working
Storage section as
01 EMPLOYEE-DETAILS.
02 EMP-NAME PIC X(30).
02 EMP-ADDRESS PIC X(26).
02 EMP-CONTACT PIC X(20).
02 EMP-NAME-DETAILS REDEFINES EMP-NAME.
03 EMP-FNAME PIC X(10).
03 EMP-MNAME PIC X(10).
03 EMP-LNAME PIC X(10).
A Quick COBOL Hands on – Quasar Chunawalla 29
02 EMP-ADDRESS-DETAILS REDEFINES EMP-ADDRESS.
03 EMP-STREET PIC X(10).
03 EMP-CITY PIC X(10).
03 EMP-PINCODE PIC X(6).
02 EMP-CONTACT-DETAILS REDEFINES EMP-CONTACT.
03 EMP-PHONE1 PIC 9(10).
03 EMP-PHONE2 PIC 9(10).
Note that :
1) In the above example, EMP-NAME has been redefined as
EMP-NAME-DETAILS, EMP-ADDRESS is redefined as EMP-ADDRESS-
DETAILS and EMP-CONTACT is redefined as
EMP-CONTACT-DETAILS. So, EMP-NAME occupies 30 bytes of
storage area. The same 30-byte storage area is also pointed
to by EMP-NAME-DETAILS, and it provides a breakup into
FNAME, MNAME and LNAME. This is a common and typical use of
REDEFINES clause.
2) One cannot use the REDFINES clause with 01-level(root-
level) data-items in the FILE SECTION. It also should not
be used 66-level(renames clause) and 88-level(condition
names).
3) The data-item which you want to redefine should not have
a OCCURS Clause.
A Quick COBOL Hands on – Quasar Chunawalla 30
Upon running the above COBOL Program LOAD, we get the
following output in the SYSOUT Dataset -
A Quick COBOL Hands on – Quasar Chunawalla 31
RENAMES Clause -
The RENAMES clause is used to regroup data-items. The
RENAMES Clause entry always must have level no. 66. You can
take existing Data-items, and re-group them, and create a
new copy in the memory under a new data-name. While using
the RENAMES clause, one must follow certain rules in COBOL.
They are as follows :
1) RENAMES clause has the following syntax
66 new-data-name RENAMES Old-data-name-1 THRU Old-data-name-2
2) The 66 level RENAMES entry should immediately follow
data-names 1 and 2.
3) The Data-names 1 and 2 should not be 01-Level record
entries.
4) The Data-names 1 and 2 should be contiguous.
A Quick COBOL Hands on – Quasar Chunawalla 32
In the above example, there are two copies of FIELD-B, one
in record-1 and another in record-3. Note that both copies
of FIELD-B will always contain the same value/contents. Any
changes to FIELD-B will affect record-1 as well as record-
3. Note that, unlike REDEFINES clause, RENAMES clause
occupies actual physical storage space. Upon submitting the
above COBOL Program LOAD, we get the following output in
the SYSOUT Dataset.
A Quick COBOL Hands on – Quasar Chunawalla 33
A Quick COBOL Hands on – Quasar Chunawalla 34
3 PROCEDURE DIVISION Arithmetic Operators in COBOL –
As with any computer programming language, COBOL allows you
to perform arithmetic operations on Inputs and store the
Output Results. COBOL has 4 basic Arithmetic Operators.
These are ADD, SUBTRACT, MULTIPLY and DIVIDE. You can even
write complex arithmetic expressions by using the COMPUTE
verb. I have written about the syntax of each operator,
followed by examples and some results.
ADD Operator -
The ADD operator can be used to add two or more quantities.
In the assembly language, we write the instruction ADD A B
to find the sum of A and B. The final sum is stored in A.
On the same lines, the ADD Operator in COBOL has the
following syntax :
Syntax
ADD one-quantity TO second-quantity
The final sum result is stored in the second-variable. For
example, if A=10, and I write
ADD 5 TO A
After performing the above step, the computer adds 5 to the
value of A, so A=15.
Suppose a variable B=5, then I may also write
ADD B TO A
In this case, too, the contents of B is added to A, and the
final sum is stored in A. So, A=15.
A Quick COBOL Hands on – Quasar Chunawalla 35
Example
Result
Syntax
However, at times, we would like the final sum-result of
addition to be stored in a separate variable, preserving
the original values of the Input-operands. So, COBOL gives
you the flexibility to include a GIVING Clause.
ADD Input-Data-1 TO Input-Data-2 GIVING Output-Result-3
For example,
ADD A TO B GIVING C.
In this case, if A=10, and B=5, then C = Sum of A and B =
15. The original values of A and B, remain as it is.
A Quick COBOL Hands on – Quasar Chunawalla 36
Example
Result
Syntax
The TO Verb in ADD Operation is not necessary. It is
sufficient in COBOL, if we write
ADD Input-1 Input-2 Input-3 ... GIVING Output-Result
A Quick COBOL Hands on – Quasar Chunawalla 37
Example
Result
SUBTRACT Operator
When you would like to SUBTRACT one quantity from another,
you use the SUBTRACT Operator in COBOL. The SUBTRACT
Operator has the following general format :
Syntax
SUBTRACT second-number FROM first-number
Here, the difference between the first-number and second-
number is stored in the first-number. If we would like the
difference to be stored in another variable, we can just
add the GIVING Clause :
SUBTRACT second-number FROM first-number GIVING difference
Example
A Quick COBOL Hands on – Quasar Chunawalla 38
Result
MULTIPLY Operator
The MULTIPLY Operator is used to find the product of two or
more quantities. Given below is the general format of the
MULTIPLY Operator :
Syntax
A x B can be done in COBOL as :
MULTIPLY A BY B
Here the product result will be stored in B.
Suppose, we would like to store the final end-product
result in C. Then, in COBOL, we must write the statement :
MULTIPLY A BY B GIVING C
Example
A Quick COBOL Hands on – Quasar Chunawalla 39
Result
Example
Result
A Quick COBOL Hands on – Quasar Chunawalla 40
Example
Result
A Quick COBOL Hands on – Quasar Chunawalla 41
Example
Result
Example
Result
A Quick COBOL Hands on – Quasar Chunawalla 42
COMPUTE Verb
Many-a-times, we would like to evaluate a complex
arithmetic expression. For example, to calculate the simple
interest, we must write, the formula is (P x N x R)/100. In
such cases, the COMPUTE Verb comes in quite handy.
The COMPUTE Verb has the following syntax :
COMPUTE result = <expression>
For example,
COMPUTE RESULT = (A + B * C – D / E)
In a complex arithmetic expression, * and / are given the
highest priority 1. + and – have lesser priority 2. So,
while evaluating a complex expression, we start scanning
the expression from left-to-right. In performing a left-to-
right scan, we must find the operator with the highest
priority, and evaluates it first.
Given below is a sample program that shows how to use the
COMPUTE Verb.
A Quick COBOL Hands on – Quasar Chunawalla 43