While Loops in Kotlin

You are here:
Estimated reading time: 3 min

In this tutorial, we will be learning how to use while loop in Kotlin. We will also be covering the do-while loop in Kotlin.

In programming, a loop is a sequence of instructions that is repeated several times until the stated condition for the loop becomes false.

While Loop

While loops in Kotlin have 3 parts:

  • A loop condition – A while loop takes a Boolean expression/condition (Boolean expressions are those expressions that evaluate to either true or false) as an argument. The loop is executed repeatedly as long as this condition yields true. If this condition yields false during an iteration, the loop halts and the control exits the while loop and shifts to statements after the while loop.
  • A loop counter variable – This variable is declared and initialized outside of the while loop. During each iteration of the while loop, the counter variable is incremented/decremented and then checked with the loop condition. If the value of the counter variable satisfies the condition, the loop body is executed. If it does not satisfy the loop condition, the loop halts.
  • An increment or decrement statement – A while loop must have an increment/decrement statement. If a loop does not have an increment/decrement statement, then the loop variable’s value will never change, as a result of which, it will always satisfy the loop condition and hence, we will enter an infinite loop which will never stop executing

Syntax

var loopVariable = 1
while(condition){
    //loop body
    loopVariable++ //or loopVariable--
}

Example

fun main(){
    var num=1
    while(num<=10){
        println(num)
    }
}

The above while loop will print numbers from 1 to 10.

var j=5
while(j<4){
    println(j)
    j++
}

This while loop will never run as the condition will never be true.

Usage

To print all elements of an array

var numArray = intArrayOf(1, 2, 3, 4)
var num=0
while(num<numArray.size){
    println(numArray[num])
    num++
}

In the above example, the while loop will print all elements of the array numArray.

First, the loop counter variable num is declared and initialized to 0, as the indices of arrays start from 0 in Kotlin.

Then in the loop condition, this num variable is checked with numArray.size (This yields the size of the array i.e. the total number of elements in the array. Here, it will give a value of 4)

According to the condition, the loop will be executed while the num variable is less than 4. (For 4 elements, the indices will be 0,1,2 and 3. Therefore, less than condition is used, as we want the loop to be executed only while the num variable is less than 4.)

In the loop body, we are printing out the elements of the array and then incrementing the num variable. If this increment statement is not included, the loop will become an infinite loop and will keep on printing the first element of the array (index:0) infinitely.

To compute the sum of numbers from 1 to 100

var i=1
var sum=0
while(i<=100){
    sum+=i //equivalent to sum=sum+i
    i++
}

To print 2n, while 2n<100

var j=2
while(j<=100){
    println(j)
    j*=2
}

do-while loop

The do-while loop in Kotlin is similar to the while loop.

the do-while loop is an exit-controlled loop i.e. the loop condition is checked while exiting the loop body. Whereas while loop is an entry-controlled loop, where the loop condition is checked before entering the loop body. Due to this difference, the do-while loop will always run at least once.

Syntax

var loopVariable=1
do{
   //loop body
    loopVariable++
}while(condition);

Example

var x=10
do{
    println(x)
    x--
}while(x>0);

The above example will print numbers from 10 to 0.

var x=10
do{
    println(x)
    x--
}while(x<5);

In the above example, 10 will be printed as output, even though it does not satisfy the do-while loop condition, as for the first iteration it will not be checked with the condition. After the first iteration, the variable x becomes 9 and is now checked with the condition which yields false and hence, the loop halts.

The while and do-while loops in Kotlin are like while and do-while loops in Java. To learn more about the comparison between Java and Kotlin, refer to our article on Java vs Kotlin here.

To learn about other control flow statements in Kotlin such as for loops, if-else and when, refer to our articles here:

For loops in Kotlin

If expression in Kotlin

When expression in Kotlin

References:

https://kotlinlang.org/docs/tutorials/kotlin-for-py/loops.html

https://kotlinlang.org/docs/reference/control-flow.html

Was this article helpful?
Dislike 0
Views: 8