Kotlin for Loop: The for loop in Kotlin iterates through whatever gives an iterator. In this tutorial, you learn to create for loop (with the assistance of examples).

There is no traditional for loop in Kotlin dissimilar to Java and different languages.

The for loop in Kotlin is used to iterate or cycle however the elements of arrays, ranges, collections and so on

In Kotlin, for loop is used to emphasize through ranges, arrays, maps, etc (anything that gives an iterator).

The syntax of for loop in Kotlin is:

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

Example: Iterate Through a Range

fun main(args: Array<String>) {

    for (i in 1..5) {
        println(i)
    }
}

Here, the loop iterates through the range and prints singular thing.

At the point when you run the program, the output will be:

1
2
3
4
5

In the event that the body of the loop contains just a single statement (like above example), it’s not important to use curly braces { }.

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

It’s feasible to emphasize through a range using for loop since ranges gives an iterator.


Example: Different Ways to Iterate Through 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..5 step 2) print(i) = ")
    for (i in 1..5 step 2) print(i)

    println()

    print("for (i in 5 downTo 1 step 2) print(i) = ")
    for (i in 5 downTo 1 step 2) print(i)
}

At the point when you run the program, the output will be:

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..5 step 2) print(i) = 135
for (i in 5 downTo 1 step 2) print(i) = 531

Iterating Through an Array

Here’s an example to iterate through a String array.

fun main(args: Array<String>) {

    var language = arrayOf("Ruby", "Koltin", "Python" "Java")

    for (item in language)
        println(item)
}

At the point when you run the program, the output will be:

Ruby
Koltin
Python
Java

It’s feasible to emphasize through an array with a file. For instance,

fun main(args: Array<String>) {

    var language = arrayOf("Ruby", "Koltin", "Python", "Java")

    for (item in language.indices) {

        // printing array elements having even index only
        if (item%2 == 0)
            println(language[item])
    }
}

At the point when you run the program, the output will be:

Ruby
Python

In the event that you need to learn more about, visit Kotlin arrays.


Iterating Through a String

fun main(args: Array<String>) {

    var text= "Kotlin"

    for (letter in text) {
        println(letter)
    }
}

At the point when you run the program, the output will be:

K
o
t
l
i
n

Comparable like arrays, you can emphasize through a String with a list. For instance,

fun main(args: Array<String>) {

    var text= "Kotlin"

    for (item in text.indices) {
        println(text[item])
    }
}

At the point when you run the program, the output will be:

K
o
t
l
i
n

You will learn to iterate over a map using for loop in Kotin map tutorial.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.


0 Comments

Leave a Reply

Avatar placeholder