Friday, December 17, 2010

COBOL Tutorial – Arithmetic Operators part 2

How is the 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.

Example 

Image12[1] 

Result 

Image13[1]