Kotlin do while Loop Tutorial

In this section, we will learn what the do-while loop is and how to use it in Kotlin.

Note: we’re assuming you’re already familiar with the Kotlin while loop.

What is do while Loop in Kotlin?

The do-while loop is used to run a block of instructions as a long as the condition of the block remains true, just like the way the while loop works.

But the difference is that:

  1. In the while loop, we mentioned that the condition of the loop should be checked first and if the condition was true, in that case only the body of the loop will run. This means there’s a chance that the loop might never get executed (if the condition was false right away).
  2. On the other hand, using the do-while loop, the body first runs and then the condition will be checked to see if it results true or false. This means it is guaranteed that the body of the loop will run at least once.

Alright, enough with theory and now let’s get into the syntax of the loop and see how we can create a do-while loop in Kotlin.

Kotlin do while loop syntax

do {

} while (condition)

do: in order to create a do-while loop, we first start with the keyword do.

{}: this is the body of the loop where we can define the instructions to be executed.

while(condition): here we define the condition of the loop to be checked every time the body runs. Note that just like the while loop, as long as the condition of the loop returns true, the loop runs over and over again.

Example: using do while loop in Kotlin

fun main(){

    var num = 10 

    do{
        println("The value of the num variable is: ${num}")
        num++
    }while(num <10)
}

Output:

The value of the num variable is: 10

How does do while loop work in Kotlin?

In the last example, if you check the condition of the while loop, you can see that the condition of the loop is already false! So if we used the while loop here, the body of the loop would never run! But because this is the do-while loop, we’re rest assured that the body will run at least once! That’s how we got the message you see on the output stream.

Kotlin infinite do while loop

Just like the while loop, we can make a do-while loop run infinitely as well! To do that, we only need to set the condition of the loop to something true (like the boolean value true) and then the loop will run forever (or at least until the operating system doesn’t shut it down automatically).

Note: It’s rare you need a program with an infinite loop! So it’s best practice to always define the instructions of the loop in a way that affects the condition of that loop so that at some point the condition becomes false and the loop breaks.

Example: creating infinite do while loop in Kotlin

fun main(){

    var num = 0 

    do{
        println("The value of the num variable is: ${num}")
        num++
        if (num == 10){
            break
        }
    }while(true)
    println("The statement after the loop")
}

Output:

The value of the num variable is: 0

The value of the num variable is: 1

The value of the num variable is: 2

The value of the num variable is: 3

The value of the num variable is: 4

The value of the num variable is: 5

The value of the num variable is: 6

The value of the num variable is: 7

The value of the num variable is: 8

The value of the num variable is: 9

The statement after the loop

Here, the condition of the loop is set to true and that means if we don’t call the break statement within the body of the loop, it will continue to run forever. That’s why we’ve used the break statement to stop the loop when the value of the num variable reached to the value 10.

Difference between do while and while in Kotlin (Kotlin do while vs while loop)

As mentioned before, the main difference between the while and do-while loop in Kotlin is on the way the condition of the loop is checked!

– For the while loop, first the condition is checked and only if it resulted true, then the body will be executed. This means there’s a chance that the body might never get a chance of running (if the condition of the loop becomes false in the first place).

– On the other hand, for the do-while loop, first it’s the body of the loop that runs and then comes to the condition of the loop. This means it is guaranteed that the body will run at least once.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies