Kotlin while Loop

Latest posts
Get notified of the Latest Sport News Update from Our Blog
Share

It is used to iterate a part of code several times. It executes until the condition is true.

Syntax

while(condition){  
//this code executes
}  

let’s take an example:

fun main(args: Array<String>){  
    var i = 1  
    while (i<5){  
        println(i)  
        i++  
    }  
}
output:
1
2
3
4

Iterate through Array using while loop

fun main(args: Array<String>){  
    var names = arrayOf("JB","Chris","AB","Sid")
    var index = 0
 
    while(index < names.size) {
        print("${names[index]} ")
        index++
    }  
} 
output
JB Chris AB Sid

Related blogs

Nullam quis risus eget urna mollis ornare vel eu leo. Aenean lacinia bibendum nulla sed 

Subscribe to get 15% discount