Arithmetic Statements
In COBOL, arithmetic operations are performed using specific statements rather than inline operators like some modern programming languages. These statements allow developers to carry out operations like addition, subtraction, multiplication, division, and more complex computations.
The arithmetic statement in COBOL language are -
- ADD
- SUBTRACT
- MULTIPLY
- DIVIDE
- COMPUTE
ADD Statement -
The ADD statement is used to perform addition operations. The ADD statement sums two or more numeric values and stores the result in the output variable. If only one value or one variable is used as an input, then it adds to the output variable and places the result into the output variable.
Syntax -
---+----2----+----3----+----4----+----5
ADD input-value1|input-variable1
[......]
TO input-variableA
GIVING output-variable1 [ROUNDED]
[,.....]
[ON SIZE ERROR Statements-block1]
[NOT ON SIZE ERROR Statements-block2]
[END-ADD].
Examples -
Scenario1 - Adding number to a variable.
WORKING-STORAGE SECTION.
01 WS-VAR.
05 WS-A PIC 9(03) VALUE 20.
PROCEDURE DIVISION.
ADD 10 TO WS-A.
DISPLAY "Result: " WS-A.
Output -
Result: 30
Scenario2 - Adding two variables to a variable.
WORKING-STORAGE SECTION.
01 WS-VAR.
05 WS-A PIC 9(03) VALUE 900.
05 WS-B PIC 9(03) VALUE 200.
05 WS-B PIC 9(03) VALUE 100.
PROCEDURE DIVISION.
ADD WS-A, WS-B TO WS-C
ON SIZE ERROR DISPLAY "OVERFLOW"
NOT ON SIZE ERROR DISPLAY "WS-C: " WS-C.
Output -
OVERFLOW
SUBTRACT Statement -
The SUBTRACT statement is used to perform subtraction operations. Depending on our requirements, it can subtract one or more numeric items from another numeric item, or it can subtract two numeric items and store the result in a third.
Syntax -
---+----2----+----3----+----4----+----5
SUBTRACT input-value1|input-variable1
FROM output variable
GIVING output-variable1 [ROUNDED]
[ON SIZE ERROR statements-block1]
[NOT ON SIZE ERROR statements-block2]
[END-SUBTRACT].
Example -
Scenario1 - Subtract one variable from other.
WORKING-STORAGE SECTION.
01 WS-VAR.
05 WS-A PIC 9(03) VALUE 20.
05 WS-B PIC 9(03) VALUE 30.
PROCEDURE DIVISION.
SUBTRACT WS-A FROM WS-B.
DISPLAY "Result: " WS-B.
Output -
Result: 10
Scenario2 - Subtract one variable from other and placing result into third.
WORKING-STORAGE SECTION.
01 WS-VAR.
05 WS-A PIC 9(03) VALUE 200.
05 WS-B PIC 9(03) VALUE 900.
05 WS-C PIC 9(03).
PROCEDURE DIVISION.
MULTIPLY WS-A BY WS-B
GIVING WS-C.
DISPLAY "WS-C: " WS-C.
Output -
WS-C: 700
MULTIPLY Statement -
The MULTIPLY statement is used for multiplication operations. It allows multiplying one or more numeric items and storing the result in one or more receiving items.
Syntax -
---+----2----+----3----+----4----+----5
MULTIPLY input-value1|input-variable1
BY input-variableA
GIVING output-variable1 [ROUNDED]
[ON SIZE ERROR Statements-block1]
[NOT ON SIZE ERROR Statements-block2]
[END-MULTIPLY].
Examples -
Scenario1 - Multiplying two variables.
WORKING-STORAGE SECTION.
01 WS-VAR.
05 WS-A PIC 9(03) VALUE 20.
05 WS-B PIC 9(03) VALUE 30.
PROCEDURE DIVISION.
MULTIPLY WS-A BY WS-B.
DISPLAY "Result: " WS-B.
Output -
Result: 600
Scenario2 - Multiply with error handling.
WORKING-STORAGE SECTION.
01 WS-VAR.
05 WS-A PIC 9(03) VALUE 90.
05 WS-B PIC 9(03) VALUE 20.
PROCEDURE DIVISION.
MULTIPLY WS-A BY WS-B
ON SIZE ERROR DISPLAY "OVERFLOW"
NOT ON SIZE ERROR DISPLAY "WS-B: " WS-B.
Output -
OVERFLOW
DIVIDE Statement -
The DIVIDE statement divides one number by another. It's similar to mathematics, where we have a dividend (the number being divided) and a divisor (the number we're dividing by). The DIVIDE statement sets the output values quotient and the remainder to the corresponding variables.
Syntax -
DIVIDE ws-variable1
INTO ws-variable2
GIVING ws-result [ROUNDED]
REMAINDER ws-remainder
[ON SIZE ERROR statements-block1]
[NOT ON SIZE ERROR statements-block2]
[END-DIVIDE].
Examples -
Scenario1 - Divide one variable with other.
WORKING-STORAGE SECTION.
01 WS-VAR.
05 WS-VAR1 PIC 9(03) VALUE 50.
05 WS-VAR2 PIC 9(03) VALUE 5.
PROCEDURE DIVISION.
DIVIDE WS-VAR1 INTO WS-VAR2
DISPLAY "Result: " WS-VAR2.
Output -
Result: 10
Scenario2 - Divide one variable with other, result and remainder into separate variables.
WORKING-STORAGE SECTION.
01 WS-VAR.
05 WS-VAR1 PIC 9(03) VALUE 32.
05 WS-VAR2 PIC 9(03) VALUE 5.
05 WS-VAR3 PIC 9(03) VALUE 0.
05 WS-VAR4 PIC 9(03) VALUE 0.
PROCEDURE DIVISION.
DIVIDE WS-VAR1 INTO WS-VAR2
GIVING WS-VAR3
REMAINDER WS-VAR4
DISPLAY "Result: " WS-VAR3.
DISPLAY "Remainder: " WS-VAR4.
Output -
Result: 6 Remainder: 2
COMPUTE Statement -
The COMPUTE is used to perform all arithmetic operations. It is flexible because it can handle multiple arithmetic operations in one expression. The arithmetic operators used in COMPUTE statements are + (ADD), - (SUBTRACT), * (MULTIPLY), / (DIVIDE), and ** (EXPONENT) statements.
Syntax -
COMPUTE output-variable [ROUNDED] = arithmetic-expression
[ON SIZE ERROR statements-set1]
[NOT ON SIZE ERROR statements-set2]
[END-COMPUTE]
Examples -
Scenario1 - Adding one variable with other.
WORKING-STORAGE SECTION.
01 WS-VAR.
05 WS-VAR1 PIC 9(03) VALUE 50.
05 WS-VAR2 PIC 9(03) VALUE 5.
05 WS-VAR3 PIC 9(03).
PROCEDURE DIVISION.
COMPUTE WS-VAR3 = WS-VAR1 + WS-VAR2.
DISPLAY "Result: " WS-VAR2.
Output -
Result: 055
Scenario2 - Subtract two variables.
WORKING-STORAGE SECTION.
01 WS-VAR.
05 WS-VAR1 PIC 9(03) VALUE 32.
05 WS-VAR2 PIC 9(03) VALUE 5.
05 WS-VAR3 PIC 9(03) VALUE 0.
PROCEDURE DIVISION.
COMPUTE WS-VAR3 = WS-VAR1 - WS-VAR2.
DISPLAY "Result: " WS-VAR3.
Output -
Result: 027
