While Loop in Kotlin With Example

In this article, we will learn about while loop in kotlin. We will learn how to repeat a process for specified number of times or repeat a process till some conditions are satisfied. Let’s understand this by taking some example.

There are 50 students in a school. You need to print name of students on your screen. What will you do? How would you write program to print name of all students? First way is to write print statement 50 times. But, do you think it’s feasible? What if there are 10000 students? How would you print name then?

There comes importance of loop statement. You need to use loop in your program. Loop just repeats a specified work till some conditions are satisfied. Using loop you can print name of 50 students easily. This is feasible for any number of students even if you have 100000 students in your school.

Now, you have understood the importance of loop statement in your program. In Kotlin, you can achieve loop in your program in 3 ways.

Use For Loop in your program
Use While Loop in your program
– Use do While Loop in your program

We have already seen how to use For Loop in your program. In this article, We will go through while loop in kotlin. You will get answer to below questions in this article.

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

Lets’ go through each point one by one.

What is while loop in kotlin?

While loop in kotlin is a control flow statement used to repeat some process till some conditions are satisfied. Syntax to use while loop is as below

while(testCondition) {
	// Code to do some actions till testCondition is satisfied i..e till it is true.
}

How does while loop in kotlin work?

Syntax to use while loop is as below –

while(testCondition) {
	// Code to do some actions till testCondition is satisfied i..e till it is true.
}

In this program, there is a condition testCondition in while loop. At first, Compiler checks this test condition. There are 2 conditions now –
a. test condition succeeds i.e. test condition is true. If it is so, code inside the while block is executed. Then, again test condition is tested. Then, the process is repeated until test condition fails.
b. test condition fails i.e. test condition is false. If it is so, while loop is terminated. code after the while block starts executing.

You can understand the code flow clearly by flowchart.

Flowchart of while loop

Tutorialwing Kotlin while loop or While Loop in kotlin

Tutorialwing Kotlin while loop flow chart




Different Example to show while loop in kotlin

Below are some examples that demonstrate different ways to use while block in kotlin.

a. Print name of students using while loop

fun main(args: Array<String>) {
	val nameList = arrayOf("Rajeev", "Rohan", "Rohit", "Roopesh")

	var index = 0;
	while (index < nameList.size) {
	    println(nameList[index])
	    index++;
	}
}

When you run the program, you will get below output:
Rajeev
Rohan
Rohit
Roopesh

When the program runs, the loop condition is checked. if it’s true, code inside the while block is executed. Again, condition is checked . If it’s true the process is repeated. Otherwise, while loop is terminated and code after while block is executed. Let’s go through each cycle of while loop of above program.

Note that nameList.size will be same for every round . Here, value of nameList.size is 4.

Round 1

index = 0 , 0 < 4 Condition is true. So, Rajeev is printed.

Round 2

index = 1 , 1 < 4 Condition is true. So, Rohan is printed.

Round 3

index = 2 , 2 < 4 Condition is true. So, Rohit is printed.

Round 4

index = 3 , 3 < 4 Condition is true. So, Roopesh is printed.

Round 5

index = 4 , 4 < 4 Condition is false. So, while loop is terminated.

b. Print Number from 1 to 10 in reverse order using while loop.

fun main(args: Array<String>) {
	var index = 10
	while(index > 0) {
	    println(index)
	    index--;
	}
}

When you run the program, you will get below output:
10
9
8
7
6
5
4
3
2
1

When the program runs, the loop condition is checked. If it’s true, code inside the while block is executed. Again, condition is checked . If it’s true, the process is repeated. Otherwise, while loop is terminated and code after while block is executed. Let’s go through each cycle of while loop of above program.

Round 1

index = 10 , 10 > 0
Condition is true. So, 10 is printed.

Round 2

index = 9 , 9 > 0
Condition is true. So, 9 is printed.

Round 3

index = 8 , 8> 0
Condition is true. So, 8 is printed.

Round 4

index = 7 , 7 > 0
Condition is true. So, 7 is printed.

Round 5

index = 6 , 6 > 0
Condition is true. So, 6 is printed.

Round 6

index = 5 , 5 > 0
Condition is true. So, 5 is printed.

Round 7

index = 4 , 4 > 0
Condition is true. So, 4 is printed.

Round 8

index = 3 , 3 > 0
Condition is true. So, 3 is printed.

Round 9

index = 2 , 2 > 0
Condition is true. So, 2 is printed.

Round 10

index = 1 , 1 > 0
Condition is true. So, 1 is printed.

Round 11

index = 0 , 0 > 0
Condition is false. So, while loop is terminated.

c. Print Square of Number from 1 to 5 using while loop in kotlin.

fun main(args: Array<String>) {
	var index = 1
	while(index <= 5) {
	    println(index * index)
	    index++
	}
}

When you run the program, you will get below output:
1
4
9
16
25

When the program runs, while loop is executed till the condition is true. While loop is terminated as soon as condition becomes false. Let’s check the step by step condition checked in above program.

Round 1

index = 1 , 1 <= 5 Condition is true. So, 1 * 1 = 1 is printed.

Round 2

index = 2 , 2 <= 5 Condition is true. So, 2 * 2 = 4 is printed.

Round 3

index = 3 , 3 <= 5 Condition is true. So, 3 * 3 = 9 is printed.

Round 4

index = 4 , 4 <= 5 Condition is true. So, 4 * 4 = 16 is printed.

Round 5

index = 5 , 5 <= 5 Condition is true. So, 5 * 5 = 25 is printed.

Round 6

index = 6 , 6 <= 5 Condition is false. So, while loop is terminated.




Different questions on while loop in kotlin

Assuming you have gone through this article carefully. There are some questions for you.

1. Guess the output of the below program

fun main(args: Array<String>) {
	var index = 5
	while(index != 0){
	    println(index);
	    index -= 2;
	}
}

2. Guess the output of the below program

fun main(args: Array<String>) {
	var index = 6
	while(index != 0) {
	    println(index);
	    index -= 2;
	}
}

3. Guess the output of the below program

fun main(args: Array<String>) {
	var y = 10
	while (y >= 0) {
	    if (y % 2 == 0)
	        println(y);
	    y—;
	}
}

4. Guess the output of the below program

fun main(args: Array<String>) {
	var y = 10
	while (y >= 0) {
	    println(y * y * y)
	    y--;
	}
}

Conclusion

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

Leave a Reply