Easytrieve DO Control Flow

Repetition without copy-paste is what DO delivers. Easytrieve offers DO WHILE and DO UNTIL—both closed by END-DO, both driven by conditional expressions, neither a fixed-repeat DO 100 TIMES. Control flow chapter DO focuses on how loops live inside JOB: walking OCCURS arrays with INDEX, summing slots until a cap, nested row and column iteration, and retry-until-valid patterns that need at least one attempt (UNTIL). WHILE and UNTIL have dedicated chapter pages; here you learn loop architecture—when to nest, how to increment counters, why infinite loops happen, and how GOTO after END-DO substitutes for missing BREAK. Sequential GET loops are usually JOB INPUT, not DO; do not force file reads into DO when the activity already iterates records.

Progress0 of 0 lessons

DO WHILE Versus DO UNTIL in Practice

Loop form selection
FormTest whenMinimum iterationsExample use
DO WHILETopZeroINDEX already past end—skip array walk
DO UNTILBottomOneRead screen field then validate

Counted Loop Pattern

text
1
2
3
4
5
CTR = 1 DO WHILE CTR LE 12 ADD AMT(CTR) TO WS-YEAR-TOTAL CTR = CTR + 1 END-DO

Twelve pay periods summed without twelve ADD statements. CTR is INDEX or numeric working storage. Initialize before DO; increment inside; condition uses LE for inclusive upper bound. Off-by-one bugs use LT 12 instead of LE 12—drops last period.

Array Walk With INDEX

text
1
2
3
4
5
6
7
8
9
10
DEFINE SLOT-IX W 3 N 0 DEFINE FLAGS W 1 A OCCURS 50 INDEX SLOT-IX SLOT-IX = 1 DO WHILE SLOT-IX LE 50 IF FLAG(SLOT-IX) EQ 'Y' ADD 1 TO ACTIVE-CNT END-IF SLOT-IX = SLOT-IX + 1 END-DO

Set SLOT-IX before referencing FLAG element. Each pass tests one slot. IF inside DO filters without separate loops. ACTIVE-CNT aggregates result after full walk.

Nested DO for Row and Column

text
1
2
3
4
5
6
7
8
9
ROW-IX = 1 DO WHILE ROW-IX LE 5 COL-IX = 1 DO WHILE COL-IX LE 10 PROCESS-CELL COL-IX = COL-IX + 1 END-DO ROW-IX = ROW-IX + 1 END-DO

Inner END-DO closes column loop before outer ROW-IX increments. Five times ten iterations. Reset COL-IX inside outer body each row. Deep nesting beyond three levels suggests procedure extraction.

DO UNTIL Validation Pattern

text
1
2
3
4
DO UNTIL WS-CODE NUMERIC DISPLAY 'ENTER NUMERIC CODE' ACCEPT WS-CODE END-DO

Body runs at least once—user always sees prompt. UNTIL WS-CODE NUMERIC exits when class test passes. Batch jobs use UNTIL less than WHILE but SCREEN-style logic maps here. Online ACCEPT details vary by environment—pattern is bottom-tested retry.

Early Exit With GOTO

text
1
2
3
4
5
6
7
8
9
SLOT-IX = 1 DO WHILE SLOT-IX LE 100 IF KEY(SLOT-IX) EQ TARGET MOVE DESC(SLOT-IX) TO WS-RESULT GOTO DONE-WALK END-IF SLOT-IX = SLOT-IX + 1 END-DO DONE-WALK. CONTINUE

Linear search through OCCURS when TABLE not used. GOTO DONE-WALK after END-DO label skips remaining iterations when key found early. For large sorted data prefer TABLE SEARCH over DO linear walk—performance differs sharply.

DO With IF — Structure Rules

END-DO must close DO before END-IF closes outer IF when IF wraps DO. Correct nesting: IF ... DO ... END-DO END-IF. Placing END-IF before END-DO is a compile error. Indent consistently.

Infinite Loop Prevention

  • Every DO WHILE must change a variable in the condition somewhere in the body—or exit via GOTO.
  • Verify increment direction matches condition—IDX = IDX + 1 with WHILE IDX LT max.
  • Test empty array: WHILE may run zero times if INDEX starts above OCCURS—often correct.
  • QA with small OCCURS before production million-iteration mistake.

DO Versus JOB INPUT

Primary file processing uses JOB INPUT name which implies read loop. DO does not replace GET for sequential extracts. Use DO for working storage iteration, retry blocks, and algorithmic steps after record is already in buffer.

Common DO Mistakes

Loop pitfalls
MistakeFix
Missing END-DOPair each DO
INDEX not initializedMOVE 1 to INDEX before DO
WHILE when need one executionUse DO UNTIL
Reference array without setting INDEXSet INDEX each iteration

Explain It Like I'm Five

DO is doing something again and again. WHILE is checking the rules before each turn—if the music is still playing, dance another spin. UNTIL is dancing once then checking if the bell rang—so you always dance at least once. END-DO is when the music stops. INDEX is which numbered hopscotch square you jump on each turn. If you forget to move to the next square, you hop forever on the same one—that is an infinite loop.

Exercises

  1. Write DO WHILE summing ten OCCURS amounts with CTR.
  2. Convert to DO UNTIL when first slot must be read before test.
  3. Add nested DO 3×4 grid with ROW-IX and COL-IX.
  4. Insert GOTO early exit when TARGET found in array walk.
  5. Identify off-by-one error in DO WHILE CTR LT 12 versus LE 12.

Quiz

Test Your Knowledge

1. DO WHILE may execute how many times if condition starts false?

  • Zero
  • Exactly one
  • At least one
  • Ten

2. Counted loop idiom in Easytrieve uses:

  • Initialize counter; DO WHILE counter LT limit; increment in body; END-DO
  • DO 100 TIMES
  • FOR I = 1 TO 100
  • LOOP 100

3. Nested DO loops each need:

  • Their own END-DO
  • One shared END-DO
  • END-IF
  • END-PROC only

4. Walking OCCURS array slots typically uses:

  • DO WHILE INDEX LE max with INDEX = INDEX + 1
  • GET per slot
  • SORT
  • TABLE SEARCH per slot

5. Infinite DO loop most often caused by:

  • Condition variable never updated toward exit
  • Using END-DO
  • Nesting only two deep
  • Alphabetic fields
Published
Read time17 min
AuthorMainframeMaster
Reviewed by MainframeMaster teamVerified: Broadcom Easytrieve Report Generator 11.6 DO Statement, END-DOSources: Broadcom Easytrieve 11.6 DO Statement; DEFINE OCCURS INDEXApplies to: Easytrieve DO loops in JOB and procedure logic