While Loop and Its Usage in Kotlin

Definition

  • A while loop 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.
  • The while loop can be imagined of as a repeating if statement.

Syntax

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

While Loop Example Program in Kotlin


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

fun main(args: Array<String>) {

    var num1 = 0
    
    while (num1<5) {
       println(" Value : $num1")
       num1++;
    }
    
}

Sample Output

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