A range loop counts through numbers.
to stops before the end value:
loop 0 to 5 |value|:
io.line([: [value]])
;
This produces 0, 1, 2, 3 and 4.
Use to & when the end value should be included:
loop 0 to & 5 |value|:
io.line([: [value]])
;
This also produces 5.
Start from zero
You can leave out the first 0.
loop to 5 |value|:
io.line([: [value]])
;
Change the step
Use by to set a signed step:
loop 0 to 10 by 2 |value|:
io.line([: [value]])
;
This produces 0, 2, 4, 6 and 8.
Count downward
Use a negative step:
loop 5 to 0 by -1 |value|:
io.line([: [value]])
;
This produces 5, 4, 3, 2 and 1.
The step's sign chooses direction. The bounds never do, so loop 5 to 0 visits nothing and loop 5 to & 0 by -1 includes 0. You do not need a reverse keyword.
Any range using F16, F32 or F64 needs an explicit float step:
loop 0.0 to 1.0 by 0.1 |amount|:
io.line([: [amount]])
;
The value binding uses the selected range type. The optional second binding counts visited iterations from zero and always has type I32, even when the range counter uses another exact type.
Fixed-width bounds follow the exact numeric domain rules. Steps are signed whole numbers from I8 through I64. An omitted step is one, and unsigned, Byte, Dec or float steps do not apply to an integer range. A literal zero step is rejected during compilation. Any other zero step fails before the first iteration. Counter updates end at the last valid endpoint rather than overflowing.
A range loop generates numeric values directly from its loop header.
loop 0 to 10 by 2 |value, ordinal|:
io.line([: [ordinal]: [value]])
;
Forms of range loops
loop start to end:
...
loop start to & end |value|:
...
loop start to end by step |value, ordinal|:
...
loop start to & end by step |value, ordinal|:
...
loop to end |value|:
...
loop to & end by step:
...
Bounds
to uses an exclusive end bound.to & uses an inclusive end bound.- An omitted start is
0. - The bounds alone select the counter type.
- There is no
reverse keyword. - When start and end are equal,
to produces no iterations and to & produces one.
Steps
by supplies a signed step. Its sign alone selects direction. A positive step counts upward, a negative step counts downward and a zero step is invalid. Bounds never infer direction, so a range whose step points away from its end bound is empty.
loop 5 to 0 by -1 |value|:
io.line([: [value]])
;
loop 5 to 0 |value|:
io.line([: [value]])
;
The first loop visits 5, 4, 3, 2 and 1. The second loop visits nothing.
Range bounds are exact fixed-width integers (I8 through U64) or binary floats (F16, F32 and F64). Byte, Dec and DecN have no range domain. Mixed bounds follow the exact numeric domain rules in Numbers. The selected range type is the counter domain, not a positional index type. The step never selects or widens that counter.
- An omitted step on an integer range is
1 with type I32. An untyped whole-number literal step also defaults to I32. - An integer range accepts a step of type
I8, I16, I32 or I64. The step keeps its own type. - A step whose type is unsigned, binary-float,
Byte or Dec is rejected on an integer range. - A range using a binary float requires an explicit
by step. - A float range accepts a binary-float step at the same or narrower precision than the counter, then converts it to the counter type. A wider float step is rejected. A typed integer step never becomes a float step, while an untyped whole-number literal step takes the counter precision.
- Start and end expressions must have numeric types.
loop 0.0 to 1.0 by 0.1 |amount|:
io.line([: [amount]])
;
loop 1.0 to & 0.0 by -0.5 |amount|:
io.line([: [amount]])
;
Range loop evaluation order
Start, end and explicit step expressions are evaluated once, from left to right, before the first bounds check.
The loop keeps those evaluated values for the complete iteration.
A dynamic zero step fails before any bounds check, even when the range is empty or points in the wrong direction.
Bindings
The first binding receives the current range value in the selected exact counter type. The bounds alone select that type, and the step keeps its own type. Range counters keep their selected domain rather than inheriting a minimum width.
The optional second binding receives the zero-based iteration ordinal and always has type I32. It counts visited iterations upward, regardless of the counter value or direction. The counter domain and ordinal type remain independent. Collection lengths, collection indices and fixed capacities also use I32.
loop 5 to 0 by -1 |value, ordinal|:
io.line([: [ordinal]: [value]])
;
This visits 5 at ordinal 0 through 1 at ordinal 4.
Bindings use the same one-or-two-name |...| rules as collection loops.
Checked updates
An integer range candidate uses exact arithmetic, and the counter is replaced only when that candidate still lies within the bound in the step's direction. A candidate that would pass the bound ends the loop, so the counter never needs a value outside its domain. An inclusive range exits after visiting its endpoint without computing a successor.
Termination also holds at the edges of the counter domain. An inclusive range ending at a type's maximum and a descending range reaching zero both complete normally. Distances up to U64::MAX stay exact, and a step at an I64 minimum keeps its full magnitude.
The I32 ordinal uses a checked integer update, so its own overflow remains an implicit failure.
near_max U8 = 254
maximum U8 = 255
loop near_max to & maximum by 1 |value, ordinal|:
io.line([: [ordinal]: [value]])
;
This range visits 254 and 255 as U8 values. Its I32 ordinal runs from 0 to 1, and termination needs no U8 successor beyond 255.
A literal zero step is rejected during compilation with MOTH-SYNTAX-0029, and a dynamic zero step uses built-in error code 306 (InvalidLoopStep).
Floating-point ranges round each candidate to the selected binary precision before checking the endpoint. A non-finite or out-of-range candidate ends the loop without becoming the next value. If an in-range candidate rounds back to the current value while the endpoint still lies ahead, the loop fails at runtime with built-in error code 307 (RangeStepNoProgress) rather than repeating that value.
Const range loops diagnose statically known zero and no-progress steps while folding. Const templates owns those compile-time rules.
Progress records target support separately from these range semantics.