Loops

While

Loops of the “while condition is true” kind can be written in the regular way:

while ( condition ) {
    ... expressions
}

Example:

let a : int = 0
let b : int = 0

while(a < 10) {
    a = a + 1
    b = b + a
}

For

TO2 supports “for in” loops:

for(variable in collection) {
    ... expressions
}

If a simple “from 0 to n” loop is required one can use a range can be use as “collection”:

let sum = 0

for(i in 0..10) {
    sum += i + 1
}

If the collection contains tuples or records a deconstruction can be used as well:

use { CONSOLE } from ksp::console

let arr = [(f: 1.0, s: "First"), (f: 2.2, s: "Second")]
let sum = 0.0

for((s, f) in arr) {
    CONSOLE.print_line(s)
    sum += f
}