Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin for with different increment

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) {} 
like image 621
ademar111190 Avatar asked Feb 11 '15 21:02

ademar111190


People also ask

Is downTo inclusive Kotlin?

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.

What does ?: Mean in Kotlin?

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.

Is there a for loop in Kotlin?

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).


1 Answers

for (i in 1..4 step 2) print(i) // prints "13" 

See here: http://kotlinlang.org/docs/reference/ranges.html

like image 149
Kon Avatar answered Sep 23 '22 12:09

Kon