Do While Loop In Kotlin With Example




In this article, we will learn how to use do while loop in kotlin with example. Also, we will go through different examples and questions that demonstrate the use of do while loop in kotlin.

Can you guess why do we need loop in the program? Answer is simple –
To Repeat the process till the condition is satisfied. For example, To print a name of all students in a class.

Now, the next question is how many ways are there in kotlin through which you can achieve loop ? Answer is –

Using For Loop
Using While Loop
Using Do While Loop

We have already gone through For loop and while loop. Now, We will look into do while loop. In this article, we will cover below topics about do while loop in kotlin.

– What is do while loop in kotlin?
– How does it work?
– Different Examples to show do while loop in kotlin
– Different questions on do while loop in kotlin

What is do while loop in kotlin?

Like For loop and while loop, do while is control flow block that is used to repeat a process for specified number of times or till some conditions are satisfied. Unlike For loop or while loop, Body of do while loop is executed atleast once before testCondition is tested.

Syntax to use do while block is –

do {
   // code to run when condition is true
} while (testCondition);

How does do while loop in kotlin work?

Syntax to use do while block is –

do {
   // code to run when condition is true
} while (testCondition);

When the program runs, code inside do block is executed once without testing the condition. After that, condition is tested. Now, there are 2 cases –
a. test condition is true. code inside do block will be executed. Then, test condition is tested again. Process is repeated till test condition is true.
b. test condition is false. If it is so, do while loop is terminated.

Flowchart of do while loop in Kotlin

Tutorialwing - Kotlin Do while loop in kotlin or kotlin while loop Flow chart

Tutorialwing – Kotlin Do while loop Flow chart




Different Examples to show do while loop in kotlin

Below are some example to demonstrates the use of do while block in kotlin.

a. Below example will print value only once.

fun main(args: Array<String>) {
	val i = 0
	do {
	    println(i);
	} while (i == 1);
}

Output:
0

When the program runs, the code inside do block is executed without testing condition in while block. So, 0 will be printed. Now, condition will be tested. since i is 0, 0 == 1 is false. So, do while loop is terminated.

b. Example to demonstrate do while loop

fun main(args: Array<String>) {
	var i = 10
	do {
	    println("Hello $i")
	    i -= 1
	} while (i > 0)
}

Output:
Hello 10
Hello 9
Hello 8
Hello 7
Hello 6
Hello 5
Hello 4
Hello 3
Hello 2
Hello 1

c. Example to demonstrate do while loop

fun main(args: Array<String>) {
	var i = 10
	do {
	    println(i)
	    i -= 1
	    if (i == 6) {
	        break
	    }
	} while (i > 0)
}

Output:
10
9
8
7

d. Example to demonstrate do while loop

fun main(args: Array<String>) {
	var i = 1
	do {
	    println("5 * $i = " + (5 * i))
	    i++
	} while (i <= 10)
}

Output:
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

Different questions on do while loop in kotlin

Now, we have some questions for you on do while block in kotlin

1. Guess the output of below program

fun main(args: Array<String>) {
	val x = 12
	var y = 1
	do
	    if (x > 5)
	        println("ONE")
	    else if (x > 10)
	        println("TWO")
	    else if (x == 15)
	        println("THREE")
	    else
	        	println("FOUR")
	while (y-- != 0)
}

2. Guess the output of below program

fun main(args: Array<String>) {
	val x = 1
	do {
	    println("Hi Tutorialwing");
	} while (x > 0);
}

3. Guess the output of below program

fun main(args: Array<String>) {
	val x = true
	do {
	    println("Hi Tutorialwing");
	} while (x);
}

4. Guess the output of below program

fun main(args: Array<String>) {
	val x = false
	do {
	    println("Hi Tutorialwing");
	} while (x);
}



Conclusion

In this post, we have seen how to use do while block in Kotlin. We have also gone through different example that demonstrates the use of do while loop in kotlin.

Now, what next?
In the next post, you will learn about break in Kotlin.

Support Us

If you have any suggestion about this post, please comment below. We are looking for someone who helps use to make our tutorial better. Also, If you want tutorials on any topic, you can mail us at tutorialwing@gmail.com. We will try to cover the topic as soon as possible. Connect with us on Facebook, Google+, Twitter and Youtube for more updates.