Skip to main content

While Loop and Its Usage in Kotlin

1 min read Updated June 30, 2026
Share:
On this page (6sections)

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

How It Works

This Kotlin program demonstrates While Loop and Its Usage. It first prepares the data it needs, then walks through the data with a loop to compute the result, and finally prints the output shown in the Sample Output above.

  1. Declare the variables that hold the program’s data.
  2. Iterate over the data using a loop to apply the logic.
  3. Print the final result to the console so you can compare it with the sample output.

Try changing the input values and re-running the program to see how the output changes — this is the fastest way to understand how the logic behaves.

Frequently Asked Questions

What is While Loop and Its Usage in Kotlin?
While Loop and Its Usage is demonstrated in this Kotlin tutorial with a complete, runnable example and its sample output.
How do I run this Kotlin example?
Run it in IntelliJ IDEA or Android Studio, or compile from the command line with `kotlinc file.kt -include-runtime -d file.jar` and run `java -jar file.jar`.
How can I practice While Loop and Its Usage?
Copy the example into IntelliJ IDEA or Android Studio, run it, then change the values or add print statements to see how the output changes.

Related Tutorials

Search tutorials