Loop is a basic feature, that evaluates conditional expression, if the condition is met, iterates enumerable types, and executes code statements until the condition is met.

Enumerable types are Arrays, List, Set

Kotlin provides multiple-loop syntax to iterate types.

Kotlin For Loop tutorial

For in-loop used to iterate the collection. It returns an element for each iteration Code inside the body executed with an element Here is a syntax

for(object in Enumerable_Collection) {
    // body
}

Enumerable_Collection is of the predefined array, list, set, or range operator values

fun main() {
    var numbers=listOf("one", "two", "three")
    for(number in numbers) {
    println(number)
    }
}

Output:

one
two
three

Above Example,

  • List objects created with a range of values using the double dot operator
  • for in loop iterates list from start to end, each element is assigned to a temporary number variable
  • Print each element for each iteration

for loop example with a range of values in natural Order:

It uses a double dot (..) or triple dot(...) operator to have a range of values

Letโ€™s see an example of the range of values in the natural order

  • (1..5): contains values of 1,2,3,4,5
  • (1...5): contains values of 1,2,3,4
fun main(){
    println("๐Ÿ‘‡Range of values in ascending order ๐Ÿ‘‡")
    for(number in 1..5){
        println(number)
    }
}

Output:

๐Ÿ‘‡Range of values in ascending order ๐Ÿ‘‡
1
2
3
4
5

Letโ€™s see an example of the range of values in descending order

for loop example with a range of values in Descending Order:

downTo provides a progressive value that is down to another lesser value.

Takes higher value, decrement the value by 1 for each iteration

Here is an example for a range of values in reverse order

fun main(){
    println("๐Ÿ‘‡Range of values in descending order: ๐Ÿ‘‡")
    for(number in  5 downTo 1){
        println(number)
    }
}

Output:

๐Ÿ‘‡Range of values in descending order: ๐Ÿ‘‡
5
4
3
2
1

Kotlin For loop starts from a given index to iterate arrays

Sometimes, We need to iterate an array using an index. until is used to check condition is met or not. For example, 1 until 4 executes 1 to 4 values.

Here is an example of iterating an array and list with index syntax.

fun main(){
    println("๐Ÿ‘‡Iterate loop Array with given index: ๐Ÿ‘‡")
   val array = arrayOf("1", "2", "3", "4", "5")
    for (i in 0 until array.size)
        println(array[i])

    println("๐Ÿ‘‡Iterate loop List with given index: ๐Ÿ‘‡")

    var numbers=listOf("one", "two", "three")
    for(index in numbers.indices) {
        println(numbers[index])
    }


}

Output

๐Ÿ‘‡Iterate loop Array with given index: ๐Ÿ‘‡
1
2
3
4
5
๐Ÿ‘‡Iterate loop List with given index: ๐Ÿ‘‡
one
two
three

For Loop break example

During loop execution, Sometimes, need to exit or stop the loop execution based on the condition met.

So what is the use of break expression? Break expression allows stopping the control flow execution inside a for or while loops

The below example iterates the range of 1 to 10 values, if the number is 6, It stops the loop execution

fun main(){
    println("๐Ÿ‘‡For Loop break: ๐Ÿ‘‡")
    for(number in 1..10){
        if(number==6)break
        println("number: $number")

}

}

Output:

๐Ÿ‘‡For Loop break: ๐Ÿ‘‡
number: 1
number: 2
number: 3
number: 4
number: 5

For Loop Continue example

Continue is a keyword used to continue the execution based on a conditional expression.

The below example prints the values from 1 to 30 without continuing.

With continued conditional expression, It prints values in multiples of 5 between 1 and 30.

fun main(){
    println("๐Ÿ‘‡For Loop continue: ๐Ÿ‘‡")
    for(number in 1..30){
        if(number%5!=0)continue
        println("number: $number")
    }
}

Output:

๐Ÿ‘‡For Loop continue: ๐Ÿ‘‡
number: 5
number: 10
number: 15
number: 20
number: 25
number: 30

For Loop Iterate string example in Kotlin

Does for loop support string iteration in Kotlin?

Kotlin supports a for loop to iterate a string. The string is also an enumerable collection of characters

Above all syntaxes used to iterate string characters

Here is an example

fun main(){
    println("๐Ÿ‘‡For Loop String iteration: ๐Ÿ‘‡")
    var str="welcome"
    for(char in str){
        println("$char")
    }
}

Output:

๐Ÿ‘‡For Loop String iteration: ๐Ÿ‘‡
w
e
l
c
o
m
e