Easytrieve Array Iteration

Iteration means visiting each array element in order—or until a condition stops the walk. Easytrieve has no FOR-I-IN-ARRAY syntax; you combine DO WHILE or DO UNTIL with INDEX increment and element references. Payroll sums twelve RATE slots, validation scans eighty character bytes for invalid digits, invoice jobs loop LINE-COUNT FILE occurrences to accumulate extended price. PERFORM PROC inside loops modularizes element logic when IF blocks grow beyond one screen. Nested loops iterate matrices: outer INDEX for department, inner for pay grade. Search loops compare each element to a key until match or exhaustion—sequential alternative to TABLE LOOKUP for tiny arrays. This page teaches loop templates, summing and counting patterns, early exit, backward walks, interaction with control breaks, performance notes for large OCCURS counts, and debugging infinite loops when INDEX never advances—common beginner mistake in first array JOB.

Progress0 of 0 lessons

Basic Forward Iteration Template

Canonical pattern works for working storage and FILE-backed arrays after COUNT is validated. Initialize INDEX to 1. Loop while INDEX less than or equal to limit—either OCCURS maximum or active COUNT from record. Body uses element operations. Increment INDEX at loop bottom. END-DO terminates. Place increment last to avoid off-by-one when combining with EXIT on last element special case.

text
1
2
3
4
5
6
TOTAL = 0 R-IDX = 1 DO WHILE R-IDX LE 12 TOTAL = TOTAL + RATE R-IDX = R-IDX + 1 END-DO

DO WHILE Versus DO UNTIL

Loop style comparison for arrays
LoopCondition testBest for
DO WHILE IDX LE MAXBefore bodyStandard forward walk, may run zero times if IDX starts above MAX
DO UNTIL IDX GT MAXAfter bodyAt least one pass when IDX starts at 1 and MAX ge 1
DO WHILE IDX LE COUNTBefore bodyVariable active elements from input COUNT

Choose based on whether zero iterations is valid when COUNT is zero. Invoice with zero lines should not enter body—DO WHILE IDX LE COUNT handles naturally when COUNT is 0 and IDX starts at 1.

Summing and Accumulating in Loops

Initialize accumulator scalar before loop. Inside loop add element to accumulator or apply business formula. Watch packed decimal overflow—field size on accumulator must hold sum of all elements. For weighted averages, accumulate numerator and denominator in separate scalars inside same loop. Control-break reports may reset accumulator at BEFORE-BREAK after printing group total from prior group array walk.

text
1
2
3
4
5
6
7
8
9
WS-TAX-TOTAL = 0 T-IDX = 1 DO WHILE T-IDX LE TAX-COUNT WS-TAX-TOTAL = WS-TAX-TOTAL + TAX-AMT T-IDX = T-IDX + 1 END-DO IF WS-TAX-TOTAL GT LIMIT PERFORM TAX-EXCEPTION END-IF

Search Loop Pattern

Sequential search sets FOUND flag false, walks INDEX from 1 to max, compares element key to search key, sets FOUND and exits loop on match. If loop completes without match, handle not-found path. For sorted arrays binary search reduces comparisons—see binary search tutorial. Arrays under twenty elements often use sequential search for simplicity. Store FOUND as A 1 field VALUE N.

text
1
2
3
4
5
6
7
8
9
FOUND = 'N' S-IDX = 1 DO WHILE S-IDX LE 50 AND FOUND EQ 'N' IF STATE-CODE EQ INPUT-STATE FOUND = 'Y' ELSE S-IDX = S-IDX + 1 END-IF END-DO

PERFORM Inside Loops

When element processing includes multiple IF branches, file writes, or table lookups, PERFORM PROCESS-ITEM keeps JOB INPUT readable. PROC uses current INDEX context—do not reset INDEX inside PROC unless intentional. Nested PERFORM depth limits apply per product; flatten if recursion-like patterns emerge. Report BEFORE-LINE can PERFORM with INDEX set from detail field mapping line number to array slot.

Nested Loop Iteration

