Chapter 7. Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
OBJECTIVES
To familiarize you with
The ways in which arithmetic may be performed in COBOL.
The formats and options available with the arithmetic verbs.
THE BASIC ARITHMETIC VERBS
All the basic arithmetic operations of ADD, SUBTRACT, MULTIPLY
, and DIVIDE
require that the fields operated on (1) have numeric PICTURE
clauses and (2) actually have numeric data when the program is executed.
ADD Statement
A simple ADD statement has the following two instruction formats:
Format 1 (ADD ... TO
)
Format 2 (ADD ... GIVING
)
Examples 1–4
ADD DEPOSIT TO BALANCE
ADD 15.80 TO TAX
ADD 40, OVERTIME-HOURS GIVING TOTAL-HOURS
ADD AMT1 AMT2 GIVING TOTAL-AMT
Fields Used in an ADD
As noted, the specified fields or operands that are added should be numeric when used in an ADD
statement. Thus, in Examples 1 through 4, all literals are numeric, and it is assumed that all data-names or identifiers, when specified in the DATA DIVISION
, have numeric PICTURE
clauses.
Tip
DEBUGGING TIP FOR EFFICIENT PROGRAM TESTING
A comma can be used anywhere in an instruction, as in Example 3, as long as at least one space follows it. We recommend that you omit commas, however, because they are added characters that can cause errors. You should separate entries instead by placing ...
Get COBOL for the 21st Century now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.