×

Search anything:

Learn to use For loop in Kotlin

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

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. For loop is a commonly used type of loop that is supported in Kotlin and we will learn about it in this article.

Looping is something we familiar. Kotlin have three types of loops namely:

  • for
  • while
  • do while

In this article, we will take a deep look into for loops in Kotlin.

while and do while are self-explanatory, then how about for ?

For

    for (i in 1..10){
        println("Hello $i time")
    }

output

Hello 1 time
Hello 2 time
Hello 3 time
Hello 4 time
Hello 5 time
Hello 6 time
Hello 7 time
Hello 8 time
Hello 9 time
Hello 10 time

here the explaination

  1. print out Hello # time
  2. here i is likes a place holder.
  3. 1..10 means 1 to 10 (10 also include)
    for (i in 10..1){
        println("Hello $i time")
    }

so above code block should work as well, right?
but that code block will print out nothing.
The exact meaning of above code block is
print out Hello # time backward from 10 to 1.
The problem here is kotlin don't work that way.
if you want to used backward here is working way.

    for (i in 10 downTo 1){
        println("Hello $i time")
    }

output

Hello 10 time
Hello 9 time
Hello 8 time
Hello 7 time
Hello 6 time
Hello 5 time
Hello 4 time
Hello 3 time
Hello 2 time
Hello 1 time
  1. 10 downTo 1 means from 10 to 1

So here the another example

    for (i in 1 until 10){
        println("Hello $i time")
    }

output

Hello 1 time
Hello 2 time
Hello 3 time
Hello 4 time
Hello 5 time
Hello 6 time
Hello 7 time
Hello 8 time
Hello 9 time

now only print out to 9 (otherwise until 10)
1 until 10 means starting from 1 until 10, but not include 10,

    for (i in 10 until 1){
        println("Hello $i time")
    }

above code will not work, likes .. (range operator) until doesn't work backward too.

    for (i in 1..10 step 2){
        println("Hello $i time")
    }

output

Hello 1 time
Hello 3 time
Hello 5 time
Hello 7 time
Hello 9 time

here we used step function.
looks at the code you will understand,
but how about step 1 instead of step 2
Here is the kotlin online ide

comment the output below

now we know something about for, but did you noticed we always know exact amount of time we want to loop over. so here the scenario,
how to loop over if we don't know the exact number to loop?

    var planets = listOf("Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune")
    for (planet in planets){
        println(planet)
    }

output

Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune

here the explaination

  1. we have a list of planets from our solar system in order.
  2. we iterate(loop) through planets and print out each and every single item from the list of planets list.

here what you needed to do

    var planets = listOf("Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune")
    for (HERE in planets){
        println(HERE)
    }

change your name at the place of HERE.
comment your output below you will find something interesting.

another task

  1. make a list of the cities from your country, or your ex name :)
  2. iterate(loop) through as you saw above code block.
  3. have fun.
    BTW you don't needed to comment your output of ex name list

Did you noticed?

we have a list of planets of our solar system, but the list itself can grow as much as you want. so exact number is not important here. As long as there are item in our list. we can iterate(loop). we don't needed to know how many time we want to loop.
Also for loop have short version too.

    var planets = listOf("Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune")
    for (planet in planets) println(planet)

you don't needed {} (code block) anymore, but if you have more than one sentence of code you still needed {} (code block).

Let's do something useful, How about total sum of 1 to 10

    var sum = 0
    for(value in 1..10){
        sum += value
    }
    println("Sum of 1 to 10 is $sum")

output

Sum of 1 to 10 is 55

sum += value is equal to sum = sum + value
here we iterate through 1 to 10 and add the total amount into sum.

another example

    var count = 0
    for(value in 1..10){
        if (value % 2 == 0){
            count++
        }
    }
    println("Even number count between 1 to 10 is $count")

output

Even number count between 1 to 10 is 5

we loop from 1 to 10 and check the remainder of value is equal to 0(that means it's even number) and then we increased the count.
So we got the how many even number between 1 to 10.
we can even inproved this code to know the exact even number. Let's see

    val evenList = mutableListOf<Int>()
    for(value in 1..10){
        if (value % 2 == 0){
            evenList.add(value)
        }
    }
    println("Even number between 1 to 10 is $evenList")

output

Even number between 1 to 10 is [2, 4, 6, 8, 10]

So I cover a lot about for loop. Hoped you get the idea of the for loop, If you don't question comment below.

Learn to use For loop in Kotlin
Share this