Friday, December 17, 2010

How does the SUBTRACT Operator work in COBOL?

When you want to find the difference of two or more quantities in COBOL, you use the SUBTRACT operator. The SUBTRACT operator subtracts one quantity from another, and finds out the result. In its simplest form, SUBTRACT has the following syntax :
Syntax
SUBTRACT one-quantity FROM second-quantity

For example,

SUBTRACT WS-VAR-2 FROM WS-VAR-1

Consider the variable WS-VAR-1 = 10, WS-VAR-2=30, subtracting WS-VAR-2 from WS-VAR-1 would result in the difference between 30 and 10. The answer is 20. This final result is stored in WS-VAR-2. SUBTRACT B FROM A is equivalent to the statement A = A – B.

Image03[2] 

The example above is shown in the code snippet below :

Image04[2]