Skip to content

Loops

while

Repeats a block while the condition is true:

var i: number = 0.0
while (i < 10.0) {
    console.log(i)
    i = i + 1.0
}

The condition must be a bool expression.

for

C-style for loop with initializer, condition, and increment:

for (var i: number = 0.0; i < 10.0; i = i + 1.0) {
    console.log(i)
}

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:

var nums: array<i32> = [10, 20, 30]
for (var n in nums) {
    console.log(n)
}

break

Exits the innermost loop immediately:

var i: i32 = 0
while (i < 100) {
    if (i == 5) {
        break
    }
    i = i + 1
}
assert(i == 5)

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.