Control flow

if / else

if ton.x < 10 {
    print("small")
} else if ton.x < 100 {
    print("medium")
} else {
    print("big")
}

Parentheses around the condition are optional.

while

ton.int i = 0
while i < 5 {
    print(i)
    i++
}

for (C-style)

for ton.int i = 0; i < 5; i++ {
    print(i)
}

for ... in (iterate an array or a dict)

ton.array names = ["kuro", "rusher", "natlep"]
for ton.n in names {
    print(n)
}

ton.dict scores = {kuro: 10, rusher: 20}
for ton.key in scores {
    print(key + " -> " + str(scores[key]))
}

Iterating a ton.dict walks its keys, in the order they were written; use scores[key] to read the matching value.

switch / case — no fallthrough: each matching case (or default, if none match) runs its own block and the switch is done.

switch ton.status {
    case 200, 201: {
        print("ok")
    }
    case 404: {
        print("not found")
    }
    default: {
        print("other: " + str(ton.status))
    }
}

A case can list several comma-separated values. Matching uses the same equality rules as ==. A stray break; inside a case (a habit from other languages) is harmless — it just exits the switch early, since there's no fallthrough to break out of in the first place.

break and continue are supported inside while, for, and for ... in.

More for ... in forms (since beta-2.0.0) — the ton. prefix on loop variables is optional, a second variable gives the index (arrays, strings) or the value (dicts), and ranges and strings can be iterated directly:

for i in 0..3 { print(i) }                    // 0 1 2
for i in 1..=3 { print(i) }                   // 1 2 3
for i, name in ["kuro", "rusher"] { print(i, name) }
for key, value in {a: 1, b: 2} { print(key, "=", value) }
for c in "abc" { print(c) }

Each iteration has its own scope, so a function created inside the loop remembers that iteration's value:

ton.array fns = []
for i in 0..3 { push(fns, () => i) }
print(fns.map(f => f()))    // [0, 1, 2]

continue inside a switch that is inside a loop now continues the loop. catch also accepts the error name without parentheses: catch err { ... }.

Edit this page on GitHub