Kotlin has the follow for
for (i in 0..10) {}
it is similar to Java
for (int i = 0; i < 10; i++) {}
but how change the increment in kotlin to get something like it in java:
for (int i = 0; i < 10; i = i + 2) {}
operator or with the rangeTo and downTo functions. Kotlin ranges are inclusive by default; that is, 1.. 3 creates a range of 1, 2, 3 values. The distance between two values is defined by the step; the default step is 1.
In certain computer programming languages, the Elvis operator ?: is a binary operator that returns its first operand if that operand is true , and otherwise evaluates and returns its second operand.
There is no traditional for loop in Kotlin unlike Java and other languages. In Kotlin, for loop is used to iterate through ranges, arrays, maps and so on (anything that provides an iterator).
for (i in 1..4 step 2) print(i) // prints "13"
See here: http://kotlinlang.org/docs/reference/ranges.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With