Do..While Loop and Its Usage in Kotlin


Definition

  • A Do..while loop is similar to while loop except that the condition will be checked after the block of code is executed.
  • Like while loop, do while is a control flow statement that allows a particular code block to be executed repeatedly based on a given condition which returns a boolean value.
  • The corresponding block of code is executed continuously until the condition fails or returns false. 

Syntax

do {
   Code to execute while the condition is true
} while ( condition );

Do..While Loop Example Program in Kotlin

// Do..While Loop in Kotlin Example Program
// Loop and Control Statements Kotlin Programs, Basic Kotlin Programs

fun main(args: Array<String>) {
    var num1 = 0
    
    do {
       println(" Value : $num1")
       num1++;
    }while (num1<5)
}

Sample Output

 Value : 0
 Value : 1
 Value : 2
 Value : 3
 Value : 4