Developers search WHILE; Easytrieve executes DO WHILE. The WHILE keyword is not a standalone statement—it introduces the top-tested loop form of DO, closed by END-DO, driven by conditional expressions the product evaluates before each pass. Zero iterations is valid: when INDEX already exceeds OCCURS or a guard flag is off before entry, DO WHILE skips the body entirely—behavior DO UNTIL cannot provide because UNTIL always runs once. Control flow WHILE chapter covers array walks, counted summation, compound AND conditions, NOT EOF in manual read loops, nesting with inner WHILE on columns, and GOTO exit when linear SEARCH finds a target early. Read DO chapter for loop family context; read statements/while for compile-level syntax details.
1234DO WHILE conditional-expression statement-1 statement-2 END-DO
conditional-expression uses relational, logical AND/OR, field class, EOF, and other IF-legal tests. Body statements indent for readability; compiler pairs DO with END-DO. Missing END-DO is a hard error.
Evaluation order: test condition, if false exit loop, if true run body, return to test. Initial false means body never executes—critical when SLOT-IX starts at 51 for OCCURS 50. DO UNTIL would still process slot 51 once incorrectly if you chose wrong form.
12345MON-IX = 1 DO WHILE MON-IX LE 12 ADD HOURS(MON-IX) TO WS-ANNUAL-HOURS MON-IX = MON-IX + 1 END-DO
Classic twelve-month accumulation. MON-IX is INDEX on HOURS OCCURS 12. Inclusive LE 12 includes twelfth month. Use consistent INDEX field named in DEFINE OCCURS clause.
1234DO WHILE IDX LE MAX AND STATUS(IDX) NE 'X' PROCESS-ACTIVE-SLOT IDX = IDX + 1 END-DO
Loop until index passes max or hits excluded status—AND short-circuits per language rules. Ensure IDX still increments when STATUS X skipped via IF inside body instead of AND if logic differs.
| Scenario | Choose |
|---|---|
| Array may be empty—start index past last | DO WHILE |
| Prompt user at least once | DO UNTIL |
| Sum only if count positive | DO WHILE count GT 0 |
| Process until valid code entered | DO UNTIL code valid |
12345GET AUXFILE DO WHILE NOT EOF AUXFILE PROCESS-AUX-RECORD GET AUXFILE END-DO
Secondary file manual loop—GET before DO, GET at end of body. JOB INPUT on primary file remains separate. Do not nest conflicting GET patterns without understanding file context buffers.
123456789OUTER = 1 DO WHILE OUTER LE ROW-MAX INNER = 1 DO WHILE INNER LE COL-MAX UPDATE-GRID INNER = INNER + 1 END-DO OUTER = OUTER + 1 END-DO
Inner END-DO required before outer increment. Reset INNER each outer pass. Complexity grows quickly—extract PROC for UPDATE-GRID when body exceeds ten lines.
When TARGET found in linear OCCURS scan, GOTO AFTER-LOOP label following END-DO avoids useless iterations. Label must be in same activity scope. Document why GOTO chosen over restructuring— future maintainers appreciate comments.
IF DEPT EQ 903 runs DO WHILE only for operations department array processing. END-DO before END-IF. Other departments skip loop entirely—zero iterations via outer IF, not inner WHILE.
DO WHILE on large OCCURS with O(1) body is fine for thousands of slots. Linear scan millions of slots for lookup should use TABLE SEARCH instead. Profile before optimizing—clarity first on modest tables.
DO WHILE is a bouncer at a club door who checks your ticket before each song. No ticket— you never dance even once. Each song you dance, the bouncer checks again. END-DO is when the club closes. If you find your friend on the first song, GOTO is leaving early through the side door instead of waiting for every song to finish.
1. Executable WHILE syntax in Easytrieve is:
2. DO WHILE evaluates condition:
3. DO WHILE CTR LE 10 with CTR starting at 11 executes:
4. Early loop exit without BREAK uses:
5. WHILE with NOT EOF is valid when: