Kotlin while Loop

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