Repetition
repeat
repeat executes the code the number of requested times. We can access the current iteration value with the it variable.
repeat(5) {
println(it)
}
outputs
0
1
2
3
4
for
By using a for loop we have more control over the start and end values.
for(i in 10 until 20 step 2) {
println(i)
}
outputs
10
12
14
16
18
We can use .. instead of until if we want the maximum value to be included. step lets us count in steps.
for (i in 10..30 step 2) {
println(i)
}
outputs
10
12
14
16
18
20
while
It’s also possible to use the while instruction to keep a loop going until a condition is no longer met. This is useful if we do not know in advance how many iterations we want.