Kotlin Loops



 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
In any programming, language loops are used to iterate over any data structure
until a certain condition is reached.
 
In Kotlin there are three types of loops.
 

1. for loop


         // iterating with for loop on an array
    var arr = arrayOf(1, 2, 3, 4, 5)
    for (i in arr) {
        println(i)
    }

    // iterating with for loop in a range
    for (i in 1..5) {
        println(i)
    }

    // iterating with for loop on a collection
    var list = listOf(1,2,3,"a", "b")
    for (item in list) {
        println(item)
    } 
 

2. while loop


         var num = 1
    while(num < 10) {
        println ("Number is $num")
        num++
    }
 

3. do-while loop


        var num = 1
    do {
         println ("Number is $num")
         num++
    } while(num < 10)



Comments