As like In Java, Kotlin while loop continually executes a block of statements while a particular condition is true.
Test expression inside the while loop parenthesis is a Boolean expression.
If the test expression is evaluated to true then contents inside the while loop will be executed.
Process continues until the test expression is evaluated to false.
If the test expression is evaluated to false while will terminate it’s execution.
1 2 3 4 5 6 7 8 9 10 11 12 |
fun main(args: Array<String>) { var i = 1 while (i <= 10) { println("Value $i") ++i } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 |
Value 1 Value 2 Value 3 Value 4 Value 5 Value 6 Value 7 Value 8 Value 9 Value 10 |