Open In App

Kotlin for loop

Last Updated : 18 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Kotlin, the for loop is equivalent to the foreach loop of other languages like C#. Here for loop is used to traverse through any data structure that provides an iterator. It is used very differently then the for loop of other programming languages like Java or C. The syntax of the for loop in Kotlin:

Syntax

for(item in collection) {
// code to execute
}

In Kotlin, a for loop is used to iterate through the following because all of them provide an iterator.


Range Using a for loop

You can traverse through the Range because it provides an iterator. There are many ways you can iterate through a Range. The 'in' operator is used in a for loop to check value lies within the Range or not. The following programs are examples of traversing the range in different ways, and 'in' is the operator to check the value in the range. If the value lies between the range, then it returns true and prints the value.

  • Iterate through the range to print the values:
Kotlin
fun main(args: Array<String>)
{
    for (i in 1..6) {
        print("$i ")
    }
}


Output

1 2 3 4 5 6


  • Iterate through the range to jump using step 3:
Kotlin
fun main(args: Array<String>)
{
    for (i in 1..10 step 3) {
        print("$i ")
    }
}


Output

1 4 7 10


  • You can not iterate through a Range from top to down without using DownTo :
Kotlin
fun main(args: Array<String>)
{
    for (i in 5..1) {
        print("$i ")
    }
    println("It prints nothing")
}


Output

It prints nothing


  • Iterate through the Range from top to down with using downTo:
Kotlin
fun main(args: Array<String>)
{
    for (i in 5 downTo 1) {
        print("$i ")
    }
}


Output

5 4 3 2 1


Iterate through the Range from top to down with using downTo and step 3:

Kotlin
fun main(args: Array<String>)
{
    for (i in 10 downTo 1 step 3) {
        print("$i ")
    }
}


Output

10 7 4 1



Array using a for loop

An array is a data structure which contains same data type like Integer or String. Array can be traversed using for loop because it also provides iterator. Each array has a starting index and by default, it is 0.

There are the following can traverse the array:

  • Traverse an array without using the index property
Kotlin
fun main(args: Array<String>) {
    var numbers = arrayOf(1,2,3,4,5,6,7,8,9,10)

    for (num in numbers){
        if(num%2 == 0){
            print("$num ")
        }
    }
}


Output

2 4 6 8 10


  • Traverse an array using the index property
Kotlin
fun main(args: Array<String>) {

    var planets = arrayOf("Earth", "Mars", "Venus", "Jupiter", "Saturn")

    for (i in planets.indices) {
        println(planets[i])
    }
}


Output

Earth
Mars
Venus
Jupiter
Saturn


  • Traverse an array using withIndex() Library Function
Kotlin
fun main(args: Array<String>) {
    var planets = arrayOf("Earth", "Mars", "Venus", "Jupiter", "Saturn")

    for ((index,value) in planets.withIndex()) {
        println("Element at $index th index is $value")
    }
}


Output

Element at 0 th index is Earth
Element at 1 th index is Mars
Element at 2 th index is Venus
Element at 3 th index is Jupiter
Element at 4 th index is Saturn


String using a for loop

A string can be traversed using the for loop because it also provides an iterator.

There are the following ways to traverse the string:

  • Without using the Index property
  • Using the Index property
  • Using with Index Library Function
Kotlin
fun main(args: Array<String>) {
    var name = "Geeks"
    var name2 = "forGeeks"
    
    // traversing string without using index property
    for (alphabet in name)   print("$alphabet ")

    // traversing string with using index property
    for (i in name2.indices) print(name2[i]+" ")
    println(" ")
    
    // traversing string using withIndex() library function
    for ((index,value) in name.withIndex())
    println("Element at $index th index is $value")
}


Output

G e e k s f o r G e e k s  
Element at 0 th index is G
Element at 1 th index is e
Element at 2 th index is e
Element at 3 th index is k
Element at 4 th index is s


collection using a for loop

You can traverse the collection using a for loop. There are three types of collections: list, map, and set. In the listOf() We can pass the different data types at the same time.

Below is the program to traverse the list using a for loop.

Kotlin
fun main(args: Array<String>) {

    // read only, fix-size
    var collection = listOf(1,2,3,"listOf", "mapOf", "setOf")

    for (element in collection) {
        println(element)
    }
}


Output

1
2
3
listOf
mapOf
setOf



Next Article
Article Tags :

Similar Reads