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.
| Form | Test when | Minimum iterations | Example use |
|---|---|---|---|
| DO WHILE | Top | Zero | INDEX already past end—skip array walk |
| DO UNTIL | Bottom | One | Read screen field then validate |
12345CTR = 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.
12345678910DEFINE 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.
123456789ROW-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.
1234DO 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.
123456789SLOT-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.
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.
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.
| Mistake | Fix |
|---|---|
| Missing END-DO | Pair each DO |
| INDEX not initialized | MOVE 1 to INDEX before DO |
| WHILE when need one execution | Use DO UNTIL |
| Reference array without setting INDEX | Set INDEX each iteration |
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.
1. DO WHILE may execute how many times if condition starts false?
2. Counted loop idiom in Easytrieve uses:
3. Nested DO loops each need:
4. Walking OCCURS array slots typically uses:
5. Infinite DO loop most often caused by: