Arithmetic Statements


COBOL has dedicated statements to perform Arithmetic operations instead of using inline operators like other programming languages. These statements allow developers to perform addition, subtraction, multiplication, division, and more complex computations.

The arithmetic statements in COBOL are -

  • ADD
  • SUBTRACT
  • MULTIPLY
  • DIVIDE
  • COMPUTE
Note! The result is stored in the variables associated with GIVING phrase.

ADD Statement


ADD statement performs addition operations. It sums two or more numeric operands to a variable coded after TO and stores the result in the output variable coded after GIVING. If the GIVING phrase is ignored, then the result is stored in the variables that are coded after the TO.

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 -

Scenario - Adding number to the value in variable.

----+----1----+----2----+----3----+----4----+----5----+
       ...
       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: 60

SUBTRACT Statement


SUBTRACT statement performs subtract operations. It subtracts two or more numeric operands from a variable coded after FROM and stores the result in the output variable coded after GIVING. If GIVING phrase is ignored, then the result stores in the variables that are coded after FROM.

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 -

Scenario - Subtract one variable from other.

----+----1----+----2----+----3----+----4----+----5----+
       ...
       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

MULTIPLY Statement


MULTIPLY statement performs multiplication operations. It multiplies one or more numeric operands by a variable coded after BY and stores the result in the output variable coded after GIVING. If GIVING phrase is ignored, then the result stores in the variables that are coded after BY phrase.

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 -

Scenario - Multiplying two variables.

----+----1----+----2----+----3----+----4----+----5----+
       ...
	   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

DIVIDE Statement


DIVIDE statement performs division operations. It's similar to mathematics, where we have a dividend (the number being divided) and a divisor (the number we're dividing by). It divides one or more numeric operands by a variable coded after BY and stores the result in the output variable coded after GIVING. It sets the output values quotient (result) and the remainder to the variables that are coded with corresponding phrases. If GIVING phrase is ignored, then the result stores in the variables that are coded after INTO phrase.

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 -

Scenario - Divide one variable with other, result and remainder into separate variables.

----+----1----+----2----+----3----+----4----+----5----+
       ...
	   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


COMPUTE is used to perform all kind of arithmetic operations in a single expression. The arithmetic operators used in COMPUTE statements are + (ADD), - (SUBTRACT), * (MULTIPLY), / (DIVIDE), and ** (EXPONENT).

Syntax -

COMPUTE output-variable [ROUNDED] = arithmetic-expression
       [ON SIZE ERROR statements-set1]
   [NOT ON SIZE ERROR statements-set2]
[END-COMPUTE]

Examples -

Scenario - Arithmetic operations on three variables.

----+----1----+----2----+----3----+----4----+----5----+
       ...
	   WORKING-STORAGE SECTION.
	   01 WS-VAR.
          05 WS-VAR1      PIC 9(03) VALUE 10.
          05 WS-VAR2      PIC 9(03) VALUE 5.
          05 WS-VAR3      PIC 9(03) VALUE 5.
          05 WS-VAR4      PIC 9(03) VALUE 0.
       ...
	   PROCEDURE DIVISION.
           COMPUTE WS-VAR4 = (WS-VAR1 - WS-VAR2) * WS-VAR3.
           DISPLAY "Result: " WS-VAR4.
		   ...

Output -

Result: 025