Table of contents
1.
Introduction
2.
Kotlin for loop 
3.
Iterate through a Range
4.
Iterate through an Array
5.
Iterate through a String
6.
Frequently Asked Questions
6.1.
What is the starting index value of an array in Kotlin?
6.2.
Is Kotlin a statically typed language?
6.3.
How can you skip iteration in for loop?
7.
Conclusion
Last Updated: Aug 13, 2025
Easy

For Loop in Kotlin

Author Nagendra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hello Ninjas!! You must have heard about the very popular language used for Android DevelopmentKotlin. It is an open-source language developed by JetBrains. With the help of Kotlin, there has been a boost in developing android applications.

For loop in Kotlin Image

Today we will look into the details of Kotlin for loop. Let's get into the basics of Kotlin for loop.

Kotlin for loop 

The Kotlin for loop is used to iterate over a collection of items. The in operator is used with the for loop to iterate over each item present in the collection, arrays, etc. Let's look at the syntax of Kotlin for loop.

Syntax:

for (elements in collection) {
    // body of loop
}

Iterate through a Range

Let's look at various examples to understand iterating through a range of numbers using Kotlin for loop.

Iterate through a Range Image

Code:

fun main() {
    println("Iterating through a range of 40 to 45")
    for (items in 40..45) 
        println(items)
}


Output:

Iterating through a range of 40 to 45
40
41
42
43
44
45

 

Explanation:

The loop iterates through a range of 40 to 45 and prints every number.

 

You can also iterate through the range by skipping the numbers. This can be done using the step keyword used within the for() loop.

Code:

fun main() {
    println("Iterating through a range of 40 to 45 with 2 steps")
    for (items in 40..45 step 2) 
        println(items)
}

 

Output:

Iterating through a range of 40 to 45 with 2 steps
40
42
44


Explanation:

The loop iterates through a range of 40 to 45 and prints the number after 2 steps.

 

You can also backward iterate through the range of numbers. This is done using the downTo keyword.

Code:

fun main() {
    println("Backward Iterating through a range of 45 to 40")
    for (i in 45 downTo 40) 
        println(i)
}

 

Output:

Backward Iterating through a range of 45 to 40
45
44
43
42
41
40

 

Explanation:

The loop iterates backward through a range of 45 to 40. The downTo keyword is used for backward iterating through a range.

 

You can also backward iterate through the range by skipping the numbers. This can be done using the step keyword used within the for() loop.

Code:

fun main() {
    println("Backward Iterating through a range of 45 to 40 with 3 steps")
    for (i in 45 downTo 40 step 3) 
        println(i)
}

 

Output:

Backward Iterating through a range of 45 to 40 with 3 steps
45
42

 

Explanation:

The loop iterates backward through a range of 45 to 40 and prints every 3rd number.

Iterate through an Array

An array is a collection of similar data-type items stored at contiguous memory locations.

Iterate Through Array image

Let's look at various examples to understand iterating through an array using Kotlin for loop.

Code:

fun main() {
    var array = arrayOf("Hello!!", "Ninjas", "Happy", "Coding")
    println("The items in the array are:")
    for (items in array)
        println(items)
}

 

Output:

The items in the array are:
Hello!!
Ninjas
Happy
Coding

 

Explanation:

The arrayOf() method is used to convert a list of items to an array. The loop iterates through each element present in the array and prints them.
 

You can also iterate over the array using the index value of the elements in the array.

Code:

fun main() {
    var array = arrayOf("Hello!!", "Ninjas", "Happy", "Coding")
    println("The items in the array are:")
    for (items in array.indices)
        println(array[items])
}

 

Output:

The items in the array are:
Hello!!
Ninjas
Happy
Coding

 

Explanation:

The program iterates using the indices of elements of the array. The array.indices give the index value rather than the element value. 

Iterate through a String

String is defined as a collection of characters that are enclosed within quotes. 

Study Image

Let's look at various examples to understand iterating through a string using Kotlin for loop.

Code:

fun main() {
    var string = "Ninjas"
    println("The letters in the string are:")
    for (letter in string) 
        println(letter)
}

 

Output:

The letters in the string are:
N
i
n
j
a
s

 

Explanation:

The program iterates through every letter of the string and prints them. 

 

You can also iterate over the array using the index value of the letters in the string.

Code:

fun main() {
    var string = "Ninjas"
    println("The letters in the string are:")
    for (letter in string.indices) 
        println(string[letter])
}

 

Output:

The letters in the string are:
N
i
n
j
a
s


Explanation:

The program iterates using the indices of letters of the string. The string.indices gives the index value rather than element value. 

Must Read Elvis Operator Kotlin

Frequently Asked Questions

What is the starting index value of an array in Kotlin?

The starting index value of an array is 0.

Is Kotlin a statically typed language?

Yes, Kotlin is an open-source, statically typed programming language.

How can you skip iteration in for loop?

The step keyword is used to skip any number of iterations in Kotlin.

Conclusion

In this article, we have extensively discussed the details of executing for loop in Kotlin.

We hope that the blog has helped you enhance your knowledge regarding for loop in Kotlin. You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews. Do upvote our blogs to help other ninjas grow. 

Happy Coding!!

Live masterclass