In this tutorial you will learn about the Kotlin while Loop and its application with practical example.
Kotlin while Loop
The while loop will execute a block of statement as long as a test expression is true.
Syntax:-
1 2 3 |
while(test_condition){ //loop body } |
Example:-
Let’s see a simple example of while loop printing “Hello World!” string 5 times.
1 2 3 4 5 6 7 8 9 10 11 |
// Program to print "Hello World!" 5 times fun main(args: Array<String>) { var i = 1 while (i <= 5) { println("Hello World!") ++i } } |
Output:-
1 2 3 4 5 |
Hello World! Hello World! Hello World! Hello World! Hello World! |