Easytrieve Loops — Control Flow Overview

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.

Progress0 of 0 lessons

Loop Pattern Catalog

Choose the right loop pattern
PatternFormWhen to use
Guarded processingDO WHILE conditionZero iterations valid
Process then testDO UNTIL conditionAt least one pass required
Fixed count NDO WHILE CTR LT N + incrementArray slots, repeat N times
File driverWHILE NOT EOF or UNTIL EOF + GETSequential batch input
Search arrayDO WHILE IDX LE MAX AND NOT FOUNDSequential table scan
Nested matricesOuter WHILE + inner WHILETwo-dimensional INDEX walks

Unified Loop Anatomy

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.

text
1
2
3
4
5
INIT-PART: IDX = 1 DO PART: DO WHILE IDX LE 12 BODY: TOTAL = TOTAL + AMOUNT PROGRESS: IDX = IDX + 1 END: END-DO

EOF Loop Family

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 and INDEX Loops

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.

Nesting Loops

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 and Loop Modularity

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.

Early Exit Strategies

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.

Loops With IF and SELECT

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.

Infinite Loop Prevention Checklist

  1. Body updates every variable in loop condition.
  2. GET or READ present when condition uses EOF.
  3. Counter increment not inside IF branch only—runs every iteration unless intentional.
  4. Compound AND conditions—each part can become false.
  5. Test with small max count before production full file.

Performance Notes

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.

Explain It Like I'm Five

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.

Exercises

  1. Implement same counted loop with WHILE LT and UNTIL GE—compare iterations.
  2. Write EOF read loop in two styles and test empty file.
  3. Nested loop with outer 3 inner 5—predict total body executions.
  4. Add FOUND flag early exit to array search loop.
  5. Refactor 20-line loop body into PERFORM and verify control variable.

Quiz

Test Your Knowledge

1. Easytrieve provides these loop forms:

  • DO WHILE and DO UNTIL only
  • DO FOR and WHILE
  • LOOP ENDLOOP
  • REPEAT UNTIL SQL

2. Counted loop idiom uses:

  • Counter variable in DO WHILE condition with increment in body
  • MACRO only
  • SORT keys
  • REPORT PAGE

3. Most common infinite loop cause:

  • Control variable never updated toward exit condition
  • END-DO present
  • Nested PERFORM
  • IF statement

4. Array iteration loop typically uses:

  • INDEX field with DO WHILE IDX LE max
  • REPORT TITLE
  • FILE only
  • No END-DO

5. Early loop exit without BREAK uses:

  • GOTO after END-DO or flag in WHILE condition
  • END-LOOP
  • RETURN from OS
  • CLOSE all files
Published
Read time16 min
AuthorMainframeMaster
Reviewed by MainframeMaster teamVerified: Broadcom Easytrieve 11.6 DO loop patternsSources: Broadcom Easytrieve 11.6 DO WHILE, DO UNTIL, END-DOApplies to: Easytrieve loop control flow patterns