MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL SUBTRACT and END-SUBTRACT Statements - Quick Reference

The SUBTRACT and END-SUBTRACT statements in COBOL are used to perform arithmetic subtraction operations. They allow you to subtract one or more values from one or more target fields, with options for storing results in separate fields and handling overflow conditions.

Primary Use

Perform arithmetic subtraction operations

Division

PROCEDURE DIVISION

Type

Arithmetic statement

Status

Required for subtraction operations

Overview

The SUBTRACT statement is a fundamental COBOL arithmetic statement used to perform subtraction operations. It can subtract a single value from multiple target fields, multiple values from a single target field, or multiple values from multiple target fields. The statement supports various options including GIVING for storing results in separate fields, ROUNDED for handling decimal precision, and ON SIZE ERROR for overflow handling. END-SUBTRACT is used to terminate complex SUBTRACT statements.

Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
SUBTRACT {value-1 | identifier-1} FROM {identifier-2 | identifier-3}... [GIVING {identifier-4 | identifier-5}...] [ROUNDED] [ON SIZE ERROR imperative-statement-1] [NOT ON SIZE ERROR imperative-statement-2] [END-SUBTRACT]. * Examples: SUBTRACT 10 FROM WS-TOTAL. SUBTRACT WS-AMOUNT FROM WS-BALANCE. SUBTRACT 5 10 15 FROM WS-VALUE1 WS-VALUE2. SUBTRACT WS-DISCOUNT FROM WS-PRICE GIVING WS-FINAL-PRICE. SUBTRACT WS-TAX FROM WS-GROSS GIVING WS-NET ROUNDED.

Practical Examples

Basic SUBTRACT Usage

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
* Basic SUBTRACT example IDENTIFICATION DIVISION. PROGRAM-ID. BASIC-SUBTRACT-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-TOTAL PIC 9(5) VALUE 1000. 01 WS-AMOUNT PIC 9(3) VALUE 250. 01 WS-RESULT PIC 9(5). 01 WS-COUNTER PIC 9(2) VALUE 10. PROCEDURE DIVISION. MAIN-LOGIC. DISPLAY "Original total: " WS-TOTAL * Basic subtraction SUBTRACT WS-AMOUNT FROM WS-TOTAL DISPLAY "After subtracting " WS-AMOUNT ": " WS-TOTAL * Subtraction with literal SUBTRACT 100 FROM WS-TOTAL DISPLAY "After subtracting 100: " WS-TOTAL * Subtraction with GIVING SUBTRACT 50 FROM WS-TOTAL GIVING WS-RESULT DISPLAY "Subtract 50 from " WS-TOTAL " giving: " WS-RESULT DISPLAY "Original total unchanged: " WS-TOTAL * Multiple subtractions SUBTRACT 10 20 30 FROM WS-COUNTER DISPLAY "Counter after multiple subtractions: " WS-COUNTER STOP RUN.

Explanation: This example demonstrates basic usage of the SUBTRACT statement. The program shows different ways to use SUBTRACT: subtracting a field value from another field, subtracting a literal value, using the GIVING clause to store results in a separate field without modifying the original, and performing multiple subtractions. The GIVING clause is particularly useful when you want to preserve the original values while performing calculations.

Financial Calculations with SUBTRACT

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
* Financial calculations with SUBTRACT example IDENTIFICATION DIVISION. PROGRAM-ID. FINANCIAL-SUBTRACT-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-GROSS-PAY PIC 9(6)V99 VALUE 2500.00. 01 WS-TAX-RATE PIC V999 VALUE 0.25. 01 WS-TAX-AMOUNT PIC 9(6)V99. 01 WS-INSURANCE PIC 9(5)V99 VALUE 150.00. 01 WS-RETIREMENT PIC 9(5)V99 VALUE 200.00. 01 WS-NET-PAY PIC 9(6)V99. 01 WS-TOTAL-DEDUCTIONS PIC 9(6)V99. PROCEDURE DIVISION. MAIN-LOGIC. DISPLAY "=== Payroll Calculation ===" DISPLAY "Gross Pay: $" WS-GROSS-PAY * Calculate tax amount MULTIPLY WS-GROSS-PAY BY WS-TAX-RATE GIVING WS-TAX-AMOUNT DISPLAY "Tax Amount: $" WS-TAX-AMOUNT * Calculate net pay by subtracting deductions SUBTRACT WS-TAX-AMOUNT FROM WS-GROSS-PAY GIVING WS-NET-PAY DISPLAY "After tax: $" WS-NET-PAY * Subtract insurance and retirement SUBTRACT WS-INSURANCE FROM WS-NET-PAY DISPLAY "After insurance: $" WS-NET-PAY SUBTRACT WS-RETIREMENT FROM WS-NET-PAY DISPLAY "After retirement: $" WS-NET-PAY * Calculate total deductions ADD WS-TAX-AMOUNT WS-INSURANCE WS-RETIREMENT GIVING WS-TOTAL-DEDUCTIONS DISPLAY "Total Deductions: $" WS-TOTAL-DEDUCTIONS * Verify calculation SUBTRACT WS-TOTAL-DEDUCTIONS FROM WS-GROSS-PAY GIVING WS-NET-PAY DISPLAY "Final Net Pay: $" WS-NET-PAY STOP RUN.

