Loops: for and while
for handles counted iteration with times:
# Perform this operation 10 times.
for (10 times) { ... }
...and iteration over ranges:
# A non-end-inclusive range.
for (x in 1 to 10) { ... }
# An end-inclusive range.
for (x in 1 thru 10) { ... }
...and iteration over collections:
let values = list(1, 2, 3, 4, 5)
for (var value : values) { ... }
A bare for { ... } loops forever until you break:
for {
if (timeUsedSoFar() > 100.seconds): break
...
}
continue skips to the next iteration:
for (operation in tasks) {
if (operation is NoOpOperation): continue
}
You can destructure the index and value together:
for (index, value in items.withIndex()):
println("$index $value")
while and do...while
while repeats a block as long as a condition holds:
var value = 10
while (value != 0) {
print(value--)
}
do...while guarantees the body runs at least once before checking the condition:
var value = 0
do {
println(value++)
} while (value < 10)