← Back to AILP Home

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:

Chapters

  1. Overview — the mental model, the truth table, and a first end-to-end example.
  2. then and end semantics — the completion decision in detail, including the zero-iteration case.
  3. break routing — why break always lands in end, and loan release on the break path.
  4. continue — why continue keeps then reachable, and the all-continue corner case.
  5. Optional blocks — then-only, end-only, and bare when, and how the fall-through value is observed.
  6. Nested when — nesting in body / then / end, innermost binding, and labeled escape through when.
  7. DiagnosticsARIA-WHEN-001..003, what triggers each, and how to fix them.
  8. when vs while — when to reach for when over a hand-rolled while with 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
};