Generally, the for loop is used to iterate through the given block of code for the specified number of times.
In Kotlin, the for loop works like the forEach in C#.
The for loop in Kotlin is used to iterate through anything that provides an iterator. For example, Kotlin array, range, string, etc.
I will show you examples of for loop in Kotlin with range, array, list and string. First, let us have a look at the syntax.
Syntax of for loop in Kotlin
The general way of using the for loop is:
You may also provide a block of code by using curly braces:
for (item in collection) {
// statement to execute
}
An example of using a for loop with range
In the first example of using the for loop in Kotlin, I am using a range from 3 to 10.
Inside the loop body, the println() is used to display the current number of the range.
See the code and output below:
fun main(args: Array<String>) { for(num in 3..10){ println(num) } }
The output of the above code is:
3
4
5
6
7
8
9
10
Using until with for loop example
The “until” returns a range from this value to excluding the max value.
The example below shows using the until in the for loop and again we will display the numbers:
The Kotlin code:
fun main(args: Array<String>) { for(n in 3 until 10){ println(n) } }
The output:
3
4
5
6
7
8
9
You can see, the 10 is not displayed, unlike the first range example.
Using step function example
In this for loop example, I used a range with the step() function.
A range from 0 to 15 is given with the step of 3; see how for loop displays the numbers:
fun main(args: Array<String>) { for (num in 0..15 step 3) { print("$num ") } }
The result:
The example of iterating over a string
In this example, we will use a string in the for loop and display it:
fun main(args: Array<String>) { var a_str= "For Loop" for (s in a_str) { println(s) } }
The output:
F
o
r
L
o
o
p
Using index property example
This example shows using a string and its index property to iterate through:
fun main(args: Array<String>) { var a_str= "using index" for (item in a_str.indices) { println(a_str[item]) } }
The output:
u
s
i
n
g
i
n
d
e
x
For loop example with a string using withIndex library function
In this example, we will iterate through a string using the withIndex library function:
fun main(args: Array<String>) { var str = "string demo" for ((i, v) in str.withIndex()) { println("$i"+ "index = $v") } }
The output:
How to iterate through an array using for loop
Now, let us have a look at the example of using an array and for loop. An array of four items is created which is followed by iterating through its items using a for loop:
fun main(args: Array<String>) { var arr_cities = arrayOf("New York", "London", "Paris" , "Mecca") for (item in arr_cities) println(item) }
The output:
New York
London
Paris
Mecca
You can see the array items are displayed without using the index property. You may also use the index property to iterate through Kotlin array as shown in the example below.
Using array index property in for loop example
This example uses the index property in the for loop:
fun main(args: Array<String>) { var arr_countries = arrayOf("USA", "UK", "France" , "UAE") for (index in arr_countries.indices) { println(arr_countries[index]) } }
The output:
USA
UK
France
UAE
Using withIndex library function example
The for loop can also be used with the withIndex() property to iterate through arrays:
fun main(args: Array<String>) { var arr_names = arrayOf("Mike", "Tine", "Rob" , "Jack") for ((ind,v) in arr_names.withIndex()) { println("$ind" + " Index = $v") } }
The output:
0 Index = Mike
1 Index = Tine
2 Index = Rob
3 Index = Jack
A for loop example with mutable list
In the following example, a mutable Kotlin list of five items is created and then a for loop is used to iterate through that list and display its items:
fun main(args: Array<String>) { val a_list = mutableListOf("A", "B", "C", "D", "E") val iterator = a_list.listIterator() for (item in iterator) { println("$item") } }
The output:
A
B
C
D
E
Summary
In this tutorial on Kotlin for loop, we learned that the for is a different type of loop than in other languages like Java. This is more like the forEach loop in C#.
We saw using the for loop with ranges, strings, arrays, and lists i.e. a for loop can be used with anything that provides an iterator.