Kotlin while Loop
From this tutorial, we will start with loops in Kotlin. We will cover the different types of loops in Kotlin which are used to execute a piece of code again and again until a certain condition is met.
Kotlin Loop
Loops in Kotlin are used to execute a particular piece of code again and again. We can break a loop by specifying a certain condition. If the given condition is achieved, the execution will end the loop. Let us understand loops with an example.
Suppose if you want to write a program to print all even numbers from 1 to 100. Achieving this task manually is really difficult, and even if we somehow manage to do it, what if the range increases to 1000 then it won't be possible to do so. For such use cases, we can use a loop, which will be executed again and again until we reach our target (i.e. 100 or 1000 number).
There are three main types of loops in kotlin:
-
While loop
-
Do-while loop
-
For loop
In this tutorial we will discuss about while
loop. We will cover remaining loops in next tutorials.
Kotlin while
Loop
The while
loop will execute a block of code until the specified condition returns true or any positive value. The execution of the loop will end when the condition returns false.
The syntax of while
loop is:
while(condition){
// Piece of code to be executed
}
Working of while
loop:
-
Before entering the loop for the first time, it will check the whether the condition is true or false.
-
If the condition is true, it will enter the loop. Otherwise, it will end the loop execution.
-
Statements inside the loop block will be executed.
-
After this, again the condition is checked and same procedure is followed until the condition becomes false.
It will go in infinite loop if the condition never fails. We should take care of this when we use a loop.
Kotlin while
Loop Example
Let's take an example where we will print a mathematical table of 2 using while
loop.
fun main() {
var iterator = 1
// when iterator becomes 10, end the loop
while(iterator <= 10){
println("2 * $iterator = " + 2*iterator )
// increment value of iterator by 1
iterator++
}
}
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
Here, the condition to execute the loop is: iterator should be less than or equal to 10. When iterator will reach 11, the condition will return false and the execution will exit the loop.
Note: To learn about ++ and -- operator, learn about Kotlin Unary operators.
One more example:
Let's take one more example to find factorial of a number:
fun main() {
var number = 10 // Number whose factorial needs to be find
var numberToIterate = number // Creating a variable to use this as an iterator
var factorial = 1
// Iterating while loop until it reaches 0
while (numberToIterate > 0){
factorial *= numberToIterate
numberToIterate--
}
println("Factorial of $number is $factorial")
}
Factorial of 10 is 3628800
Summary
In this tutorial, we covered a basic introduction to loops in Kotlin, why we need loops and types of loops. We also covered the while
loop in detail. We will learn about for
loop and do-while
loop in next tutorials.