VOOZH about

URL: https://www.geeksforgeeks.org/kotlin/kotlin-for-loop/

⇱ Kotlin for loop - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Kotlin for loop

Last Updated : 18 May, 2025

In Kotlin, the for loop is equivalent to the foreach loop of other languages like C#. Here for loop is used to traverse through any data structure that provides an iterator. It is used very differently then the for loop of other programming languages like Java or C. The syntax of the for loop in Kotlin:

Syntax

for(item in collection) {
// code to execute
}

In Kotlin, a for loop is used to iterate through the following because all of them provide an iterator.


Range Using a for loop

You can traverse through the Range because it provides an iterator. There are many ways you can iterate through a Range. The 'in' operator is used in a for loop to check value lies within the Range or not. The following programs are examples of traversing the range in different ways, and 'in' is the operator to check the value in the range. If the value lies between the range, then it returns true and prints the value.

  • Iterate through the range to print the values:


Output

1 2 3 4 5 6


  • Iterate through the range to jump using step 3:


Output

1 4 7 10


  • You can not iterate through a Range from top to down without using DownTo :


Output

It prints nothing


  • Iterate through the Range from top to down with using downTo:


Output

5 4 3 2 1


Iterate through the Range from top to down with using downTo and step 3:


Output

10 7 4 1



Array using a 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 can traverse the array:

  • Traverse an array without using the index property


Output

2 4 6 8 10


  • Traverse an array using the index property


Output

Earth
Mars
Venus
Jupiter
Saturn


  • Traverse an array using withIndex() Library Function


Output

Element at 0 th index is Earth
Element at 1 th index is Mars
Element at 2 th index is Venus
Element at 3 th index is Jupiter
Element at 4 th index is Saturn


String using a for loop

A string can be traversed using the for loop because it also provides an iterator.

There are the following ways to traverse the string:

  • Without using the Index property
  • Using the Index property
  • Using with Index Library Function


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


collection using a for loop

You can traverse the collection using a for loop. There are three types of collections: list, map, and set. In the listOf() We can pass the different data types at the same time.

Below is the program to traverse the list using a for loop.


Output

1
2
3
listOf
mapOf
setOf


Comment
Article Tags:

Explore