SUBTRACT Statement
- SUBTRACT statement performs subtraction operations.
- Depending on our requirements, it can subtract one or more numeric items from another numeric item or subtract two numeric items and store the result in a third.
SUBTRACT statement has below formats based on their usage -
- Simple SUBTRACT statement.
- SUBTRACT with GIVING.
- SUBTRACT with ON SIZE ERROR (Error handling).
- SUBTRACT with CORRESPONDING.
Simple SUBTRACT -
In this format, all the input numeric values or values in variables are subtracted from the output variable, and the result will be placed into the output variable.
Syntax -
---+----2----+----3----+----4----+----5
SUBTRACT input-value1|input-variable1
FROM output variable [ROUNDED]
[END-SUBTRACT].
Parameters -
- input-variable1, ... - Numeric items that will be subtracted. From example, it is WS-A.
- FROM output-variable - Specifies the numeric item from which the subtraction will take place. From example, it is WS-B.
- ROUNDED Phrase - Used to round the fraction result to the nearest integer value based on the faction value.
- END-SUBTRACT - Specifies the scope terminator for the SUBTRACT statement and is optional when the period is coded at the end of the SUBTRACT statement.
Example -
Scenario - 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
SUBTRACT with GIVING -
The GIVING phrase is used when the output variables only used to store the result and not part of the inputs. All inputs before the keyword FROM are subtracted from the inputs after the keyword FROM, and the result is stored in the variables with the GIVING phrase.
Syntax -
---+----2----+----3----+----4----+----5
SUBTRACT input-value1|input-variable1
FROM output variable
GIVING output-variable1 [ROUNDED]
[END-MULTIPLY].
Example -
Scenario - 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
SUBTRACT with ON SIZE ERROR (Error handling) -
ON SIZE ERROR is used to handle the program flow when the result value exceeds the maximum value that a result variable can hold. NOT ON SIZE ERROR phrase handles the program flow when the result value is within the range.
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].
Parameters -
- SIZE ERROR - Refer to the SIZE ERROR phrase for full information.
- statements-block1 - specifies the set of statements that are executed when SIZE ERROR occurs. From the example, it is DISPLAY "OVERFLOW".
- statements-block2 - specifies the set of statements that are executed when the multiply is successful. From the example, it is DISPLAY "WS-C: " WS-C.
Example -
Scenario - Subtract error handling.
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(02).
PROCEDURE DIVISION.
SUBTRACT WS-A FROM WS-B
GIVING WS-C
ON SIZE ERROR DISPLAY "OVERFLOW"
NOT ON SIZE ERROR DISPLAY "WS-C: " WS-C.
Output -
OVERFLOW
SUBTRACT with CORRESPONDING -
SUBTRACT CORRESPONDING [CORR] subtracts the corresponding identical elementary variables in two groups. The results get stored in the elementary variables of the group variable after the keyword FROM.
Syntax -
---+----2----+----3----+----4----+----5
SUBTRACT [CORRESPONDING|CORR]
input-group-variable
FROM output-group-variable
[ON SIZE ERROR Statements-block1]
[NOT ON SIZE ERROR Statements-block2]
[END-SUBTRACT].
Parameters -
- CORRESPONDING|CORR Phrase - This phrase performs the SUBTRACT on the same named elementary variables under two different group variables.
- input-group-variable - From example, it is WS-VAR1.
- output-group-variable - It is one of the inputs. From example, it is WS-VAR2.
Example -
Scenario - Subtracting corresponding variables in two groups.
WORKING-STORAGE SECTION.
01 WS-VAR1.
05 WS-A PIC 9(02) VALUE 10.
05 WS-B PIC 9(02) VALUE 10.
01 WS-VAR2.
05 WS-A PIC 9(02) VALUE 20.
05 WS-B PIC 9(02) VALUE 30.
PROCEDURE DIVISION.
SUBTRACT CORR WS-VAR1 FROM WS-VAR2.
DISPLAY "WS-A: " WS-A OF WS-VAR2.
DISPLAY "WS-B: " WS-B OF WS-VAR2.
OUTPUT -
WS-A: 10 WS-B: 20