Explanation: This example shows how to use SUBTRACT for financial calculations in a payroll system. The program calculates net pay by subtracting various deductions (tax, insurance, retirement) from gross pay. It demonstrates both direct subtraction (modifying the target field) and subtraction with GIVING (preserving original values). The program also shows how to verify calculations by performing the same operation in a different way.

Inventory Management with SUBTRACT

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
* Inventory management with SUBTRACT example IDENTIFICATION DIVISION. PROGRAM-ID. INVENTORY-SUBTRACT-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-ITEM-RECORD. 05 WS-ITEM-ID PIC 9(5). 05 WS-ITEM-NAME PIC X(20). 05 WS-QUANTITY-ON-HAND PIC 9(4). 05 WS-REORDER-POINT PIC 9(3). 05 WS-UNIT-PRICE PIC 9(5)V99. 01 WS-SALES-QUANTITY PIC 9(3). 01 WS-REMAINING-QTY PIC 9(4). 01 WS-REORDER-FLAG PIC X VALUE 'N'. 88 NEED-REORDER VALUE 'Y'. PROCEDURE DIVISION. MAIN-LOGIC. * Initialize item data MOVE 12345 TO WS-ITEM-ID MOVE "Widget A" TO WS-ITEM-NAME MOVE 500 TO WS-QUANTITY-ON-HAND MOVE 100 TO WS-REORDER-POINT MOVE 25.50 TO WS-UNIT-PRICE DISPLAY "=== Inventory Update ===" DISPLAY "Item: " WS-ITEM-ID " - " WS-ITEM-NAME DISPLAY "Quantity on hand: " WS-QUANTITY-ON-HAND DISPLAY "Reorder point: " WS-REORDER-POINT * Process sales transaction MOVE 75 TO WS-SALES-QUANTITY DISPLAY "Sales quantity: " WS-SALES-QUANTITY * Subtract sales from inventory SUBTRACT WS-SALES-QUANTITY FROM WS-QUANTITY-ON-HAND DISPLAY "Remaining quantity: " WS-QUANTITY-ON-HAND * Check if reorder is needed IF WS-QUANTITY-ON-HAND <= WS-REORDER-POINT SET NEED-REORDER TO TRUE DISPLAY "*** REORDER NEEDED ***" END-IF * Process another sale MOVE 50 TO WS-SALES-QUANTITY DISPLAY "Additional sales: " WS-SALES-QUANTITY SUBTRACT WS-SALES-QUANTITY FROM WS-QUANTITY-ON-HAND DISPLAY "Final quantity: " WS-QUANTITY-ON-HAND * Check reorder status again IF WS-QUANTITY-ON-HAND <= WS-REORDER-POINT SET NEED-REORDER TO TRUE DISPLAY "*** REORDER NEEDED ***" END-IF STOP RUN.

Explanation: This example demonstrates using SUBTRACT for inventory management. The program tracks item quantities and subtracts sales from the on-hand quantity. It shows how to use SUBTRACT in a business context where you need to maintain accurate inventory levels. The program also includes logic to check if reordering is needed based on the remaining quantity compared to a reorder point.

