DO UNTIL implements bottom-tested control flow: statements in the loop body run, then the product evaluates the UNTIL condition; while the condition remains false, another iteration begins. That guarantees at least one execution—unlike DO WHILE which may skip the body entirely when the entry test fails. UNTIL fits process-then-decide designs: read a record before testing EOF, display a screen field before checking validity, or apply a formula before testing convergence. Syntax is DO UNTIL conditional-expression, body statements, END-DO. The condition expresses what you wait for to become true to stop looping—DO UNTIL EOF means keep going until EOF is true. Beginners invert conditions when translating from WHILE NOT EOF to UNTIL EOF. This page teaches bottom-tested flow diagrams, sentinel and EOF patterns, validation-after-input, UNTIL with counters, nesting with WHILE, conversion guidelines between loop styles, and production pitfalls when empty files must not process a phantom record—where WHILE may be safer with restructured read logic.
Control enters loop body immediately on first encounter with DO UNTIL. After body completes, condition evaluates. False means loop again; true means fall through to statement after END-DO. Flowchart shape is body-first diamond-after. Contrast WHILE diamond-before body. Choosing UNTIL documents intent to reviewers: this block always runs once minimum—a useful comment substitute in legacy shops.
| Aspect | DO WHILE | DO UNTIL |
|---|---|---|
| Test timing | Before body | After body |
| Minimum iterations | Zero | One |
| Continues while | Condition true | Condition false |
| Empty file read-in-body | Often zero reads with pre-test EOF | One GET unless guarded |
Traditional pattern: DO UNTIL EOF MASTER, GET MASTER at top of body, process, END-DO. First GET runs before first EOF test—on empty file some layouts still set EOF immediately after GET, processing zero business records; verify EOF flag timing for your FILE type. Alternative: priming read before loop then DO WHILE NOT EOF with GET at end—WHILE variant avoids UNTIL empty-file nuance. Pick one pattern per shop and document EOF interaction with VB versus FB files.
1234567JOB INPUT MASTER DO UNTIL EOF MASTER GET MASTER IF NOT EOF MASTER PERFORM PROCESS-RECORD END-IF END-DO
Online and batch validation sometimes must display or compute before knowing success. DO UNTIL VALID EQ Y with body that sets VALID after edits mirrors at-least-once semantics. Batch analog: compute TAX, DO UNTIL TAX GE MIN-TAX OR RETRIES GT 3, adjust, increment RETRIES, END-DO. Control exits when condition true—either tax sufficient or retry cap hit. Cap check in UNTIL prevents infinite adjustment loops.
Less common than counted WHILE but valid: DO UNTIL CTR GE 10, body, CTR = CTR + 1, END-DO executes until CTR reaches 10—ten iterations if CTR starts 0 and increments each pass. Equivalence to DO WHILE CTR LT 10 depends on start value and increment placement. Prefer WHILE for counted loops unless bottom test documents stop-when-reached clearly.
DO WHILE NOT EOF parallels DO UNTIL EOF with read-in-body restructuring. DO WHILE IDX LE MAX parallels DO UNTIL IDX GT MAX. When converting, write truth table for boundary values—IDX equals MAX is common off-by-one failure. Test both forms with same data in dev before refactoring production proc.
Outer UNTIL inner WHILE appears when outer driver must process once per file section while inner skips zero detail lines. END-DO pairing must match nesting order. Mixed loops increase review burden— extract to PERFORM when possible. Screen programs rarely nest deep UNTIL; batch accumulators may nest UNTIL on control breaks with inner WHILE on array slots.
PERFORM VALIDATE-ROW inside DO UNTIL NOT EOF; procedure EXIT when fatal error sets ABEND flag and UNTIL condition includes ABEND EQ N. EXIT from PROC does not exit UNTIL loop unless PERFORM returns and loop condition updated. Separate procedure flow from loop flow in design docs.
DO UNTIL is do something, then ask are we done yet. You always do it at least once. Keep doing and asking until the answer is yes, we are done. DO WHILE asks first and might skip entirely. Pick UNTIL when the first turn always matters—like tasting soup once before deciding if it needs salt.
1. DO UNTIL always executes its body at least:
2. DO UNTIL continues while:
3. UNTIL fits control flow when:
4. DO UNTIL is closed by:
5. Sentinel-controlled input with read in body often uses: