Kotlin do-while Loop

Introduction

The do-while loop in Kotlin is used to repeatedly execute a block of code as long as a specified condition is true. Unlike the while loop, the do-while loop guarantees that the loop body will be executed at least once because the condition is evaluated after the loop body. This chapter will cover the syntax and usage of the do-while loop in Kotlin with examples.

Basic do-while Loop

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

Syntax

do {
    // Code to execute
} while (condition)
  • condition: A Boolean expression that is evaluated after 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
    do {
        println("Iteration $i")
        i++
    } while (i <= 5)
}

Explanation:

  • var i = 1: Initializes a variable i with the value 1.
  • do { ... } while (i <= 5): Executes the loop body first, then checks the condition i <= 5. Since i is 1, 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.

Example Program with do-while Loop

Here is an example program that demonstrates the use of the do-while loop in Kotlin:

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

Explanation:

  • var i = 1: Initializes a variable i with the value 1.
  • do { ... } while (i <= 5): Executes the loop body first, then checks the condition i <= 5. Since i is 1, 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 do-while Loop

break Statement

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

Syntax

break

Example

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

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.

Syntax

continue

Example

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

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 with do-while Loop, break, and continue

Here is an example program that demonstrates the use of the do-while loop along with the break and continue statements in Kotlin:

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

    // do-while loop with break
    println("\ndo-while loop with break:")
    var j = 1
    do {
        if (j == 3) {
            break
        }
        println("Iteration $j")
        j++
    } while (j <= 5)

    // do-while loop with continue
    println("\ndo-while loop with continue:")
    var k = 1
    do {
        if (k == 3) {
            k++
            continue
        }
        println("Iteration $k")
        k++
    } while (k <= 5)
}

Explanation:

  • The first do-while loop demonstrates a basic iteration from 1 to 5.
  • The second do-while loop includes a break statement that terminates the loop when j equals 3.
  • The third do-while loop includes a continue statement that skips the iteration when k equals 3.

Conclusion

In this chapter, you learned about the do-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 do-while loops. 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