10 · while, break, continue & loop else
Run until a condition fails, bail out early with break, skip bad data with continue, and master the rare loop/else clause.
`while` loops run for an *unknown* number of iterations determined at runtime — they're the natural fit for convergence checks and event-driven loops.
Without this:
Without `while`, you can't implement gradient descent termination (stop when loss stops improving), retry logic, or any loop whose count depends on the data.
Use while condition: when the number of iterations is not known in advance. The body runs as long as the condition is truthy; once it becomes falsy, the loop ends and execution continues after it. for is for a known collection; while is for an unknown duration.
Python (in browser)
Two exit conditions: loss plateau (`abs(delta) <= tol`) OR safety cap (`iteration >= max_iter`). Real training loops always include both — never rely on convergence alone.
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
break exits the innermost loop immediately — the rest of that iteration and all future iterations are skipped. It's the right tool when you've found what you were looking for and don't need to keep searching.
Python (in browser)
`break` exits after the first match. Without it, the loop would scan all remaining checkpoints unnecessarily.
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
continue skips the rest of the current iteration and jumps to the next. Use it to filter out bad samples in a data pipeline without deeply nesting the processing logic inside an if block.
Python (in browser)
`val != val` is the pure Python way to detect `float('nan')` — NaN is the only value that is not equal to itself. In real code you'd use `math.isnan(val)`, but the identity trick is worth knowing.
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
The `for/else` clause is intentionally non-runnable here — read it to understand the *concept*. The `else` block fires only on natural loop exhaustion, never after a `break`.
When does the `else` clause of a `for` loop execute?
- `while cond:` runs until the condition is falsy — pair it with a `max_iter` safety cap to prevent infinite loops.
- `break` exits the current loop immediately; `continue` skips to the next iteration. Both work in `for` and `while`.
- The `for/while ... else` clause runs only when the loop exits naturally (no `break`) — it's an elegant replacement for a boolean 'found' flag.
Gradient descent termination uses `while abs(loss - prev_loss) > tol and iteration < max_iter` — exactly the two-condition pattern above. Early stopping callbacks call `break` inside the epoch loop when validation loss stops improving. Data-cleaning pipelines use `continue` to skip corrupted rows without restructuring the entire loop body.
If you remove it: Without `while` and `break`, gradient descent would have to run a fixed number of steps with no way to stop early when convergence is achieved — wasting compute. Without `continue`, outlier filtering requires nesting all the processing logic inside an `if` block, making the loop harder to read and extend.