← Fundamentals
Fundamentals

While loops and termination

Which upload state advances on a pass, and what remains for the next request?

A repeated test and the state changed by its body determine how a loop makes progress.

Outcome

Trace repeated tests, fresh input and retained work to make a finite loop stop at the intended boundary.

Terminology (3)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Pre-test loop
A loop whose condition is tested before each body execution, including the first.
Progress
A state change that moves the computation toward a boundary or exhaustion.
Pending work
Already obtained work that has not yet been consumed and must survive a call boundary.

Progress toward a stopping boundary

A pre-test loop evaluates its condition before admitting each body execution. Termination needs a reason that the repeated work eventually stops: a bounded counter advances, a remaining quantity decreases, or a finite input is exhausted. State what changes and which boundary that change approaches.

Input progress and completed work can require separate state. Reading a chunk advances the input cursor, while consuming part of it may leave work for a later call. Trace both states when deciding whether a loop should continue, stop or hand control back to its caller.

01Admit the next step

Count the scheduled work before starting the preview

An upload-buffer helper estimates bytes from a known chunk schedule, previews a bounded stream and fills successive request buffers. Empty chunks are valid data; a chunk larger than one request must leave its unused bytes for the next.

Before estimating the scheduled bytes, check how many steps a loop admits. The helper has three steps left; each body completes one. Predict the final done and left counts, including the state seen by the next test.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let left = 3;
let done = 0;

while (left > 0) {
  done += 1;
  left -= 1;
}

console.log("done", done, "left", left);
What prints?
TypeScript · terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
npx tsc main.ts --noEmit --skipLibCheck --target es2022 --module nodenext --types node && node main.ts

Save one complete example at a time under the shown filename in a fresh folder. Run the shown command. The browser lab supplies its editor, input fixtures and runner.

02Refresh the input

Check whether the preview’s readiness is still current

The preview proposes saving its readiness once before entering a loop. This probe also caps execution at three turns, so the result can be inspected without an unbounded run. Predict whether changing remaining updates the saved ready value.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let remaining = 2;
const ready = remaining > 0;
let turns = 0;

while (ready && turns < 3) {
  remaining -= 1;
  turns += 1;
}

console.log("turns", turns, "remaining", remaining);
Which final pair prints?

03Track input and units

Keep advancing through empty chunks

The next preview input is an empty chunk followed by six units of work. Trace both the input cursor and accumulated units: does the empty chunk end this finite input sequence?

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const chunks = [0, 6];
let at = 0;
let units = 0;

while (at < chunks.length) {
  units += chunks[at];
  at += 1;
}

console.log("chunks", at, "units", units);
Which report prints?

04Practice

Repair the upload-buffer helper

Input is preview limit, capacity A, capacity B, chunk count, then nonnegative chunk sizes. Supplied simulation inputs are nonnegative integers with at most 32 chunks of at most 512 bytes each, limit 0–32 and capacities 0–16,384. The declared chunk count matches the supplied values; decoding and the reader are provided.

The preview reports its total and returned-chunk count. The final consumer reports used, pending and returned chunks after each of two request-buffer windows. The reader returns absence at EOF; a zero-sized chunk is valid data.

Start the lab

Apply an inclusive bound, refresh the preview’s input, then preserve partially consumed work between windows.

  1. easy · F-WHILE-V4-1 Include every scheduled chunk
  2. medium · F-WHILE-V4-2 Preview fresh chunks
  3. hard · F-WHILE-V4-3 Carry unfinished work
Sources

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.