Transcript chapter 7

Structured COBOL
Programming
Nancy Stern
Hofstra University
Robert A. Stern
“Copyright @ 2000 John Wiley & Sons, In. All rights
reserved. Reproduction or translation of this work beyond
that permitted in Section 117 of the 1976 United States
Copyright Act without the express permission of the
copyright owner is unlawful. Request for further information
should be addressed to the permissions Department , John
Wily & Sons, Inc. The purchaser may make back-up copies
for his/her own use only and not for distribution or resale.
The Publisher assumes no responsibility for errors,
omissions, or damages, caused by the use of these programs
or from the use of the information contained herein.”
Nassau Community
College
PowerPoint Presentation:
Richard H. Baum, Ph.D.
DeVry Institute of Technology
9th Edition
CHAPTER 7
COMPUTING IN COBOL: THE
ARITHMETIC VERBS AND
INTRINSIC FUNCTIONS
Structured COBOL Programming, Stern & Stern, 9th Edition
OBJECTIVES
• To Familiarize you with:
1. The ways in which arithmetic may be
performed in COBOL.
2. The formats and options available
with the arithmetic verbs.
Structured COBOL Programming, Stern & Stern, 9th Edition
CONTENTS
• The Basic Arithmetic Verbs
– ADD Statement
– SUBTRACT Statement
– MULTIPLY Statement
– DIVIDE Statement
• Options Available with Arithmetic Verbs
– ROUNDED Option
– ON SIZE ERROR Clause
Structured COBOL Programming, Stern & Stern, 9th Edition
CONTENTS
– NOT ON SIZE ERROR Clause
– Determining the Size of Receiving Fields
• The COMPUTE Statement
– Basic Format
– Order of Evaluation
– Comparing COMPUTE to Arithmetic Verbs
Structured COBOL Programming, Stern & Stern, 9th Edition
CONTENTS
• Use of Signed Numbers in Arithmetic
Operations
– The Use of S in PIC Clauses for Fields That
Can Be Negative
– Rules for Performing Arithmetic with Signed
Numbers
– Entering Signed Numbers
• Improving Program Efficiency with the
USAGE Clause
– Format
Structured COBOL Programming, Stern & Stern, 9th Edition
CONTENTS
– USAGE IS DISPLAY
– USAGE IS PACKED-DECIMAL or
COMPUTATIONAL-3
– USAGE IS COMPUTATIONAL
• Intrinsic Functions (COBOL 85)
– Calendar Functions
– Numerical Analysis Functions
– Trigonometric Functions
– Financial Functions
– Character and String Functions
Structured COBOL Programming, Stern & Stern, 9th Edition
THE BASIC ARITHMETIC
VERBS
• All the basic arithmetic operations of
ADD, SUBTRACT, MULTIPLY, and
DIVIDE require that the fields
operated on:
1. Have numeric PIC clauses
2. Actually have numeric data when
the program is executed.
Structured COBOL Programming, Stern & Stern, 9th Edition
ADD STATEMENT
ADD has two instruction formats:
Format 1: (Add…TO)
ADD {identifier-1} {literal-1}...TO
identifier-2...
Format 2: (ADD…GIVING)
ADD {identifier-1} {literal-1}...TO
{identifier-2}{literal-2} GIVING
identifier-3...
Structured COBOL Programming, Stern & Stern, 9th Edition
ADD STATEMENT
• Fields Used in an ADD
– The fields or operands that are added
should be numeric.
– All literals are numeric.
– All data-names or identifiers, in the
DATA DIVISION, have numeric
PICTURE clauses.
Structured COBOL Programming, Stern & Stern, 9th Edition
DEBUGGING TIP
• A comma can be used anywhere in an
instruction, as long as at least one space
follows it.
– However, this not really recommended since
this adds characters that can cause errors.
• It is preferred that you should separate
entries instead by placing them on
individual lines.
Structured COBOL Programming, Stern & Stern, 9th Edition
ADD STATEMENT
The Resultant Field in an ADD
• The result, or sum, of an ADD operation is
always placed in the last field mentioned.
• The only field that is altered as a result of
the ADD operation is this last field,
– This is the field directly following the word
TO, when using Format 1, or GIVING, when
using Format 2.
Structured COBOL Programming, Stern & Stern, 9th Edition
ADD STATEMENT
The Resultant Field in an ADD
• In all cases, the resultant field must be an
identifier or data-name, not a literal.
– The statement ADD HOURS-WORKED TO 40,
for example, is incorrect.
• When using the TO format in an ADD
statement, all the data-names and literals
are added together, and the result is
placed in the last field.
Structured COBOL Programming, Stern & Stern, 9th Edition
ADD STATEMENT
An example of a proper ADD is:
ADD HOURS-WORKED TO WEEKLY
HOURS
– The fields HOURS-WORKED and
WEEKLY-HOURS are added together,
and the result is placed in the last field
specified--WEEKLY-HOURS.
Structured COBOL Programming, Stern & Stern, 9th Edition
ADD STATEMENT
• When using the GIVING format, all fields
and literals preceding the word GIVING
are added together and the sum is placed
in the field following the word GIVING.
– Thus, when using the GIVING format, the last
data field is not part of the ADD operation.
– Because it is not part of the arithmetic
operation, it can be a report-item with edit
symbols, as seen below:
ADD HOURS-WORKED WEEKLY-HOURS
GIVING TOTAL-HOURS.
Structured COBOL Programming, Stern & Stern, 9th Edition
ADD STATEMENT
Deciding Whether to Use the TO or
GIVING Format
• Use the GIVING format with the ADD
statement when the contents of
operands are to be retained.
• When you will no longer need the
original contents of an operand after
the addition, the TO format may be
used.
Structured COBOL Programming, Stern & Stern, 9th Edition
ADD STATEMENT
RULES FOR INTERPRETING FORMATS
1. Underlined words are required.
2. UPPERCASE words are COBOL
reserved words.
3. The word “identifier” means a field
or record defined in the DATA
DIVISION.
Structured COBOL Programming, Stern & Stern, 9th Edition
ADD STATEMENT
RULES FOR INTERPRETING
FORMATS
4. The braces {} mean that one of
the enclosed words is required.
5. The ellipses or dots (...) indicate
that two or more fields or literals
may be specified.
Structured COBOL Programming, Stern & Stern, 9th Edition
ADD STATEMENT
Adding More Than Two Fields
• You are not restricted to two operands
when using an ADD operation:
ADD AMT1
AMT2
AMT3
GIVING AMT4
• The original contents of AMT4, the
resultant field, are destroyed and have no
effect on the ADD operation.
– The three operands AMT1, AMT2, and AMT3
are unchanged.
Structured COBOL Programming, Stern & Stern, 9th Edition
ADD STATEMENT
Example 8:
ADD AMT1
AMT2
AMT3
TO AMT4
• AMT1, AMT2, and AMT3 are added to the
original contents of AMT4. The result is
placed in AMT4.
– AMT1, AMT2, AMT3 are unchanged just as in
the GIVING option.
Structured COBOL Programming, Stern & Stern, 9th Edition
ADD STATEMENT
• Producing More Than One Sum
– It is also possible to perform several ADD
operations with a single statement, using the
TO format.
• The following is a valid statement:
ADD AMT1 AMT2 TO TOTAL1 TOTAL2
– This results are the same as:
ADD AMT1 AMT2 TO TOTAL1
ADD AMT1 AMT2 TO TOTAL2
Structured COBOL Programming, Stern & Stern, 9th Edition
ADD STATEMENT
• RULES FOR ADDITION
1. All literals and fields that are part
of the addition must be numeric.
– After the word GIVING, the field may
be a report-item.
2. The resultant field, following the
word TO or the word GIVING, must
be a data-name, not a literal.
Structured COBOL Programming, Stern & Stern, 9th Edition
ADD STATEMENT
• RULES FOR ADDITION
3. When using the TO format, the
data-name following the word TO is
the receiving field.
– This receiving field is part of the ADD;
that is, its initial contents are summed
along with the other fields.
– The receiving field must be numeric
when using this format.
Structured COBOL Programming, Stern & Stern, 9th Edition
ADD STATEMENT
• RULES FOR ADDITION
4. When using the GIVING format, the
data-name following the word GIVING is
the receiving field.
– It will contain the sum, but its original
contents will not be part of the ADD.
– It may be either a numeric field or a reportitem.
5. The words TO and GIVING may be
specified in the same statement if you are
using a COBOL 85 compiler.
Structured COBOL Programming, Stern & Stern, 9th Edition
QUESTIONS?
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
Indicate the errors, if any, in
Statements 1 and 2
1. ADD '12' TO TOTAL
SOLUTION: ‘12’ is not a numeric literal
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
2. ADD TAX TO TOTAL GIVING AMT
SOLUTION:-- For COBOL 85, the
statement is okay; for COBOL 74, the
words TO and GIVING may not
appear in the same ADD statement.
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
3. If ADD 1 15 3 TO COUNTER is
performed and COUNTER is
initialized at 10, the sum of _____
will be placed in______ at the end
of the operation
SOLUTION:--29; COUNTER
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
4. Without using the word TO, write a
statement equivalent to the one in
Question 3.
SOLUTION:-- ADD 1 15 3 COUNTER
GIVING WS-AREA1.
– In this case, the result is placed in WS-AREA1
and COUNTER remains unchanged. The
arithmetic is the same as in the previous
problem.
– ADD 1 15 3 COUNTER GIVING COUNTER is
also correct.
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
5. If ADD 1 15 3 GIVING
COUNTER is performed,
______will be the result in _____.
SOLUTION:--19; COUNTER
Structured COBOL Programming, Stern & Stern, 9th Edition
SUBTRACT STATEMENT
• The SUBTRACT operation has the
following two formats:
Format 1
SUBTRACT {identifier-1} {literal-1} ...
FROM identifier-2 ..
Format 2
SUBTRACT {identifier-1} {literal-1} ...
FROM {identifier-2} {literal-2}
GIVING identifier-3...
Structured COBOL Programming, Stern & Stern, 9th Edition
SUBTRACT STATEMENT
• Rules for Interpreting the Instruction
Format
1. All literals and data-names that are part
of the subtraction must be numeric; the
field specified after the word GIVING,
however, may be a report-item.
2. The receiving field, must be a data-name
and not a literal.
– The following statement is incorrect:
SUBTRACT TAX FROOM 100.00
Structured COBOL Programming, Stern & Stern, 9th Edition
SUBTRACT STATEMENT
• Rules for Interpreting the Instruction
Format
3. All fields and literals preceding the word
FROM will be added together and the sum
subtracted from the field following the
word FROM.
– The result, or difference, will be placed in this
same field if no GIVING option is used.
– All other fields will remain unchanged.
Structured COBOL Programming, Stern & Stern, 9th Edition
SUBTRACT STATEMENT
• Rules for Interpreting the Instruction
Format
4. When using the GIVING option, the
operation performed is the same as in
Rule 3, but the result, or difference, is
placed in the field following the word
GIVING.
– The initial contents of the resultant field after
the word GIVING do not take part in the
arithmetic operation.
Structured COBOL Programming, Stern & Stern, 9th Edition
SUBTRACT STATEMENT
Examples:
1.
SUBTRACT 15.40 TAX TOTAL
FROM AMT
2.
SUBTRACT 15.40 TAX TOTAL
FROM AMT GIVING NET
• Examples 1 and 2 produce the same
result but in different storage areas.
– In Example 2, the original contents of NET
are replaced with the result and do not affect
the calculation.
Structured COBOL Programming, Stern & Stern, 9th Edition
SUBTRACT STATEMENT
Deciding Which Format to Use
• As a rule, when the contents of an
operand are not needed after the
SUBTRACT operation, Format 1 may be
used.
• When the contents of all operands are to
be retained, use Format 2.
Structured COBOL Programming, Stern & Stern, 9th Edition
SUBTRACT STATEMENT
• As with ADD operations, all commas are
optional.
– A space must, however, follow each comma.
• It is possible to perform several
SUBTRACT operations with a single
statement using Format 1: The following
is a valid statement:
SUBTRACT AMT1 AMT2 AMT3
FROM TOTAL 1
TOTAL 2
TOTAL 3
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
1. In the operation:
SUBTRACT 1500 FROM GROSS GIVING NET
the result, or difference, is placed in ____.
– What happens to the original contents of
GROSS? If GROSS has an original value of
8500, and NET has an original value of 2000,
the result in NET would be _______.
SOLUTION:-- NET; it remains unchanged;
7000 (The original 2000 in NET does not
enter into the calculation.)
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
What is wrong with Statements 2 and 3?
2. SUBTRACT $23.00 FROM AMOUNT
SOLUTION:-- $23.00 is an invalid numeric
literal - numeric literals may not contain
dollar signs.
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
3. SUBTRACT AMT FROM 900.00
SOLUTION:--The resultant field of a
SUBTRACT operation may not be a
literal.
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
4. Change the statement in Question 3
to make it valid.
SOLUTION:--SUBTRACT AMT FROM
900.00 GIVING TOTAL
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
5. Use one SUBTRACT statement to
subtract three fields (TAX, CREDIT,
DISCOUNT) from TOTAL and place
the answer in WS-AMT.
SOLUTION:--SUBTRACT TAX CREDIT
DISCOUNT FROM TOTAL GIVING
WS-AMT
Structured COBOL Programming, Stern & Stern, 9th Edition
MULTIPLY and DIVIDE
Statements
• MULTIPLY has the following formats:
FORMAT 1:
MULTIPLY {identifier-1} {literal-1} BY
identifier-2..
FORMAT 2:
MULTIPLY {identifier-1}{literal-1} BY
{identifier-2} {literal-2}
GIVING
identifier-3...
Structured COBOL Programming, Stern & Stern, 9th Edition
MULTIPLY and DIVIDE
Statements
Multiply Examples:
1. MULTIPLY HOURS-WORKED BY
HOURLY-RATE
2. MULTIPLY QTY BY PRICE
3. MULTIPLY 2000 BY NO-OFEXEMPTIONS GIVING EXEMPTIONAMT.
4. MULTIPLY 60 BY HOURS GIVING
MINUTES
Structured COBOL Programming, Stern & Stern, 9th Edition
MULTIPLY and DIVIDE
Statements
• The DIVIDE statement has the following
instruction formats:
Format 1:
DIVIDE {identifier-1} {literal-1} INTO
identifier-2 . . .
Format 2:
DIVIDE {identifier-1} {literal-1}
{identifier-2} {literal-2} GIVING
identifier-3...
Structured COBOL Programming, Stern & Stern, 9th Edition
MULTIPLY and DIVIDE
Statements
Format 3:
DIVIDE {identifier-1} {literal-1} BY
{identifier-2} {literal-2} GIVING
identifier-3...
• Either the word INTO or BY may be used
with a DIVIDE statement.
• The GIVING clause is optional with INTO
but is required with BY.
Structured COBOL Programming, Stern & Stern, 9th Edition
MULTIPLY and DIVIDE
Statements
DIVIDE EXAMPLES:
1. DIVIDE MINUTES BY 60 GIVING
HOURS
2. DIVIDE 60 INTO MINUTES GIVING
HOURS
3. DIVIDE 12 INTO ANN-SAL-IN GIVING
MONTHLY-SAL-OUT
Structured COBOL Programming, Stern & Stern, 9th Edition
MULTIPLY and DIVIDE
Statements
DIVIDE EXAMPLES:
4. DIVIDE ANN-SAL-IN BY 12 GIVING
MONTHLY-SAL-OUT
• All arithmetic operations can have more
than one resultant field.
– Although ADD and SUBTRACT instructions can
operate on numerous fields, the MULTIPLY
and DIVIDE instructions are limited in the
number of operations performed.
Structured COBOL Programming, Stern & Stern, 9th Edition
EXAMPLES OF ARITHMETIC
OPERATIONS
Example 1:
Celsius temperatures are to be converted to
Fahrenheit temperatures according to
the following formula:
FAHRENHEIT= (9/5) CELSIUS +32
One solution is as follows:
MULTIPLY 9 BY CELSIUS
DIVIDE 5 INTO CELSIUS
ADD 32 CELSIUS GIVING FAHRENHEIT
Structured COBOL Programming, Stern & Stern, 9th Edition
EXAMPLES OF ARITHMETIC
OPERATIONS
FAHRENHEIT= (9/5) CELSIUS +32
A second solution is:
MULTIPLY 1.8 BY CELSIUS
ADD 32 CELSIUS GIVING
FAHRENHEIT
Structured COBOL Programming, Stern & Stern, 9th Edition
EXAMPLES OF ARITHMETIC
OPERATIONS
Example 2: Compute the average of three
fields, EXAM1, EXAM2, EXAM3. Place the
answer in AVERAGE without altering the
contents of the three fields.
One possible solution is:
ADD EXAM 1 EXAM 2 EXAM 3
GIVING AVERAGE
DIVIDE 3 INTO AVERAGE
Structured COBOL Programming, Stern & Stern, 9th Edition
MULTIPLY and DIVIDE
Statements
Use of the REMAINDER Clause in the
DIVIDE Operation
• When performing a division operation,
the result will be placed in the receiving
field according to the PIC specifications
of that field. Consider the following:
Example 1:
DIVIDE 130 BY 40 GIVING WS-TOTAL
Structured COBOL Programming, Stern & Stern, 9th Edition
MULTIPLY and DIVIDE
Statements
Use of the REMAINDER Clause in the
DIVIDE Operation
Example 1: DIVIDE 130 BY 40 GIVING
WS-TOTAL
• WS-TOTAL has a PICTURE of 99. After
the operation is performed, 03 is placed
in WS-TOTAL. The remainder of the
division can be saved by using the
REMAINDER clause
Structured COBOL Programming, Stern & Stern, 9th Edition
MULTIPLY and DIVIDE
Statements
• Additional Instruction Formats for the
DIVIDE Statement
Format 4: DIVIDE {identifier-1} {literal-1}
INTO {identifier-2} {literal-2} GIVING
identifier-3 REMAINDER identifier-4..
Format 5: DIVIDE {identifier-1} {literal-1}
BY {identifier-2} {literal-2} GIVING
identifier-3 REMAINDER identifier-4..
Structured COBOL Programming, Stern & Stern, 9th Edition
MORE QUESTIONS!!!
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
1. DISTANCE is the distance traveled in a
specific car trip, and GAS is the number of
gallons of gas used. Calculate the average
gas mileage and place it in a field called
AVERAGE.
SOLUTION:--DIVIDE DISTANCE BY GAS
GIVING AVERAGE
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
2. Using MULTIPLY and DIVIDE verbs,
compute:
(C / B + E / F) X S
SOLUTION:
DIVIDE B INTO C
DIVIDE F INTO E
ADD C
E
GIVING WS-HOLD-AREA
MULTIPLY WS-HOLD-AREA BY S
GIVING ANS.
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
What, if anything, is wrong with the
following three statements?:
3. DIVIDE -35 INTO A
SOLUTION:--Nothing is wrong
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
4. MULTIPLY A TIMES B GIVING C
SOLUTION:--The preposition must be
BY in the MULTIPLY operation.
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
5. MULTIPLY A BY B BY C GIVING D
SOLUTION:--Only two operands may be
multiplied together with one MULTIPLY
verb
Structured COBOL Programming, Stern & Stern, 9th Edition
DEBUGGING WITH COBOL 85
• When using a separate clause such as ON
SIZE ERROR, use a scope terminator with
COBOL 85 to delimit or end the arithmetic
operation.
• END-ADD, END-SUBTRACT, ENDMULTIPLY, and END-DIVIDE are all
permissible scope terminators.
• For COBOL 74, each statement ends with
a period instead of a scope terminator.
Structured COBOL Programming, Stern & Stern, 9th Edition
THE COMPUTE
STATEMENT
Structured COBOL Programming, Stern & Stern, 9th Edition
BASIC FORMAT
• If complex or extensive arithmetic
operations are required in a program the
use of the four arithmetic verbs may
prove cumbersome.
• The COMPUTE verb provides another
method of performing arithmetic. The
COMPUTE statement uses arithmetic
symbols rather than arithmetic verbs.
Structured COBOL Programming, Stern & Stern, 9th Edition
SYMBOLS USED IN A
COMPUTE
Symbol
Meaning
+
ADD
-
SUBTRACT
*
MULTIPLY
/
DIVIDE
**
exponentiation
– There is no COBOL verb for exponentiation.
Structured COBOL Programming, Stern & Stern, 9th Edition
SYMBOLS USED IN A
COMPUTE
The following examples illustrate the use of
the COMPUTE verb:
1. COMPUTE TAX = .05 * AMT
2. COMPUTE DAILY-SALES = QTY * UNITPRICE / 5
3. COMPUTE NET = AMT - .05 * AMT
Structured COBOL Programming, Stern & Stern, 9th Edition
COMPUTE BASIC FORMAT
• The COMPUTE statement has a dataname or identifier to the left of the equal
sign.
– The value computed in the arithmetic
expression to the right of the equal sign is
placed in the field preceding the equal sign.
• The fields specified after the equal sign in
a COMPUTE statement may be numeric
literals or data-names with numeric PIC
clauses.
– The COMPUTE statement may include more
than one operation.
Structured COBOL Programming, Stern & Stern, 9th Edition
COMPUTE BASIC FORMAT
• The COMPUTE statement has the
advantage of performing more than one
arithmetic operation with a single verb.
– For this reason, it is often less cumbersome to
use COMPUTE statements to code complex
arithmetic.
• In addition, we may raise a number to a
power with the use of the arithmetic
symbol ** in a COMPUTE statement.
Structured COBOL Programming, Stern & Stern, 9th Edition
Rules with Using COMPUTE
• The ROUNDED and ON SIZE ERROR
options may also be used with the
COMPUTE.
• The rules governing the use of these
clauses in ADD, SUBTRACT, MULTIPLY,
and DIVIDE operations apply to COMPUTE
statements as well.
• To round the results in a COMPUTE
statement to the specifications of the
receiving field, we use the ROUNDED
option directly following the receiving
field.
Structured COBOL Programming, Stern & Stern, 9th Edition
Rules with Using COMPUTE
• The Format for the COMPUTE verb is as
follows:
COMPUTE identifier-1 [ROUNDED] . . . =
{arithmetic expression-1} {literal1}{identifier-2}
[ON SIZE ERROR
imperative statement-1] [NOT ON SIZE
ERROR imperative statement-2]
[END-COMPUTE]*
*COBOL 85 only
Structured COBOL Programming, Stern & Stern, 9th Edition
Order of Evaluation
• The order in which arithmetic operations
are performed will affect the results in a
COMPUTE statement. Consider the
following example:
COMPUTE UNIT-PRICE-OUT = AMT1-IN +
AMT2-IN / QTY-IN
• Depending on the order of evaluation of
arithmetic operations, one of the
following would be the mathematical
equivalent of the preceding:
a. UNIT-PRICE-OUT = AMT1-IN + AMT2-IN QTYIN / QTY-IN
Structured COBOL Programming, Stern & Stern, 9th Edition
Order of Evaluation
b. UNIT-PRICE-OUT = AMT1-IN + AMT2IN / QTY-IN
• Note that (a) and (b) are not identical.
• If AMT1-IN = 3,AMT2-IN = 6, and QTY-IN
= 3, the result of the COMPUTE statement
evaluated according to the formula in (a)
is 3 [(3 + 6) / 3] but according to the
formula in (b) is 5 [3 + (6 / 3)].
Structured COBOL Programming, Stern & Stern, 9th Edition
The Sequence in which
Operations are Performed in a
COMPUTE Statement
1. **
2. * or / (whichever appears first from
left to right)
3. + or - (whichever appears first from
left to right)
4. The use of parentheses overrides rules
1--3. That is, operations within
parentheses are performed first.
Structured COBOL Programming, Stern & Stern, 9th Edition
The Sequence in which
Operations are Performed in a
COMPUTE Statement
• Without parentheses, exponentiation is
performed first.
• Multiplication and division operations
follow any exponentiation and precede
addition or subtraction operations.
• If there are two or more multiplication or
division operations, they are evaluated
from left to right in the expression.
• Addition and subtraction are evaluated
last; these are also from left to right.
Structured COBOL Programming, Stern & Stern, 9th Edition
DEBUGGING TIP
• If one arithmetic statement will
suffice, use it;
• If it takes more than one, use a
COMPUTE.
Structured COBOL Programming, Stern & Stern, 9th Edition
COBOL 2000+ CHANGES
1. Spaces around arithmetic operators
such as *, /, +, - , and ** will no longer
be required.
2. The COMPUTE statement will yield the
same results regardless of the compiler
used by making the precision or number
of decimal places in each intermediate
calculation fixed.
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SLIDES END HERE
CHAPTER SUMMARY COMES NEXT
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY
A. The ADD, SUBTRACT, MULTIPLY, and
DIVIDE verbs all have a GIVING format.
– With this GIVING format, the receiving field
is not part of the arithmetic and can be a
report-item.
B. A COMPUTE can be used for performing
multiplication, division, addition,
subtraction, exponentiation, or a
combination of these.
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY
C. The COMPUTE can save coding if used in
place of the ADD, SUBTRACT, MULTIPLY,
and DIVIDE verbs:
OPERATIONS
+ Addition
- Subtraction
/ Division
** Exponentiation
* Multiplication
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY
D. If several operations are performed with
one COMPUTE statement, the order of
evaluation is as follows:
1. **
2. * or / in sequence left to right
3. + or - in sequence left to right
4. Parentheses () override normal hierarchy
rules
E. The ROUNDED and ON SIZE ERROR
options can be used with the four
arithmetic verbs and with the COMPUTE.
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY
F. With COBOL 85, NOT ON SIZE ERROR can
be used as well. With COBOL 85
– With COBOL 85, when using ON SIZE ERROR
or NOT ON SIZE ERROR with any arithmetic
verb, use a scope terminator (END-ADD, ENDSUBTRACT, END-MULTIPLY, END-DIVIDE,
END-COMPUTE).
– With COBOL 74, be sure the ON SIZE ERROR
clause ends with a period.
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY
G. USAGE Clause
1. Specifies how data is to be stored
internally.
2. Options available:
a. USAGE IS DISPLAY
b. USAGE IS [PACKED-DECIMAL,
COMPUTATIONAL-3, COMP-3]
c. USAGE IS COMPUTATIONAL
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY
H. Intrinsic functions have been added as
an extension to COBOL 85.
– These include calendar, numerical analysis,
statistical, trigonometric, financial, character,
and string functions.
Structured COBOL Programming, Stern & Stern, 9th Edition