Kotlin while Loop

Introduction

The while loop in Kotlin is used to repeatedly execute a block of code as long as a specified condition is true. It is a fundamental control flow statement that allows for repeated execution of a block of code based on a condition. This chapter will cover the syntax and usage of the while loop in Kotlin with examples.

Basic while Loop

The while loop evaluates a condition before executing the loop body. If the condition is true, the loop body is executed. This process repeats until the condition becomes false.

Syntax

while (condition) {
    // Code to execute as long as condition is true
}
  • condition: A Boolean expression that is evaluated before each iteration of the loop.
  • block of code: The set of instructions that will be executed repeatedly as long as the condition is true.

Example

fun main() {
    var i = 1
    while (i <= 5) {
        println("Iteration $i")
        i++
    }
}

Explanation:

  • var i = 1: Initializes a variable i with the value 1.
  • while (i <= 5): Checks if i is less than or equal to 5. Since i is 1, the condition is true, and the loop body executes.
  • println("Iteration $i"): Prints the current value of i.
  • i++: Increments the value of i by 1.
  • The loop continues until i becomes greater than 5.

Using break and continue in while Loop

break Statement

The break statement is used to terminate the loop immediately, regardless of the loop condition.

Example

fun main() {
    var i = 1
    while (i <= 5) {
        if (i == 3) {
            break
        }
        println("Iteration $i")
        i++
    }
}

Explanation:

  • When i equals 3, the break statement is executed, and the loop terminates.
  • The output will be:
    Iteration 1
    Iteration 2
    

continue Statement

The continue statement skips the current iteration of the loop and proceeds to the next iteration.

Example

fun main() {
    var i = 1
    while (i <= 5) {
        if (i == 3) {
            i++
            continue
        }
        println("Iteration $i")
        i++
    }
}

Explanation:

  • When i equals 3, the continue statement is executed, skipping the rest of the loop body for that iteration.
  • The output will be:
    Iteration 1
    Iteration 2
    Iteration 4
    Iteration 5
    

Example Program using while Loop

fun main() {
    // Basic while loop
    println("Basic while loop:")
    var i = 1
    while (i <= 5) {
        println("Iteration $i")
        i++
    }

    // while loop with break
    println("\nwhile loop with break:")
    var k = 1
    while (k <= 5) {
        if (k == 3) {
            break
        }
        println("Iteration $k")
        k++
    }

    // while loop with continue
    println("\nwhile loop with continue:")
    var l = 1
    while (l <= 5) {
        if (l == 3) {
            l++
            continue
        }
        println("Iteration $l")
        l++
    }
}

Output

Basic while loop:
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5

while loop with break:
Iteration 1
Iteration 2

while loop with continue:
Iteration 1
Iteration 2
Iteration 4
Iteration 5

Conclusion

In this chapter, you learned about the while loop in Kotlin, including its syntax and usage for repeated execution of code blocks based on conditions. You also saw how to use the break and continue statements to control the flow within a loop. Understanding how to use these loops is essential for performing repetitive tasks and managing control flow in your Kotlin programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top