Matrix processing uses outer loop on ROW-IDX and inner on COL-IDX. Outer DO WHILE ROW-IDX LE ROW-MAX contains inner DO WHILE COL-IDX LE COL-MAX. Reset COL-IDX to 1 at start of each outer iteration when inner walk must restart at first column. Total iterations is product of dimensions—OCCURS 100 inner inside OCCURS 100 outer is ten thousand passes; performance test before production month-end.

Partial Iteration With COUNT

Input records often carry ACTIVE-COUNT less than OCCURS maximum. Loop WHILE IDX LE ACTIVE-COUNT only. Validate ACTIVE-COUNT LE OCCURS before loop. Unused slots may retain garbage on FILE—do not read slots above COUNT even though loop could reach them if COUNT wrong.

Backward Iteration

Set IDX to OCCURS maximum, DO WHILE IDX GE 1, process, decrement IDX. Useful deleting trailing blank character slots or finding last non-zero amount. Combine with IF element NE ZERO to break early when trailing structure is sparse.

Character Buffer Walk

One-byte OCCURS iteration parses strings: CX = 1, DO WHILE CX LE 80 AND DELIM-FOUND EQ N, inspect CH, increment CX. Delimiter found sets flag and exits via AND condition on DO WHILE. Same iteration mechanics as numeric arrays—only element width differs.

Iteration With Control Breaks

Report jobs may walk employee month array only at detail time while control fields come from first element or summary in BEFORE-BREAK. Do not iterate entire array on every control break level—match business rule: line prints one element per detail record versus summary prints sum from loop in BEFORE-BREAK.

Performance Considerations

Large OCCURS with nested loops multiply CPU. TABLE LOOKUP or keyed search wins for repeated random access on big tables loaded once. Sequential loop wins for full pass summing. Avoid PERFORM file I/O inside innermost loop when batch time matters. DISPLAY inside loops floods SYSPRINT—remove before production.

Common Iteration Mistakes

  • Missing INDEX increment—infinite loop.
  • Increment before processing causing skip of first or last element.
  • Search loop incrementing INDEX even after match—double increment bug.
  • Nested loop wrong END-DO pairing—inner loop never closes.
  • Using OCCURS max when COUNT from file is authoritative.

Explain It Like I'm Five

Iteration is visiting every box in order. Start at box 1, do your chore, move to box 2, repeat until no boxes left. If you forget to move to the next box, you keep doing the same box forever. Two nested loops are walking every box on shelf 1, then every box on shelf 2, and so on. Searching is walking boxes until you find the one with your name sticker.

Exercises

  1. Write sum loop for OCCURS 6 amount array.
  2. Write search loop setting FOUND when code matches.
  3. Add COUNT guard ACTIVE LE 8 before loop on eight-slot FILE array.
  4. Refactor five-line loop body into PERFORM PROC.
  5. Write backward loop finding last non-blank character slot.

Quiz

Test Your Knowledge

1. Standard array walk loop structure:

  • Set INDEX to 1, DO WHILE INDEX LE max, increment INDEX
  • SORT only
  • GOTO without INDEX
  • REPORT TITLE

2. Nested array iteration needs:

  • Outer and inner INDEX with correct increment order
  • One INDEX only
  • No END-DO
  • JCL restart

3. PERFORM in array loop is used to:

  • Call reusable element-processing procedure
  • Replace INDEX
  • Skip OCCURS
  • Close VSAM

4. Searching array for a value usually:

  • Walks INDEX until match or end with IF and EXIT
  • Uses SORT keys only
  • Cannot be done
  • Requires SQL

5. Infinite array loop often caused by:

  • Forgetting to increment INDEX
  • VALUE on DEFINE
  • FILE FB
  • HEADING
Published
Read time15 min
AuthorMainframeMaster
Reviewed by MainframeMaster teamVerified: Broadcom Easytrieve 11.6 DO and INDEX iteration patternsSources: Broadcom Easytrieve 11.6 Language Reference DO, PERFORM, DEFINE INDEXApplies to: Easytrieve array iteration and loops