Loops
while
Repeats a block while the condition is true:
The condition must be a bool expression.
for
C-style for loop with initializer, condition, and increment:
The loop variable is scoped to the for block — it is not accessible after the loop ends.
All three clauses are required:
| Clause | Description |
|---|---|
| Initializer | var name: type = value — declares the loop variable |
| Condition | Bool expression — checked before each iteration |
| Increment | Assignment — executed after each iteration |
If the condition is false on the first check, the body never executes.
for-in
Iterate over array elements:
var names: array<string> = ["alice", "bob", "charlie"]
for (var name in names) {
console.log(name)
}
The iteration variable type is inferred from the array element type. The original array is not modified.
Works with any array type:
break
Exits the innermost loop immediately:
Works in while, for, and for-in loops.
continue
Skips to the next iteration:
for (var i: i32 = 0; i < 5; i = i + 1) {
if (i == 2) {
continue
}
console.log(i) # prints 0, 1, 3, 4
}
Nested Loops
for (var i: number = 0.0; i < 3.0; i = i + 1.0) {
for (var j: number = 0.0; j < 3.0; j = j + 1.0) {
console.log(i * 3.0 + j)
}
}
break and continue only affect the innermost loop.