Understanding Ranges in Kotlin

January 14, 2019

Often times we need to create a loop, however in many different languages to define a range varies. In Kotlin, there are few ways we can define a range for a for-loop.

for (i in 1..100) { ... }  // closed range: includes 100
for (i in 1 until 100) { ... } // half-open range: does not include 100
for (x in 2..10 step 2) { ... }
for (x in 10 downTo 1) { ... }
if (x in 1..10) { ... }