Answers for "loops in kotlin"

11

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
2

loops in kotlin

-------------------------------------------------------------------------------------------------------------------------------
(for) -------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------- 
    "for each"
        # var colorarray = arrayOf("Red","Green","Blue","Yellow")
        # for (i in colorarray){
        #     println(i)
        # }

        # for (i in colorarray.indices){
        #     println(colorarray[i])
        # }

    "range"
        # for (i in 0..10){
        #   println(i)
        # }

    "range with keywoard"
        # for (i in 0.rangeTo(10).step(3)){
        #   println(i)
        # }

    "step"
        # for (i in 0..10 step 2){
        #   println(i)
        # }

    "down to"
        # for (i in 10 downTo 0){
        #    println(i)
        # }
    
    "down to with keywoard"
        # for (i in 10.downTo(0).step(3)){
        #     println(i)
        # }

    "until"
        - do not use last value here it will print only 0-9
        # for (i in 0 until 10){
        #     println(i)
        # }

        # for (i in 0 until colorarray.size){
        #     println(colorarray[i])
        # }
Posted by: Guest on November-13-2021
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
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

while loop kotlin

for (item in collection) print(item)
Posted by: Guest on March-02-2020
0

how to do a for loop loop kotlin

//This is how you do a for loop in Kotlin

fun main() {

    for (i in 1..5) {
        println(i)
    }
}


//It will print
1
2
3
4
5

//have fun trying kotlin! You will get it hopefully very quickly!











//If your try copying and pasting the code put it in this function! Hopefully it will work!

fun main(args: Array<String>) {

println("Hello Bob")

}
Posted by: Guest on July-26-2021

Browse Popular Code Answers by Language