Title: Arithmetic Statements in COBOL
1Arithmetic Statements in COBOL
2Arithmetic Verb Template
- Most COBOL arithmetic verbs conform to the
template above. For example - ADD INVOICE TO CASH-TOTAL
- ADD MALES TO FEMALES GIVING TOTAL-STUDENTS
- SUBTRACT TAX FROM NET-PAY SUBTRACT TAX FROM
GROSS-PAY GIVING NET-PAY - DIVIDE TOTAL BY STUDENTS GIVING
STUDENT-AVERAGE DIVIDE STUDENTS INTO TOTAL
GIVING STUDENT-AVERAGE - MULTIPLY 10 BY MAGNITUDE
- MULTIPLY MEMBERS BY SUBS GIVING TOTAL-SUBS
- The exceptions are the COMPUTE and the DIVIDE
with REMAINDER.
3Addition (page 255)
Syntax ADD identifier-1 / literal-1 . . . TO
identifier-2 GIVING identifier-n END-AD
D
- Examples
- 1. ADD A TO B
- Before
- After
-
7
5
5
12
4More ADD Examples
ADD CASH TO TOTAL Before
3 100 After ADD CASH 20 TO
TOTAL WAGE Before 5
1000 100 After ADD CASH
TOTAL GIVING RESULT Before 3
1000 0015 After ADD MALES TO
FEMALES GIVING TOTAL-STUDENTS Before
1500 0625
1234 After
- 3 103
- 5 1025 125
- 3 1000 1003
- 1500 0625 2125
5Subtract Statement (page 259)
Syntax SUBTRACT identifier-1 / literal-1 . . .
FROM identifier-2 / literal-2
GIVING identifier-n END-SUBTRACT
6SUBTRACT Examples
SUBTRACT TAX FROM GROSS-PAY TOTAL Before
120 4000
9120 After SUBTRACT TAX 80 FROM
TOTAL Before 100
480 After SUBTRACT TAX FROM
GROSS-PAY GIVING NET-PAY Before
750 1000
0012 After
- 120 3880 9000
- 100 300
- 750 1000
0250
7Multiplication (page 261)
Syntax MULTIPLY identifier-1 / literal-1 . .
. BY identifier-2 GIVING
identifier-n END-MULTIPLY
8MULTIPLY Examples
MULTIPLY SUBS BY
MEMBERS GIVING TOTAL-SUBS
END-MULTIPLY.
SUBS MEMBERS TOTAL-SUBS Before
15.50 100
0123.45 After MULTIPLY 10 BY MAGNITUDE SIZE
Before
355 125 After MULTIPLY A BY
B GIVING C Before
10 20
43 After
- 15.50 100 1550.00
- 3550 1250
- 10 20
200
9The Divide Statements
10DIVIDE Examples
DIVIDE A INTO B (gt
B/A) A
B Before
10 50 After DIVIDE A BY B
GIVING C (gt A/B)
A B
C Before
60 20 11 After DIVIDE
TOTAL BY MEMBERS GIVING AVERAGE
ROUNDED. Before 9234.55
100 1234.56 After
- 10 5
- 60 20 3
- 9234.55 100
92.35
11The COMPUTE
Precedence Rules. 1. EXPONENTIAL NN 2.
MULTIPLY x / DIVIDE 3. ADD - SUBT
RACT - order can be changed by ( )
Compute IrishPrice SterlingPrice / Rate
100. Before 1000.50
156.25 87 After
12The ROUNDED option
Receiving Field Actual Result Truncated
Result Rounded Result PIC 9(3)V9.
123.25 PIC 9(3). 123.25
- The ROUNDED option takes effect when, after
decimal point alignment, the result calculated
must be truncated on the right hand side. - The option adds 1 to the receiving item when the
leftmost truncated digit has an absolute value of
5 or greater. - Immediately follows the receiving field
- Is optional with all Arithmetic operations
13The ROUNDED option
PICTURE FIELD
ROUNDING FACTOR TO ADD
PIC 9(n) PIC 9(n)V9 PIC 9(n)V99 PIC 9(n)V999 PIC
9(n)V9999 etc
0.5 0.05 0.005 0.0005 0.00005 etc
Where n is a ve Integer, n lt 18