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.
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.
123456TOTAL = 0 R-IDX = 1 DO WHILE R-IDX LE 12 TOTAL = TOTAL + RATE R-IDX = R-IDX + 1 END-DO
| Loop | Condition test | Best for |
|---|---|---|
| DO WHILE IDX LE MAX | Before body | Standard forward walk, may run zero times if IDX starts above MAX |
| DO UNTIL IDX GT MAX | After body | At least one pass when IDX starts at 1 and MAX ge 1 |
| DO WHILE IDX LE COUNT | Before body | Variable 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.
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.
123456789WS-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
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.
123456789FOUND = '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
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.
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.
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.
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.
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.
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.
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.
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.
1. Standard array walk loop structure:
2. Nested array iteration needs:
3. PERFORM in array loop is used to:
4. Searching array for a value usually:
5. Infinite array loop often caused by: