Skip to content

Loops — for, while, do...while, and labelled break

Level: 101 · for anyone who has written a loop in another language

Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.

One line: do...while always runs its body once, a plain break leaves only the innermost loop, and break outer leaves every loop out to the label — a label even lets break leave a plain block, though continue must name a loop.

Keywords: for, while, do...while, break, continue, label

What the finished page will answer

  • How many times does do { ... } while (false) run its body, and how many times does while (false) { ... }?
  • In two nested loops, which loop does a plain break leave, and which does break outer leave?
  • Why does a continue placed before i++ in a while loop spin forever, when the same continue in a for loop still runs the update?
  • What does break do inside a labelled block that is not a loop, and why is continue to that label a SyntaxError?
  • What does for (let i = 0, j = 9; i < j; i++, j--) visit, and what does for (;;) do?

Examples it will need

  • loops_break_and_labels_js.js — the pairs a nested search visits with break, with break outer and with a labelled block
  • loops_continue_in_while_js.js — continue in a for loop beside the same continue in a while loop, the second stopped by a guard after 20 turns
  • loops_continue_label_sh.sh — the SyntaxError for continue naming a block label, and for a label that does not exist

See also

Sources to start from