do-while loop first executes the code of do block then it checks the condition. do-while loop executes at least once even the condition of while is false.
Syntax
do{
//block of code executes
}
while(condition)
Example
fun main(args: Array<String>){
var i = 0
do {
println(i)
i++
}
while (i<=5)
}
output
0
1
2
3
4
5
Let’s see an example where a block of code executes only once and the condition in while is false.
fun main(args: Array<String>){
var i = 10
do {
println(i)
i++
}
while (i<=5)
}
output
10