Easytrieve WHILE — DO WHILE Top-Tested Loops

Repetition is everywhere in batch Easytrieve: read until end of file, retry until a key is valid, scan an array index until a slot is empty. The language answers with DO WHILE—not a standalone WHILE statement as some indexes name the topic. Syntax is DO WHILE conditional-expression, one or more body statements, END-DO. The condition is evaluated before each pass; when false initially, the body never runs. That top-tested behavior differs from DO UNTIL, which always executes once before its bottom test. WHILE pairs with field relational conditions, file presence tests like NOT EOF, and compound AND/OR expressions. This page teaches DO WHILE format, contrast with UNTIL and IF, nesting, early exit with GOTO, counted-loop idioms, infinite-loop debugging, and valid placement in JOB, PROGRAM, and procedure blocks—everything a beginner needs before writing iterative maintenance logic beside sequential GET loops.

Progress0 of 0 lessons

Statement Format

text
1
2
3
4
5
6
JOB INPUT NULL NAME DO-EX-1 CTR = 0 DO WHILE CTR LT 10 CTR = CTR + 1 DISPLAY 'CTR=' CTR END-DO

Broadcom's introductory example sets CTR to zero, then DO WHILE CTR LT 10 runs while counter stays below ten. Each iteration increments CTR and DISPLAYs the value. When CTR reaches ten the condition fails before the next body entry and control falls through to the first statement after END-DO. The conditional expression uses the same grammar as IF—field relational, AND, OR.

DO WHILE Versus DO UNTIL

Loop forms compared
FormWhen condition is testedMinimum iterations
DO WHILEBefore body (top)Zero
DO UNTILAfter body (bottom)One

Choose WHILE when skipping the body entirely is correct—empty input file simulated on NULL, counter already at limit, permission flag false before first attempt. Choose UNTIL when the body must execute at least once—display prompt before reading validation result, or process then check EOF at bottom mirroring DO UNTIL NOT EOF patterns in some shops. Identical counted loops can use either form with careful condition placement; WHILE is more common for zero-or-more iteration.

END-DO Requirement

Every DO WHILE requires a matching END-DO. Omitting END-DO produces compile errors or attaches body statements to the wrong scope. Nested loops need one END-DO per DO—indentation in source is for humans; the compiler pairs by statement order. Comments between DO and END-DO do not break pairing.

Counted Loop Idiom

text
1
2
3
4
5
6
DEFINE IDX W 3 N VALUE 1 JOB INPUT NULL NAME COUNT-DEMO DO WHILE IDX LE 100 DISPLAY 'PROCESSING ROW ' IDX IDX = IDX + 1 END-DO

Easytrieve has no DO 100 TIMES syntax. Initialize IDX, test IDX LE 100, increment inside body. Forgetting IDX = IDX + 1 creates an infinite loop—classic beginner bug caught when SYSPRINT floods duplicate lines until the job is cancelled. Always verify the loop variable moves toward making the condition false.

File Processing With DO WHILE

text
1
2
3
4
5
6
7
JOB INPUT PAYROLL NAME PROCESS DO WHILE NOT EOF PAYROLL GET PAYROLL IF DEPT EQ 901 ADD GROSS TO WS-D901-GROSS END-IF END-DO

Many programs use implicit JOB INPUT iteration instead of explicit DO WHILE NOT EOF. When you code manual GET loops—multiple files or NULL drivers—DO WHILE NOT EOF guards each GET. Ensure GET runs inside the body and EOF flag updates before the top test on the next iteration. Mixing implicit JOB INPUT with an outer DO WHILE on the same file double-reads records.

Nested DO WHILE

text
1
2
3
4
5
6
7
8
9
10
11
12
13
DEFINE COUNT-1 W 3 N VALUE 0 DEFINE COUNT-2 W 3 N VALUE 0 DEFINE RESULT W 3 N VALUE 0 JOB INPUT NULL NAME NEST-DEMO DO WHILE COUNT-1 LT 10 COUNT-1 = COUNT-1 + 1 COUNT-2 = 0 DO WHILE COUNT-2 LT 10 COUNT-2 = COUNT-2 + 1 RESULT = COUNT-1 * COUNT-2 DISPLAY 'C1=' COUNT-1 ' C2=' COUNT-2 ' R=' RESULT END-DO END-DO

