When / Then / End Cookbook
This subsection is the canonical v0.39.6 guide for Nitpick's when loop and its
two completion clauses, then and end. It complements the
loops cookbook: when is a loop, so the shared block,
scope, break/continue, label, and invariant rules from that section all
apply unchanged here. This guide focuses on what is unique to when — the
completion decision.
The v0.39.x cycle locked the shipped behavior of the construct:
when (cond) { body }is a condition-driven loop, exactly likewhile.- an optional
then { ... }block runs when the loop exits cleanly — the body completed at least one full iteration and the condition then went false. - an optional
end { ... }block runs when the loop is cut short — abreakfired, or the loop ran zero clean iterations. continuedoes not route toend: it re-tests the condition without marking completion, so a later clean pass still reachesthen.then/endare independent and optional; either, both, or neither may appear, and they bind to the nearest unmatchedwhen.- the parser rejects malformed clauses with
ARIA-WHEN-001..003.
Chapters
- Overview — the mental model, the truth table, and a first end-to-end example.
- then and end semantics — the completion decision in detail, including the zero-iteration case.
- break routing — why
breakalways lands inend, and loan release on the break path. - continue — why
continuekeepsthenreachable, and the all-continue corner case. - Optional blocks — then-only, end-only, and bare
when, and how the fall-through value is observed. - Nested when — nesting in body / then / end, innermost binding,
and labeled escape through
when. - Diagnostics —
ARIA-WHEN-001..003, what triggers each, and how to fix them. - when vs while — when to reach for
whenover a hand-rolledwhilewith a completion flag.
At a glance
func:failsafe = int32(tbb32:err) {
exit 1;
};
func:main = int32() {
int32:i = 0i32;
int32:result = 0i32;
when (i < 3i32) {
i = i + 1i32;
} then {
result = 2i32; // ran: loop exited cleanly
} end {
result = 3i32; // skipped: no break, and iterations happened
}
exit result; // 2
};