Skip to main content

For

for repeats a block. times runs it a number of times:

for (3 times) {
println("again")
}

Ranges​

to counts up to the end and stops before it, and thru includes the end:

# 1 to 9.
for (x in 1 to 10) {
println("${x}")
}

# 1 to 10.
for (x in 1 thru 10) {
println("${x}")
}

downto and downthru count down the same way:

# 10 to 2.
for (x in 10 downto 1) {
println("${x}")
}

# 10 to 1.
for (x in 10 downthru 1) {
println("${x}")
}

Arrays​

for goes through an array's values in order:

let values : i32[] = [1, 2, 3, 4, 5]
for (value in values) {
println("${value}")
}

Looping until break​

A bare for loops forever, until break leaves it:

var count = 0
for {
count = count + 1
if (count > 100): break
}

continue skips to the next time round:

for (x in 1 to 10) {
if (x % 2 == 0): continue
println("${x}")
}