Kotlin For Loop Tutorial With Example

In this post, we will learn how to use Kotlin for loop. We will learn how to use for loop in kotlin to traverse through anything. For loop in kotlin is used to iterate through anything that provides iterator. We have also shown some examples that explains how to use for loop in kotlin.

Unlike c or java or other programming languages, for loop is used differently in kotlin.

Syntax to use Kotlin for loop is –

for(item in collection) {
	// Code to perform any action in item of the collection. 
}

In Kotlin, you can use for loop to iterate through following things –
Range
Array
String
Collection

Remember we can iterate through anything using kotlin for loop if it provides iterator. Here, Range, Array, String etc. provide iterator. So, we can iterate through it using for loop.

Iterate Or Traverse Through Range using for loop in Kotlin

We have given an example to show how can you traverse through Range in Kotlin. However, there are many more ways through which you can iterate through Range.

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

When you run the program, you will get below output
1
2
3
4
5
6
7
8
9
10

Here, we are iterating through range 1 to 10 using for loop. Then, printing each item in range.

Did you know?

you can remove curly brackets if there is only one line of statement within block. It means it’s not necessary to use curly bracket when there is only line of code in for loop block in kotlin. For Example, you can also write above program as below.

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

When you run the program, you will get below output
1
2
3
4
5
6
7
8
9
10

Different ways to iterate through Range using for loop in Kotlin

Now, we will see how you can iterate through Range in different ways.

1. Guess the output of below program

fun main(args: Array<String>) {
	for (item in 1..6) print(item)
}

2. Guess the output of below program

fun main(args: Array<String>) {
	for (item in 6..1) print(item)
}

Output:

3. Guess the output of below program

fun main(args: Array<String>) {
	for (item in 6 downTo 1) print(item)
}

4. Guess the output of below program

fun main(args: Array<String>) {
	for (i in 1..6 step 2) print(i)
}

5. Guess the output of below program

fun main(args: Array<String>) {
	for (i in 6 downTo 1 step 2) print(i)
}



Iterate Or Traverse Through An Array Using For Loop In Kotlin

In Kotlin, you can traverse through an array in following ways –
a. Without using Index property
b. Using Index property
c. Using withIndex Library Function

a. Iterate Array Without Using Index Property

Syntax to iterate through array, using kotlin for loop without using index property, is as below –

for(itemInArray in Array) {
	// Write code to perform the action with item….
}

– Example to show iteration through array using for loop in kotlin

fun main(args: Array<String>) {
	var nameList = arrayOf("Rajeev", "Rohan", "Rohit", "Roopesh")
	for (name in nameList)
	    println(name)
}

Output:
Rajeev
Rohan
Rohit
Roopesh

b. Iterate Through Array Using Index Property

Syntax to iterate through array, using kotlin for loop with index property, is as below –

fun main(args: Array<String>) {
	for (index in array.indices) {
	   // Write code to perform some action…. you can access array element at position index using array[index]
	}
}

– Example to show iteration through array using for loop in kotlin

fun main(args: Array<String>) {
	var books = arrayOf("Harry Potter", "Black Beauty", "The Alchemist", "The Ginger Man", "The Hite Report")
	for (index in books.indices) {
    		println(books[index])
	}
}

Output:
Harry Potter
Black Beauty
The Alchemist
The Ginger Man
The Hite Report

c. Iterate Through Array Using withIndex() Library Function

Syntax to iterate through array with kotlin for loop using withIndex library function is as below –

fun main(args: Array<String>) {
for ((index,value) in array.withIndex()) {
		// Write code to perform some action….
	}
}

– Example to show iteration through array using for loop in kotlin

fun main(args: Array<String>) {
	var books = arrayOf("Harry Potter", "Black Beauty", "The Alchemist", "The Ginger Man", "The Hite Report")
	for ((index,value) in books.withIndex()) {
	    println("$index" + "th position: $value")
	}
}

Output:
0th position: Harry Potter
1th position: Black Beauty
2th position: The Alchemist
3th position: The Ginger Man
4th position: The Hite Report

Iterate Or traverse Through a String using For Loop in Kotiln

You can iterate or traverse through a String using kotlin for loop in following ways –
a. Without using Index property
b. Using Index property
c. Using withIndex() library function.

a. Iterate String Without Using Index Property

Syntax to iterate String without using index property is as below –

for(letter in String) {
	// Write code to perform the action with letter….
}

– Example to show iteration through String using for loop in kotlin

fun main(args: Array<String>) {
	var name = "Tutorialwing"

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

Output:
T
u
t
o
r
i
a
l
w
i
n
g

b. Iterate String Using Index property

Like arrays, you can also iterate through String using index property in kotlin.
Syntax to iterate String with kotlin for loop using index property is as below –

fun main(args: Array<String>) {
	for (index in string.indices) {
	   // Write code to perform some action…. you can access string element at position index using string[index]
	}
}

– Example to show iteration through string using for loop in kotlin…

fun main(args: Array<String>) {
	var name = "Tutorialwing"

	for (index in name.indices) {
	    println(name[index])
	}
}

Output:
T
u
t
o
r
i
a
l
w
i
n
g




c. Iterate String Using withIndex() Library Function

Syntax to iterate through String with kotlin for loop using withIndex library function is as below –

fun main(args: Array<String>) {
for ((index,value) in string.withIndex()) {
		// Write code to perform some action….
	}
}

– Example to show iteration through String using for loop in kotlin

fun main(args: Array<String>) {
	var name = "Tutorialwing"

	for ((index, value) in name.withIndex()) {
	    println("$index"+ "th position: $value")
	}
}

Output:
0th position: T
1th position: u
2th position: t
3th position: o
4th position: r
5th position: i
6th position: a
7th position: l
8th position: w
9th position: i
10th position: n
11th position: g

Conclusion

In this post, we have seen how to use for loop in Kotlin. Now, we know that we can use kotlin for loop to iterate through ranges, arrays, strings etc. In fact, we can use for loop to iterate through anything that provides iterator in kotlin. Please make sure to check examples given in this tutorial. It will make your concepts clear about kotlin for loop.

Leave a Reply