What is Kotlin for loop?

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 can be used to iterate through anything that provides an iterator. For example, a range, array, string, etc.

I will show you the examples of for loop in Kotlin with range, array, and string etc. First, let us have a look at the syntax.

Syntax of for loop in Kotlin

The general way of using the for loop is:

for (item in collection)  print(item)

You may also provide a block of code by using curly braces:

for (item in collection) {

// statement to execute

}

The 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:

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:

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:

The result:

0 3 6 9 12 15

The example of iterating over a string

In this example, we will use a string in the for loop and display it:

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:

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:

The output:

Kotlin for loop

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:

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.

Also, notice the usage of println() without the curly braces as we just executed one line of code.

Using array index property in for loop example

This example uses the index property in the for loop:

The output:

USA

UK

France

UAE

Using withIndex library function example

The for loop can also be used with the withIndex() property to iterate arrays:

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 list of five items is created and then a for loop is used to iterate through that list and displaying its items:

The output:       

A

B

C

D

E

Summary

In this tutorial of Kotlin for loop, we learned that the for is a different type of loop then in other languages like Java. This is more like the forEach loop in C# etc.

We saw using the for loop with ranges, strings, arrays, and list i.e. a for loop can be used with anything that provides an iterator.