Loops repeat control flow until a condition ends iteration. Easytrieve offers two loop verbs under DO: WHILE tests before each pass and may run zero times; UNTIL tests after each pass and runs at least once. Everything else—file reads until EOF, walking twelve month buckets, searching fifty table slots, nested department and employee processing—is built from those two forms plus END-DO terminator, counter variables, INDEX fields for arrays, PERFORM for modular bodies, and GOTO or flag variables for early exit. There is no DO 10 TIMES, no FOR-EACH collection iterator, no BREAK keyword. Beginners who understand this unified model write maintainable batch jobs instead of copying linear code for each record slot. This loops overview compares patterns side by side, presents selection flowchart guidance, covers nesting and performance, ties loops to IF and SELECT gating, and lists anti-patterns operations teams see in stuck REGION jobs—infinite DO WHILE from forgotten increment, double READ from mixed WHILE-UNTIL EOF styles, nested END-DO misalignment.
| Pattern | Form | When to use |
|---|---|---|
| Guarded processing | DO WHILE condition | Zero iterations valid |
| Process then test | DO UNTIL condition | At least one pass required |
| Fixed count N | DO WHILE CTR LT N + increment | Array slots, repeat N times |
| File driver | WHILE NOT EOF or UNTIL EOF + GET | Sequential batch input |
| Search array | DO WHILE IDX LE MAX AND NOT FOUND | Sequential table scan |
| Nested matrices | Outer WHILE + inner WHILE | Two-dimensional INDEX walks |
Every loop has four parts: initializer before DO, condition on DO line, body statements including progress toward exit, END-DO terminator. Missing any part breaks control flow. Initializer sets counter, INDEX, or relies on EOF flag. Body must move state—increment, GET, set FOUND. Condition must eventually become false or true per UNTIL semantics. END-DO closes scope for nested IF and inner DO.
12345INIT-PART: IDX = 1 DO PART: DO WHILE IDX LE 12 BODY: TOTAL = TOTAL + AMOUNT PROGRESS: IDX = IDX + 1 END: END-DO
Style A: DO WHILE NOT EOF, GET, process, END-DO—zero iterations on empty if EOF true before loop. Style B: DO UNTIL EOF, GET, IF NOT EOF process, END-DO—may GET once on empty. Style C: priming GET before DO WHILE NOT EOF with GET at loop end—classic COBOL-like flow adapted to Easytrieve. Pick one style per application; mixing styles within one program confuses maintainers and duplicates READ logic.
Array iteration is counted loop on INDEX field: DO WHILE IDX LE ACTIVE-COUNT with element access and IDX = IDX + 1. OCCURS maximum caps IDX range. Partial arrays use COUNT from record not full OCCURS. Character byte walks use same pattern with one-byte OCCURS. Link to array iteration tutorial for summing and search variants inside loop body.
Outer loop drives file or department; inner loop drives lines or employees. Each level needs own control variable and END-DO. Complexity grows as product of iteration counts—100 outer × 100 inner is ten thousand passes. Extract inner loop to PERFORM when body exceeds ten lines. Never share one INDEX between unrelated arrays in nested loops without resetting between uses.
PERFORM PROCESS-LINE inside DO WHILE NOT EOF keeps JOB INPUT readable. Procedure inherits loop context— current record buffer valid inside PERFORM. Avoid PERFORM that PERFORMs back into same loop with recursion-like patterns. Report BEFORE-LINE loops uncommon; accumulate in BEFORE-BREAK with WHILE on break array instead of WHILE on every print line when performance critical.
No BREAK. Options: set WHILE condition false via FOUND flag, GOTO after END-DO, EXIT from PERFORMed proc when search fails (proc EXIT does not exit outer DO unless combined with flag). Search loops: DO WHILE IDX LE MAX AND FOUND EQ N is clearest—FOUND Y stops WHILE on next test.
Loop body often IF validates then processes. Sort BEFORE may loop on input via activity driver not DO—distinction: DO loops explicit in proc; JOB INPUT implicit loop on file. Inside BEFORE proc, each record one pass—IF and SELECT not wrapped in DO unless processing array in one record. Understand implicit JOB loop plus explicit DO loop stacking.
DISPLAY inside tight loops floods output. PERFORM file I/O inside innermost nested loop multiplies I/O. TABLE LOOKUP may beat inner WHILE scan on large random access. Loops are correct first; optimize when profiling shows loop body hot path.
Loops are doing something again and again until a rule says stop. Easytrieve has two stop rules: check before starting each turn, or check after each turn. You always need to tell the loop when to stop by changing numbers or reading until the file ends. Nested loops are a loop inside a loop—like for each shelf, check every box on that shelf.
1. Easytrieve provides these loop forms:
2. Counted loop idiom uses:
3. Most common infinite loop cause:
4. Array iteration loop typically uses:
5. Early loop exit without BREAK uses: