Kotlin for loop (original) (raw)

Last Updated : 18 May, 2025

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.

Table of Content

Range Using a for loop

You can traverse through the **Rangebecause 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.

fun main(args: Array) { for (i in 1..6) { print("$i ") } }

`

**Output

1 2 3 4 5 6

fun main(args: Array) { for (i in 1..10 step 3) { print("$i ") } }

`

**Output

1 4 7 10

fun main(args: Array) { for (i in 5..1) { print("$i ") } println("It prints nothing") }

`

**Output

It prints nothing

fun main(args: Array) { 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) { 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:

fun main(args: Array) { 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

fun main(args: Array) {

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

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

}

`

**Output

Earth
Mars
Venus
Jupiter
Saturn

fun main(args: Array) { var planets = arrayOf("Earth", "Mars", "Venus", "Jupiter", "Saturn")

for ((index,value) in planets.withIndex()) {
    println("Element at <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi><mi>n</mi><mi>d</mi><mi>e</mi><mi>x</mi><mi>t</mi><mi>h</mi><mi>i</mi><mi>n</mi><mi>d</mi><mi>e</mi><mi>x</mi><mi>i</mi><mi>s</mi></mrow><annotation encoding="application/x-tex">index th index is </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">in</span><span class="mord mathnormal">d</span><span class="mord mathnormal">e</span><span class="mord mathnormal">x</span><span class="mord mathnormal">t</span><span class="mord mathnormal">hin</span><span class="mord mathnormal">d</span><span class="mord mathnormal">e</span><span class="mord mathnormal">x</span><span class="mord mathnormal">i</span><span class="mord mathnormal">s</span></span></span></span>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

Stringusing 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:

fun main(args: Array) { 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 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi><mi>n</mi><mi>d</mi><mi>e</mi><mi>x</mi><mi>t</mi><mi>h</mi><mi>i</mi><mi>n</mi><mi>d</mi><mi>e</mi><mi>x</mi><mi>i</mi><mi>s</mi></mrow><annotation encoding="application/x-tex">index th index is </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">in</span><span class="mord mathnormal">d</span><span class="mord mathnormal">e</span><span class="mord mathnormal">x</span><span class="mord mathnormal">t</span><span class="mord mathnormal">hin</span><span class="mord mathnormal">d</span><span class="mord mathnormal">e</span><span class="mord mathnormal">x</span><span class="mord mathnormal">i</span><span class="mord mathnormal">s</span></span></span></span>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) {

// 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