Sling Academy
Home/Kotlin/How to Write a `while` Loop in Kotlin

How to Write a `while` Loop in Kotlin

Last updated: November 30, 2024

Introduction to `while` Loops in Kotlin

The `while` loop in Kotlin is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. It continues to execute the block of code as long as the condition evaluates to true.

Basic Syntax of a `while` Loop


while (condition) {
    // code block to be executed
}

In this syntax, the condition is evaluated before every execution of the loop block. If the condition is true, the code block within the loop is executed. This process repeats until the condition becomes false.

Example 1: Simple `while` Loop


fun main() {
    var counter = 0

    while (counter < 5) {
        println("Counter: $counter")
        counter++
    }
}

In this example, a variable counter is initialized to 0. The loop condition checks if counter is less than 5. Inside the loop, it prints the current value of counter and increments it by 1 on each iteration. The loop stops when counter reaches 5.

`while` vs `do-while` Loop

Kotlin also offers a do-while loop, which is similar to a while loop, but with a key difference: it executes the code block at least once before the condition is tested. Here's how it looks:


do {
    // code block to be executed
} while (condition)

Let's see an example using a do-while loop:


fun main() {
    var counter = 0

    do {
        println("Counter: $counter")
        counter++
    } while (counter < 5)
}

In this example, the loop prints the counter value and increments it without first checking the condition, ensuring the loop block is executed at least once.

Use Cases for `while` Loops

The `while` loop is particularly useful in scenarios where the number of iterations is not known beforehand. It's commonly used when processing user input or reading data until an end-of-file condition is met.

Conclusion

The `while` loop is a versatile tool in Kotlin for controlling the flow of applications. It's ideal for scenarios where you need to perform repeated operations based on dynamic conditions.

Next Article: How to Use `do-while` Loops in Kotlin

Previous Article: Working with Indices in `for` Loops in Kotlin

Series: Control Flow in Kotlin

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin