Kotlin do-while loop

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

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

Related blogs

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

Subscribe to get 15% discount