Answers for "kotlin for"

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

kotlin simple for loop

val names = listOf("Jack", "John", "Tim")
for(name in names){
	println(name)
}
Posted by: Guest on February-18-2020
1

kotlin for

for (i in 1..5) print(i) 			// "12345"
for (i in 5 downTo 1) print(i) 		// "54321"
for (i in 0 until 5) { 				// "01234"
     println(i)
}
for(i in 0 until list.size()){
    println(i)
}
Posted by: Guest on April-20-2021
3

kotlin iterate array

val names = listOf("Anne", "Peter", "Jeff")
for (name in names) {
    println(name)
}
Posted by: Guest on May-17-2020
0

kotlin for

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