You often need to do Math in COBOL. You can perform Arithmetic in COBOL using Arithmetic Operators. When you perform some arithmetic on two or more numeric quantities, such an expression is called an Arithmetic Expressions. There are 4 main Arithmetic Operators in COBOL – ADD, SUBTRACT, MULTIPLY and DIVIDE.
You can even write complex arithmetic expressions by using the COMPUTE verb. I have written about the syntax of each operator, followed by examples and some results.
You can even write complex arithmetic expressions by using the COMPUTE verb. I have written about the syntax of each operator, followed by examples and some results.
2. How’z the ADD Operator used in COBOL
The ADD operator can be used to add two or more quantities. In the assembly language, we write the instruction ADD A B to find the sum of A and B. The final sum is stored in A. On the same lines, the ADD Operator in COBOL has the following syntax :
Syntax
ADD one-quantity TO second-quantity
The final sum result is stored in the second-variable. For example, if A=10, and I write
ADD 5 TO A
After performing the above step, the computer adds 5 to the value of A, so A=15. Suppose a variable B=5, then I may also write
ADD B TO A
Syntax
ADD A B C TO SUM
In this case, all the three group of numbers A B and C, would be added to SUM. So, assuming A=10, B=15, and C=20, if initially RESULT = 100, after addition of A, B and C to SUM, the value of RESULT = 100 + (10+15+20) = 145.
Syntax
ADD one-quantity TO second-quantity
The final sum result is stored in the second-variable. For example, if A=10, and I write
ADD 5 TO A
After performing the above step, the computer adds 5 to the value of A, so A=15. Suppose a variable B=5, then I may also write
ADD B TO A
Syntax
ADD A B C TO SUM
In this case, all the three group of numbers A B and C, would be added to SUM. So, assuming A=10, B=15, and C=20, if initially RESULT = 100, after addition of A, B and C to SUM, the value of RESULT = 100 + (10+15+20) = 145.