Answers for "kotlin for loop"

6

kotlin loop

for (i in 1..5) print(i)
-> 12345

for (i in 5 downTo 1) print(i)
-> 54321

for (i in 3..6 step 2) print(i)
-> 35

for (i in 'd'..'g') print (i)
-> defg
Posted by: Guest on June-16-2020
11

Kotlin when

val x = 3
when(x) {
    3 -> println("yes")
    8 -> println("no")
    else -> println("maybe")
}
// when can also be used as an expression!
val y = when(x) {
    3 -> "yes"
    8 -> "no"
    else -> "maybe"
}
println(y) // "yes"
Posted by: Guest on May-10-2020
3

for loop kotlin

val array = arrayOf(1, 3, 9)
for (item in array) {
    //loops items
}
for (index in 0..array.size - 1) {
	//loops all indices
}
for (index in 0 untill array.size) {
    //loops all indices
}
for (index in array.indices) {
    //loops all indices (performs just as well as two examples above)
}
Posted by: Guest on May-10-2020
7

when kotlin

when (x) {
    1 -> print("x == 1")
    2 -> print("x == 2")
    else -> { // Note the block
        print("x is neither 1 nor 2")
    }
}
Posted by: Guest on April-25-2020
0

kotlin for loop

val nums = arrayOf(1, 5, 10, 15, 20)
for (x 
  in nums) {
  println(x)
}
Posted by: Guest on September-09-2021
0

kotlin for loop

val list = listOf("A", "B", "C")
for (element in list) {
 println(element)
}
Posted by: Guest on January-15-2021

Browse Popular Code Answers by Language