Easytrieve WHILE Control Flow

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.

Progress0 of 0 lessons

DO WHILE Format

text
1
2
3
4
DO 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.

Top-Tested Behavior

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.

Counted DO WHILE

text
1
2
3
4
5
MON-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.

WHILE With Compound Conditions

text
1
2
3
4
DO 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.

DO WHILE Versus DO UNTIL — Side by Side

WHILE versus UNTIL
ScenarioChoose
Array may be empty—start index past lastDO WHILE
Prompt user at least onceDO UNTIL
Sum only if count positiveDO WHILE count GT 0
Process until valid code enteredDO UNTIL code valid

Manual Read DO WHILE NOT EOF

text
1
2
3
4
5
GET 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.

Nested DO WHILE

text
1
2
3
4
5
6
7
8
9
OUTER = 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.

GOTO Early Exit

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.

WHILE Inside IF

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.

Performance Notes

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.

Common WHILE Mistakes

  • Writing WHILE without DO—compile error.
  • Forgetting MON-IX increment—infinite loop.
  • Using UNTIL when zero iterations required.
  • END-IF instead of END-DO.
  • INDEX not set before first element reference.

Explain It Like I'm Five

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.

Exercises

  1. Write DO WHILE summing OCCURS 24 hourly rates.
  2. Show initial INDEX 99 with OCCURS 50—how many iterations?
  3. Add AND condition to skip STATUS X slots.
  4. Convert manual GET loop skeleton for secondary file.
  5. Nest 2×2 WHILE loops with correct END-DO order.

Quiz

Test Your Knowledge

1. Executable WHILE syntax in Easytrieve is:

  • DO WHILE condition ... END-DO
  • WHILE ... END-WHILE
  • IF WHILE
  • WHILE DO

2. DO WHILE evaluates condition:

  • Before each iteration
  • After each iteration only
  • Once at compile
  • Never

3. DO WHILE CTR LE 10 with CTR starting at 11 executes:

  • Zero times
  • Once
  • Ten times
  • Eleven times

4. Early loop exit without BREAK uses:

  • GOTO label after END-DO
  • END-WHILE
  • EXIT JOB
  • STOP LOOP

5. WHILE with NOT EOF is valid when:

  • Manual GET loop you control—not always JOB INPUT default
  • Inside FILE define
  • Only in JCL
  • Never in Easytrieve
Published
Read time16 min
AuthorMainframeMaster
Reviewed by MainframeMaster teamVerified: Broadcom Easytrieve Report Generator 11.6 DO WHILE, END-DOSources: Broadcom Easytrieve 11.6 DO Statement; WHILE parameterApplies to: Easytrieve DO WHILE top-tested loops