Complex SUBTRACT with Error Handling

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
* Complex SUBTRACT with error handling example IDENTIFICATION DIVISION. PROGRAM-ID. COMPLEX-SUBTRACT-EXAMPLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-ACCOUNT-BALANCE PIC 9(7)V99 VALUE 1000.00. 01 WS-WITHDRAWAL-AMOUNT PIC 9(7)V99. 01 WS-NEW-BALANCE PIC 9(7)V99. 01 WS-OVERDRAFT-FEE PIC 9(3)V99 VALUE 25.00. 01 WS-OVERDRAFT-FLAG PIC X VALUE 'N'. 88 OVERDRAFT-OCCURRED VALUE 'Y'. PROCEDURE DIVISION. MAIN-LOGIC. DISPLAY "=== Bank Account Withdrawal ===" DISPLAY "Current balance: $" WS-ACCOUNT-BALANCE * Test case 1: Normal withdrawal MOVE 500.00 TO WS-WITHDRAWAL-AMOUNT PERFORM PROCESS-WITHDRAWAL * Test case 2: Large withdrawal (potential overdraft) MOVE 1200.00 TO WS-WITHDRAWAL-AMOUNT PERFORM PROCESS-WITHDRAWAL * Test case 3: Multiple withdrawals MOVE 200.00 TO WS-WITHDRAWAL-AMOUNT PERFORM PROCESS-WITHDRAWAL MOVE 150.00 TO WS-WITHDRAWAL-AMOUNT PERFORM PROCESS-WITHDRAWAL STOP RUN. PROCESS-WITHDRAWAL. DISPLAY "Withdrawal amount: $" WS-WITHDRAWAL-AMOUNT * Subtract withdrawal from balance SUBTRACT WS-WITHDRAWAL-AMOUNT FROM WS-ACCOUNT-BALANCE ON SIZE ERROR DISPLAY "*** OVERDRAFT DETECTED ***" SET OVERDRAFT-OCCURRED TO TRUE * Add back the withdrawal and apply overdraft fee ADD WS-WITHDRAWAL-AMOUNT TO WS-ACCOUNT-BALANCE SUBTRACT WS-WITHDRAWAL-AMOUNT FROM WS-ACCOUNT-BALANCE SUBTRACT WS-OVERDRAFT-FEE FROM WS-ACCOUNT-BALANCE DISPLAY "Overdraft fee applied: $" WS-OVERDRAFT-FEE NOT ON SIZE ERROR DISPLAY "Withdrawal processed successfully" END-SUBTRACT DISPLAY "New balance: $" WS-ACCOUNT-BALANCE DISPLAY "---".

Explanation: This example shows complex SUBTRACT usage with error handling in a banking application. The program processes withdrawals and handles overdraft situations using the ON SIZE ERROR clause. When an overdraft occurs (negative balance), the program applies an overdraft fee. The END-SUBTRACT statement properly terminates the complex SUBTRACT statement with error handling. This demonstrates how to use SUBTRACT in real-world applications where error conditions must be handled gracefully.

Best Practices and Considerations

Important Considerations

  • Use GIVING clause to preserve original values
  • Handle overflow conditions with ON SIZE ERROR
  • Use ROUNDED for decimal precision control
  • Ensure proper field sizes for results
  • Consider using END-SUBTRACT for complex statements

Advantages

  • Supports multiple operand operations
  • Provides overflow error handling
  • Supports decimal arithmetic
  • Can preserve original values with GIVING
  • Standard COBOL arithmetic operation

Limitations

  • May cause overflow with large numbers
  • Requires proper field size planning
  • Can be complex with multiple operands
  • May not handle all data types
  • Requires careful decimal precision management

Best Practices

  • • Use GIVING clause when you need to preserve original values
  • • Always handle overflow conditions with ON SIZE ERROR
  • • Use ROUNDED for decimal precision control
  • • Plan field sizes carefully to avoid overflow
  • • Use END-SUBTRACT for complex statements with error handling

Test Your Knowledge

1. What is the primary purpose of the SUBTRACT statement in COBOL?

  • To add numbers together
  • To perform arithmetic subtraction operations
  • To multiply numbers
  • To divide numbers

2. Which of the following is a valid SUBTRACT statement format?

  • SUBTRACT 10 FROM WS-TOTAL
  • SUBTRACT WS-TOTAL FROM 10
  • SUBTRACT 10 TO WS-TOTAL
  • SUBTRACT WS-TOTAL TO 10

3. What happens when you use SUBTRACT with multiple FROM operands?

  • Only the first operand is subtracted
  • The value is subtracted from each target field
  • An error occurs
  • The operation is ignored

4. What is the purpose of the GIVING clause in SUBTRACT?

  • To specify the source of the subtraction
  • To specify a separate result field
  • To indicate the end of the statement
  • To specify rounding options

5. Can the SUBTRACT statement handle decimal arithmetic?

  • No, only integer arithmetic
  • Yes, with proper decimal field definitions
  • Only in some implementations
  • Only with specific compiler options