July 2018
Intermediate to advanced
242 pages
8h 6m
English
The Kotlin standard library provides a convenient way of creating progression with a custom step value. We can do so using an extension function for progressions of integral types called step(). We can also benefit from the infix notation and declare a progression with a custom step, as follows:
val progression: IntProgression = 0..1000 step 100
If we were to use progression in the for loop, it would iterate 10 times:
val progression: IntProgression = 0..1000 step 100for (i in progression) { println(i)}
We could also achieve the same result by iterating with the while loop, as follows:
var i = 0while (i <= 1000) { println(i) i += 100}
Read now
Unlock full access