Repository navigation
Control Flow
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 ($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.
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
Begin here
Values and expressions
- Language basics
- Variables and scope
- Numbers and logic
- Strings and paths
- Arrays
- Hash maps
- Ranges and iteration
- Control flow
- Match and type guards
Functions and types
- Functions and calls
- Closures and function types
- Methods and properties
- Structs
- Enums
- Named types and unions
- Conversions
- Types and properties
Shell and modules
- Pipelines
- Pipeline splits
- Pipeline formats
- Commands and exit status
- Redirection
- Environment and exported variables
- Modules and visibility
- Nested modules and directory packages
- Conditional imports and declarations
- Errors and cleanup
- Supervised processes and terminals
Library guides
- Standard library
- The implicit prelude
- Bytes, files, readers, and writers
- Explicit item streams
- JSON documents and typed mapping
- Sockets, DNS, and TLS
- HTTP clients
- HTTP servers
- Framing, server-sent events, and JSON-RPC
- Terminal colors, styles, and geometry
- Build an application console
Interactive use