Easytrieve ADD Statement

Running totals power batch reporting—department gross pay, record counts, error tallies. The ADD statement increments a numeric accumulator in one readable line: ADD GROSS TO DEPT-TOTAL. No temporary variable required when grammar allows ADD value TO field directly. COBOL programmers recognize ADD TO immediately; assignment enthusiasts write DEPT-TOTAL = DEPT-TOTAL + GROSS instead. Both express accumulation when target and source types align. ADD belongs in Activity procedural logic inside JOB loops—not in Library, not in REPORT TITLE. Misplacing ADD before fields initialize yields wrong totals; forgetting to zero accumulators at control break boundaries poisons subtotals. This page teaches ADD syntax variants, contrast with + operator and SUM statement, implied decimal behavior on packed fields, counter increments ADD 1 TO REC-COUNT, interaction with IF filters, overflow risks, and testing patterns auditors use to reconcile ADD totals against SUM report lines.

Progress0 of 0 lessons

Basic ADD Syntax

Core form adds one value into one target field. ADD source TO target increases target by source amount. Source may be field name or literal. Some grammars allow ADD A B TO C adding sum of A and B into C—verify Language Reference for multi-operand forms on your release. ADD executes immediately when reached—unlike SUM which defers to report writer timing on CONTROL breaks.

text
1
2
3
4
5
JOB INPUT PAYROLL IF GROSS GT 0 ADD GROSS TO DEPT-TOTAL ADD 1 TO EMP-COUNT END-IF

ADD Versus Assignment With +

ADD compared to assignment
FormExampleNotes
ADD statementADD GROSS TO TOTALStatement keyword TO
Assignment +TOTAL = TOTAL + GROSSExpression on right side
ADD literalADD 1 TO LINE-COUNTCommon counter increment
SUM on REPORTSUM GROSS on CONTROLReport writer not ADD

Initializing Accumulators

Working storage accumulators need zero before loop unless VALUE on DEFINE sets initial state. At department control break, reset DEPT-TOTAL when logic tracks per-dept subtotals manually—report SUM handles break totals declaratively but manual ADD paths require explicit MOVE 0 TO DEPT-TOTAL or ADD negative prior total patterns avoided by clear reset. JOB activity RESET on W fields may reinitialize at activity start per variable lifetime rules.

Numeric Types and Implied Decimal

ADD GROSS TO TOTAL when both P 2 adds at cents scale. Adding P 2 to N 5 may convert per rules—test in dev. Overflow when TOTAL exceeds field size causes size error or wrap—size TOTAL wide enough for maximum run. ADD to packed field with invalid input data from file causes data exception—validate with testing functions before ADD on untrusted amounts.

ADD for Counters

ADD 1 TO REC-COUNT increments processed records. ADD 1 TO ERR-COUNT when validation fails. Counters feed trailer records and DISPLAY diagnostics. Integer N or P 0 fields suit counts; do not use implied decimal P 2 for counts unless intentional. Compare REC-COUNT to trailer control field at EOF for reconciliation.

text
1
2
3
4
5
6
7
JOB INPUT INFILE ADD 1 TO REC-COUNT IF INVALID-REC EQ Y ADD 1 TO BAD-COUNT ELSE ADD AMT TO GOOD-TOTAL END-IF

ADD Inside IF and PROC

ADD only runs when control reaches it—guard with IF DEPT EQ 911 before ADD to dept accumulator. PERFORM CALC PROC may contain ADD steps reused across branches. Do not ADD inside report AFTER-BREAK when SUM already maintains same total—double-count risk unless intentional cross-check.

ADD Versus SUM and TOTAL

SUM on REPORT declarative line accumulates during report processing aligned with CONTROL field breaks—formatted on break lines automatically. ADD in JOB builds working totals for WRITE to extract file, DISPLAY, or conditional logic before PRINT. Some programs ADD in JOB for audit and rely on SUM for printed subtotals—totals must match or document intentional difference.

Common ADD Mistakes

  • ADD before accumulator initialized to zero.
  • ADD to alphabetic field expecting concatenation.
  • Double accumulation with both ADD and SUM on same amount unknowingly.
  • Field too small for running sum magnitude.
  • ADD on edited display field not numeric storage.
  • Forgetting to reset subtotal accumulator at break boundary in manual logic.

Explain It Like I'm Five

ADD is putting more coins in a jar. The jar already had some coins; ADD drops in the new amount and the jar now has the old plus the new. Saying TOTAL = TOTAL + GROSS is the same idea with different words. SUM on the report is a special jar the report machine fills when the department name changes on the page.

Exercises

  1. Write ADD GROSS TO DEPT-TOTAL guarded by IF GROSS GT 0.
  2. Rewrite same logic using assignment with + operator.
  3. Explain when SUM replaces manual ADD for department subtotals.
  4. Add REC-COUNT increment and trailer reconciliation pseudocode.
  5. Describe overflow risk when TOTAL field too narrow.

Quiz

Test Your Knowledge

1. ADD GROSS TO DEPT-TOTAL means:

  • Add GROSS into DEPT-TOTAL accumulator
  • Replace DEPT-TOTAL with GROSS only
  • Compare GROSS to DEPT-TOTAL
  • Open file GROSS

2. ADD statement versus TOTAL = TOTAL + AMT assignment:

  • Both increment accumulator—syntax differs
  • ADD only works on alphabetic
  • Assignment illegal for totals
  • ADD is report only

3. ADD on packed P 2 fields:

  • Respects implied decimal scale
  • Ignores decimals always
  • Converts to alphabetic
  • JCL only

4. ADD in JOB loop over input file:

  • Builds running totals per department or grand total
  • Sorts file
  • Defines FILE
  • Closes report

5. ADD versus SUM on REPORT:

  • ADD in JOB logic; SUM declarative on report breaks
  • Identical
  • SUM in JOB only
  • ADD in TITLE only
Published
Read time14 min
AuthorMainframeMaster
Reviewed by MainframeMaster teamVerified: Broadcom Easytrieve 11.6 ADD statement in Activity procedural logicSources: Broadcom Language Reference ADD statement, Activity programmingApplies to: Easytrieve ADD statement for accumulators