Tutorial by Examples

Integral type ranges ( IntRange , LongRange , CharRange ) have an extra feature: they can be iterated over. The compiler takes care of converting this analogously to Java's indexed for-loop, without extra overhead for (i in 1..4) print(i) // prints "1234" for (i in 4..1) print(i) // pri...
if you want to iterate over numbers in reverse order? It's simple. You can use the downTo() function defined in the standard library for (i in 4 downTo 1) print(i) // prints "4321"
Is it possible to iterate over numbers with arbitrary step, not equal to 1? Sure, the step() function will help you for (i in 1..4 step 2) print(i) // prints "13" for (i in 4 downTo 1 step 2) print(i) // prints "42"
To create a range which does not include its end element, you can use the until function: for (i in 1 until 10) { // i in [1, 10), 10 is excluded println(i) }

Page 1 of 1