Kotlin Flow Control : While Loop and Do-While Loop


While and Do-While loop in Kotlin programming language works exactly in a similar way like it works in other programming languages. The only difference between these loops is, in case of Do-while loop the condition will be tested at the end of the loop. Which means, Do-while loop body is always executed at least once even if the condition is false.

Example:
fun main(args: Array<String>) {
   var x:Int = 0
   println("-: While Loop Example :-")
   
   while(x< = 10) {
      println(x)
      x++
   }

  // OR
  // Do-While Loop

  do {
      println(x)
      x++
   } while(x<=10)

}


We will get same output in both case. The only difference between both statments is, in case of Do-While statment, expression will be executed till x = 11.

While Loop Output:
-: While Loop Example :-
0
1
2
3
4
5
6
7
8
9
10

Comments