Javatpoint Logo
Javatpoint Logo

Kotlin for Loop

Kotlin for loop is used to iterate a part of program several times. It iterates through arrays, ranges, collections, or anything that provides for iterate. Kotlin for loop is equivalent to the foreach loop in languages like C#.

Syntax of for loop in Kotlin:

Iterate through array

Let's see a simple example of iterating the elements of array.



If the body of for loop contains only one single line of statement, it is not necessary to enclose within curly braces {}.

The elements of an array are iterated on the basis of indices (index) of array. For example:


marks[0]: 80
marks[1]: 85
marks[2]: 60
marks[3]: 90
marks[4]: 70

Iterate through range

Let's see an example of iterating the elements of range.


for (i in 1..5) print(i) = 12345
for (i in 5..1) print(i) = 
for (i in 5 downTo 1) print(i) = 54321
for (i in 5 downTo 2) print(i) = 5432
for (i in 1..5 step 2) print(i) = 135
for (i in 5 downTo 1 step 2) print(i) = 531

Next TopicKotlin while Loop

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA