In Kotlin, for loop is equivalent to foreach loop of other languages like C#. Here for loop is used to traverse through any data structure which provides an iterator. It is used very differently then the for loop of other programming languages like Java or C.
The syntax of for loop in Kotlin:
for(item in collection) {
// code to execute
}
In Kotlin, for loop is used to iterate through the following because all of them provides iterator.
- Range
- Array
- String
- Collection
Iterate through range using for loop –
You can traverse through Range because it provides iterator. There are many ways you can iterate through Range. The in operator used in for loop to check value lies within the Range or not.
Below programs are example of traversing the range in different ways and in is the operator to check the value in the range. If value lies in between range then it returns true and prints the value.
- Iterate through range to print the values:
fun main(args: Array<String>)
{
for (i in 1 .. 6 ) {
print( "$i " )
}
}
|
Output:
1 2 3 4 5 6
- Iterate through range to jump using step-3 :
fun main(args: Array<String>)
{
for (i in 1 .. 10 step 3 ) {
print( "$i " )
}
}
|
Output:
1 4 7 10
- You can not iterate through Range from top to down without using DownTo :
fun main(args: Array<String>)
{
for (i in 5 .. 1 ) {
print( "$i " )
}
println( "It prints nothing" )
}
|
Output:
It prints nothing
- Iterate through Range from top to down with using downTo :
fun main(args: Array<String>)
{
for (i in 5 downTo 1 ) {
print( "$i " )
}
}
|
Output:
5 4 3 2 1
- Iterate through Range from top to down with using downTo and step 3:
fun main(args: Array<String>)
{
for (i in 10 downTo 1 step 3 ) {
print( "$i " )
}
}
|
Output:
10 7 4 1
Iterate through array using 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 you can traverse array:
- Without using Index property
- With Using Index property
- Using withIndex Library Function
Iterate through string using for loop –
A string can be traversed using the for loop because it also provides iterator.
There are following ways to traverse the string:
- Without using Index property
- With Using Index property
- Using withIndex Library Function
fun main(args: Array<String>) {
var name = "Geeks"
var name2 = "forGeeks"
for (alphabet in name) print( "$alphabet " )
for (i in name2.indices) print(name2[i]+ " " )
println( " " )
for ((index,value) in name.withIndex())
println( "Element at $index th index is $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
Iterate through collection using for loop –
You can traverse the collection using the for loop. There are three types of collections list, map and set.
In the listOf()
function we can pass the different data types at the same time.
Below is the program to traverse the list using for loop.
fun main(args: Array<String>) {
var collection = listOf( 1 , 2 , 3 , "listOf" , "mapOf" , "setOf" )
for (element in collection) {
println(element)
}
}
|
Output:
1
2
3
listOf
mapOf
setOf
Last Updated :
20 May, 2019
Like Article
Save Article
Vote for difficulty
Current difficulty :
Basic