Outer loop increments COUNT-1; inner loop resets COUNT-2 each outer pass and runs a multiplication table style calculation. Complexity grows quickly—extract inner bodies to named PROC procedures when nesting exceeds two levels for supportability.

Early Exit With GOTO

Easytrieve lacks BREAK or LEAVE. When a search inside DO WHILE finds a match and should stop iterating, branch to a label immediately after END-DO:

text
1
2
3
4
5
6
7
8
9
10
11
DO WHILE IDX LE MAX IF TABLE-KEY(IDX) EQ TARGET GOTO FOUND-KEY END-IF IDX = IDX + 1 END-DO DISPLAY 'NOT FOUND' GOTO DONE FOUND-KEY. DISPLAY 'FOUND AT ' IDX DONE.

GOTO FOUND-KEY skips remaining iterations. Use descriptive label names and document why early exit is required—auditors flag unstructured GOTO without comments.

Compound Conditions

DO WHILE supports AND and OR like IF: DO WHILE CTR LT 100 AND STATUS EQ 'A' processes only active rows below the cap. Parentheses are not required for simple chains; complex logic may use intermediate W flags set in the body for clarity. Field series and file presence conditions are valid where grammar allows.

WHILE Versus IF for Single Pass

Do not wrap a single statement in DO WHILE when IF suffices. DO WHILE implies repetition. Conversely, copying the same IF block ten times is worse than DO WHILE with a counter. Choose the construct that matches iteration intent.

Common WHILE Mistakes

  • Coding WHILE without DO—invalid syntax.
  • Missing END-DO terminator.
  • Loop variable never updated—infinite loop.
  • Using DO UNTIL when zero iterations are needed on empty input.
  • Double-processing files with JOB INPUT plus manual DO WHILE GET.
  • Expecting BREAK keyword—use GOTO after END-DO label instead.

Explain It Like I'm Five

DO WHILE is a teacher saying keep doing your worksheet problems while you still have fewer than ten stars. Before each new problem the teacher checks your star count. If you already have ten, you do not get another problem at all—that is top-tested. END-DO is the bell ringing when the while rule is finished. If you find the answer early, you can jump to recess with GOTO instead of finishing every remaining problem.

Exercises

  1. Write DO WHILE that runs exactly zero times when counter starts at 10 and limit is LT 10.
  2. Convert the same logic to DO UNTIL and explain iteration count difference.
  3. Add nested DO WHILE printing a five-by-five multiplication grid.
  4. Implement early exit with GOTO when a key matches inside a scan loop.
  5. List three symptoms of an infinite DO WHILE in batch SYSPRINT.

Quiz

Test Your Knowledge

1. WHILE in Easytrieve is coded as:

  • DO WHILE condition ... END-DO
  • WHILE condition alone
  • WHILE ... END-WHILE
  • LOOP WHILE ... STOP

2. DO WHILE tests its condition:

  • Before each iteration (top-tested)
  • After each iteration only
  • Never
  • Only at compile time

3. DO WHILE versus DO UNTIL when condition starts false:

  • WHILE runs zero times; UNTIL runs at least once
  • Both run once
  • Both run forever
  • WHILE runs once

4. To exit a DO WHILE loop early when a condition is met inside:

  • GOTO label after END-DO
  • END-WHILE
  • BREAK keyword
  • STOP JOB only

5. Every DO WHILE must end with:

  • END-DO
  • END-IF
  • END-PROC
  • END
Published
Read time15 min
AuthorMainframeMaster
Reviewed by MainframeMaster teamVerified: Broadcom Easytrieve Report Generator 11.6 DO WHILE and END-DOSources: Broadcom Easytrieve 11.6 DO UNTIL and DO WHILE Statements, JOB ActivitiesApplies to: Easytrieve DO WHILE loops