Kotlin for Loop

In this tutorial you will learn about the Kotlin for Loop and its application with practical example.

Kotlin for Loop

The for loop takes a collection of data(ranges, arrays, collections, or anything that provides an iterator) and iterate through the items one at a time in sequence.

Syntax :-

For Loop to Iterate Through an Array

Example :-

Output :-

The array can also iterated through array indices (index) of array.

Example:-

Output:-

Alternatively, you can use the withIndex library function as following –

Example:-

Output:-

For loop Iterate Through a Range

Example:-

Output:-

Different Ways to Iterate Through a Range

Example:-

Output:-

For loop Iterate Through a String

Example:-

Output:-

 

In this tutorial we have learn about the Kotlin for Loop and its application with practical example. I hope you will like this tutorial.