Skip to content

Control Flow

Chloë Strainge edited this page Oct 14, 2026 · 1 revision

Control flow

Conditional values

if is both a statement and a value expression. Conditions use the language's truthiness rules. Braces are required.

let $stock = 3
let $label = if $stock == 0 { "sold out" }
    else if $stock < 5 { "limited" }
    else { "available" }
echo $label
limited

Only the selected body runs. Branches create scopes. An if without a selected branch or fallback produces (). Valid syntax is required in all branches.

for and destructuring

for ($name, $minutes) in [["stretch", 5], ["walk", 20]]
{
    echo $name $minutes
}
let $visits = 0
for _ in 0..3 { $visits = $visits + 1 }
echo $visits
stretch 5
walk 20
3

_ discards a binding. Flat destructuring requires the exact item length. A hash can use the same two-position pattern. See iteration for source snapshots and custom iterators.

Repetition

let $remaining = 2
while $remaining > 0 { $remaining = $remaining - 1 }
until $remaining == 2 { $remaining = $remaining + 1 }
loop
{
    $remaining = $remaining + 1
    if $remaining == 3 { continue }
    break
}
echo $remaining
4

while repeats while its condition is true; until repeats while false. Both check before the first body. loop has no condition.

break exits the innermost loop in the current execution frame. continue starts its next iteration. Neither accepts a value or a label, and a called function cannot break its caller's loop. Scope unwinding and finally still apply.

return leaves the current function. Use error handling when recovery, rather than loop control, is needed.


Related: Match and type guards · Numbers and logic

Clone this wiki locally