Learn about While and Do While loops in Kotlin

Do not miss this exclusive book on Binary Tree Problems. Get it now for free.

Reading time: 20 minutes

Loops are native programming techniques which enables a programmer to do multiple identical things in a few lines of code. It is used to repeatedly do the same task with different parameters. While and Do While are two types of loops that are supported in Kotlin and we will learn about them in this article.

While and Do While works almost identical.

While loop in Kotlin

While (some condition) is true, do the following code.
That the definition of while

While

while(true)
{
    println("Hello")
}

Caution don't run above code.
here the explaination
while condition is true print out "Hello"
above code will print out Hello forever.

That is called forever loop/ Infinite loop.

var i = 1
while(i <= 8)
{
    println("i is $i")
    i++
}

output

i is 1
i is 2
i is 3
i is 4
i is 5
i is 6
i is 7
i is 8
  1. we have a variable named i and assigned Integer value of 1
  2. while i is smaller than or equal to 8 following code block will work.
  3. print out i is 1 until 8
  4. add 1 to i
  5. i++ sames as i = i + 1
  6. later you might be searched for difference between ++i and i++

Above example isn't exciting right?

so let's do something useful.
How about sum of number 1 to 34?
Let's do it.

var startNumber = 1
var sum = 0
while(startNumber <= 34)
{
    println("startNumber is now $startNumber")
    sum += startNumber
    println("sum is $sum")
    startNumber++
}

output

startNumber is now 1
sum is 1
startNumber is now 2
sum is 3
startNumber is now 3
sum is 6
.... output is too long, so I remove some.
startNumber is now 32
sum is 528
startNumber is now 33
sum is 561
startNumber is now 34
sum is 595
  1. we have a variable named startNumber, typed is Integer and assign a value of 1
  2. we have another variable named sum, typed is Integer and assign a value of 0
  3. now we have a while, our condition is as long as startNumber is less than or equal to 34 we gonna execute the following code.
  4. sum += startNumber same as sum = sum + startNumber
  5. and finally we add 1 startNumber
  6. useful, right?

Do While in Kotlin

do while is almost same as while
while check the condition then execute the code.
do while execute the code, then check the condition
so what is the difference between them?
even if you don't remember anything, just remember this.
do while execute atlease once but while doesn't.

var startNumber = 1
var sum = 0
do{
    println("startNumber is now $startNumber")
    sum += startNumber
    println("sum is $sum")
    startNumber++
}while(startNumber <= 10)

output

startNumber is now 1
sum is 1
startNumber is now 2
sum is 3
startNumber is now 3
sum is 6
startNumber is now 4
sum is 10
startNumber is now 5
sum is 15
startNumber is now 6
sum is 21
startNumber is now 7
sum is 28
startNumber is now 8
sum is 36
startNumber is now 9
sum is 45
startNumber is now 10
sum is 55

so while and do while are not so different.
but remember do while atleast one time.

Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.