English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Kotlin for Loop

In Kotlin, for loops iterate over any object that provides an iterator. In this article, you will learn how to create a for loop (with examples).

Unlike Java and other languages, Kotlin does not haveTraditional for loop.

In Kotlin, the for loop is used to iterate over ranges, arrays, maps, etc. (any object that provides an iterator).

The syntax of for loop in Kotlin is:

for (item in collection) {
    //Loop body
{}

Examples: Iterating over a range

fun main(args: Array<String>) {
    for (i in 1..5) {
        println(i)
    {}
{}

Here, the loop iterates over the range and prints a single item.

The output when running this program is:

1
2
3
4
5

If the body of the loop contains only one statement (as in the above example), you do not need to use braces { }.

fun main(args: Array<String>) {
    for (i in 1..5) println(i)
{}

You can use a for loop to iterate over a range because range provides an iterator.

Examples: Different ways to iterate over a range

fun main(args: Array<String>) {
    print("for (i in 1..5) print(i) = ")
    for (i in 1..5) print(i)
    println()
    print("for (i in 5..1) print(i) = ")
    for (i in 5..1) print(i)             // prints nothing
    println()
    print("for (i in 5 downTo 1) print(i) = ")
    for (i in 5 downTo 1) print(i)
    println()
    print("for (i in 1..4 step 2) print(i) = ")
    for (i in 1..5 step 2) print(i)
    println()
    print("for (i in 4 downTo 1 step 2) print(i) = ")
    for (i in 5 downTo 1 step 2) print(i)
{}

The output when running this program is:

for (i in 1..5) print(i) = 12345
for (i in 5..1) print(i) = 
for (i in 5 downTo 1) print(i) = 54321
for (i in 1..4 step 2) print(i) = 135
for (i in 4 downTo 1 step 2) print(i) = 531

Iterate over the array

This is an example of iterating over a String array.

fun main(args: Array<String>) {
    var language = arrayOf("Ruby", "Kotlin", "Python", "Java")
    for (item in language)
        println(item)
{}

The output when running this program is:

Ruby
Kotlin
Python
Java

You can iterate over an array with indices. For example,

fun main(args: Array<String>) {
    var language = arrayOf("Ruby", "Kotlin", "Python", "Java")
    for (item in language.indices) {
        //print elements of the array with even indices only
        if (item2 == 0) {}}
            println(language[item])
        {}
        
    {}
{}

The output when running this program is:

Ruby
Python

Iterate over the string

fun main(args: Array<String>) {
    var text = "Kotlin"
    for (letter in text) {
        println(letter)
    {}
{}

The output when running this program is:

K
o
t
l
i
n

Similar to arrays, you can use indices to iterate over strings. For example,

fun main(args: Array<String>) {
    var text = "Kotlin"
    for (item in text.indices) {
        println(text[item])
    {}
{}

The output when running this program is:

K
o
t
l
i
n