In this section, we will learn what the while loop is and how to use it in Kotlin.
The while loop in Kotlin
The while loop is a way of creating a block of code that can be executed repeatedly! Basically, it’s a way of creating a loop in Kotlin that repeats its execution based on a condition that we define for the loop.
To be clear, the while loop has a condition and a block. Within the block, we define the instructions that we want to run repeatedly. Now, in the condition part, we define an expression that results either true or false. If the result was true, then the body of the loop will run. But if the result was false, the loop will never run.
Note: every time the body of the while loop runs, its condition will be checked again. So as long as the condition of a loop results true, its body runs over and over again.
Kotlin while loop Syntax:
This is the syntax of the while loop:
while (condition){ #instructions of the loop… }
while
: in order to create while loop, we start with the keyword while.
(condition)
: this is where we set the condition of the loop to be checked. If this condition evaluated to true, then the body of the loop will run. Otherwise, if the condition resulted false, the body might never get a chance of running!
{}
: the pair of curly braces that comes after the condition of the loop defines the while loop’s body and here we can set the instructions we want to run in the loop.
Example: using while loop in Kotlin
fun main(){ var num = 0 while(num <10){ println("The value of the num variable is: ${num}") num++ } 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
How Does while Loop Work in Kotlin?
In the example above, the condition of the while loop is to see if the value of the num
variable is less than the value of 10. For now, the current value of this variable is 0 and so the result of the condition is true. For this reason, the body of the loop gets executed.
Within the body of the loop, we have two statements.
- The first one simply sends a message to the output stream telling the current value of the num variable.
- The second statement increases the value of the num variable by one.
Now, after the execution of the while loop’s body, your program will come back to check the condition of the loop again to see if the condition still holds true.
So here, because the value of the num variable is still less than the value 10, the condition remains true and so the body of the loop continues to run.
This process continues for 10 times (meaning the condition of the loop will be checked for 10 times and its body also runs for 10 times).
Now, for the 11 times, the condition of the loop is no longer holds true! This is because the value of the num variable is no-longer less than the value 10.
At this moment, the body of the loop won’t run anymore (Basically, it breaks now) and your program jumps out of the loop and continues to run the instructions after the while loop.
That’s how the while loop works.
Note: it’s a good practice to always set the instructions of a while loop in a way that affects the condition of that loop so that at some point, the condition of the loop becomes false and eventually the loop breaks.
How to Exit while Loop in Kotlin?
Usually a while loop runs as long as its condition remains true. This means we need to set the instructions within the body of a loop in a way that affects the condition of that loop so that eventually turns the condition into false and hence, making the loop to break.
But sometimes we might need to abrupt the iteration of the loop and make it to break immediately even though the condition of the loop still holds true!
This is where we can use the break statement.
In the Kotlin break statement section we will talk about this statement in greater details but for now just remember that if you call this statement in a loop, it will break that loop no-matter if its condition still holds true and lets the program to continue running the instructions after the loop.
Note: to use the break statement, simply put the keyword break
in the body of the target loop. This statement does not take a value.
Example: using break statement in while loop
fun main(){ var num = 0 while(num <10){ if (num == 5){ break } println("The value of the num variable is: ${num}") num++ } }
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
Based on the condition of the while loop in this example, we should expect the loop to iterate 10 times, but as you can see, the loop terminated after just five iterations! This is because of the call to the break statement that happened when the value of the num variable reached to the value 5.
Basically, at this stage, the loop terminates and the program continues to run the instructions after the loop.
Kotlin while loop continue statement
Other than terminating the entire loop using the break statement, sometime we might want to just stop the current iteration of a loop and move on to the next iteration.
This is where we can use the continue statement.
This statement is explained in greater details in the Kotlin continue statement section but for now just remember that whenever you call this statement, it will stop only the current iteration of a loop and continues to run the next iteration in the same loop (if there is an iteration remained of course).
Example: using continue statement in while loop
fun main(){ var num = 0 while(num <10){ num++ if (num <= 5){ continue } println("The value of the num variable is: ${num}") } }
Output:
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 value of the num variable is: 10
In this example, as long as the value of the num variable is less than the value 5, a call to the continue statement will be made and so the program will stop that current iteration right at the continue statement and jumps into the next iteration to run that.
That’s why we didn’t get any message as long as the value of the num variable was less than the value 5.
Kotlin Infinite while loop
An infinite while loop is a loop that its condition never becomes false!
Such type of loops runs forever (or at least as long as the operating system doesn’t shut it down).
One of the simplest way of creating an infinite loop in Kotlin is to set the condition of the loop to the boolean value true
.
That way, every time the condition is checked, the value true returns as a result.
Note: In rare cases, you might want to have an infinite loop in your program! So it’s almost best practice to make sure the loop will break, eventually.
Example: creating infinite while loop in Kotlin
fun main(){ var num = 0 while (true){ println("The value of the num variable is: ${num}") if (num == 5){ break } num++ } println("The statement after the while 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 statement after the while loop
Note that the condition of the loop in this example is the value true
. So that means if we didn’t call the break statement in the body of the while loop, it would’ve run forever! (You can simply remove the if clause from the example above and rerun the program, then you would see that it won’t stop at all).
Nested while Loop in Kotlin
The body of a while loop is a place where we can put any type of statements! That includes using another while loop!
Now, the process of setting a while loop within the body of another loop is called nesting while loop.
Note: when creating a while loop within the body of another loop, be aware that for each iteration of the parent loop, the inner while loop will run its entire iterations!
Example: creating nested while loop in Kotlin
fun main(){ var num = 0 var num2 = 0 while(num <3){ println("*****************The value of the num variable is: ${num}") while (num2 < 3){ println("The value of the num2 variable is: ${num2}") num2++ } num2 = 0 num++ } }
Output:
*****************The value of the num variable is: 0 The value of the num2 variable is: 0 The value of the num2 variable is: 1 The value of the num2 variable is: 2 *****************The value of the num variable is: 1 The value of the num2 variable is: 0 The value of the num2 variable is: 1 The value of the num2 variable is: 2 *****************The value of the num variable is: 2 The value of the num2 variable is: 0 The value of the num2 variable is: 1 The value of the num2 variable is: 2
More to Read: