In this tutorial you will learn about the Kotlin do…while Loop and its application with practical example.
Kotlin do…while Loop
The do…while statement will execute a block of code and then test the condition for next iteration and executes next only if condition is true, block of code executes at least once.
Syntax:-
1 2 3 4 |
do{ //loop body } while(test_condition); |
Example:-
Let’s see a simple example of do-while loop printing “Hello World!” for 5 times.
1 2 3 4 5 6 7 8 |
fun main(args: Array<String>){ var i = 1 do { println("Hello World!") i++ } while (i<=5); } |
Output:-
1 2 3 4 5 |
Hello World! Hello World! Hello World! Hello World! Hello World! |