Master Mainframe Technologies - COBOL, JCL, DB2, VSAM, CICS & More
ABEND Codes SQLCODEs File Status Interview Prep Contact
💻 COBOL

COBOL ADD Statement

Beginner 🕑 10 min read 👁 0 views

COBOL ADD Statement

The ADD statement sums two or more numeric values and stores the result.

Syntax

ADD identifier-1/literal TO identifier-2 [GIVING identifier-3]
ADD identifier-1 identifier-2 ... GIVING identifier-3
ADD CORRESPONDING group-1 TO group-2

Example Program

       IDENTIFICATION DIVISION.
       PROGRAM-ID. ADD-DEMO.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-NUM1        PIC 9(4) VALUE 1000.
       01 WS-NUM2        PIC 9(4) VALUE 2500.
       01 WS-NUM3        PIC 9(4) VALUE 500.
       01 WS-RESULT      PIC 9(5) VALUE 0.

       PROCEDURE DIVISION.
      * ADD TO (result stored in second operand)
           ADD WS-NUM1 TO WS-NUM2
           DISPLAY 'ADD TO: ' WS-NUM2

      * ADD GIVING (result stored in third operand)
           ADD WS-NUM1 WS-NUM3 GIVING WS-RESULT
           DISPLAY 'ADD GIVING: ' WS-RESULT

      * ADD with ON SIZE ERROR
           ADD 99999 TO WS-NUM1
               ON SIZE ERROR
                   DISPLAY 'Size error occurred!'
               NOT ON SIZE ERROR
                   DISPLAY 'Result: ' WS-NUM1
           END-ADD

           STOP RUN.

Expected Output

ADD TO: 3500
ADD GIVING: 01500
Size error occurred!

Key Points

  • ADD TO modifies the destination operand
  • ADD GIVING preserves source operands
  • ON SIZE ERROR handles overflow conditions
  • ROUNDED option performs rounding