Examples · 21 of 339
21-loops
Three loops: for x in xs, for i, x in xs (with the index), and loop { }, which runs until a break or return.
tin run examples/21-loops.tin// Three loops: `for x in xs`, `for i, x in xs` (with the index), and
// `loop { }`, which runs until a `break` or `return`.
fn main() {
let joints = ["base", "shoulder", "elbow", "wrist"]
for i, name in joints {
print("{i}: {name}")
}
// settle a value: halve the error until it is small enough
var error = 1.0
var steps = 0
loop {
error = error / 2.0
steps += 1
if error < 0.01 { break }
}
print("settled in {steps} steps")
} 0: base
1: shoulder
2: elbow
3: wrist
settled in 7 steps