

The general syntax of the forEach loop function we used in Kotlin is given below:įoreEach function takes a condition as a parameter that decides what action should be taken in each collection specified. Syntax of the ForEach Function in Kotlin: The last iteration expression permits the index to be adjusted at each pass ends. The conditional expression then decides whether the loop is continued or not. It also includes an initialization statement that defines an initial value of an index. The function always starts with the modifier “forEach.” The for expression is a looping statement that is frequently used in kotlin.

What is ForEach Function in Kotlin:Īs the forEach function name suggested, it derived its name from the fact that it goes over each collection item one by one. The for loop and forEach loop are the same therefore, it is comparable to the function approach towards the traditional for loop techniques. In the article, we will have a clear and simple concept of using the forEach loop function in the kotlin language. The kotlin is a loop statement that is more conventionally used to make other loops like a while loop. The Kotlin forEach function can be used to perform any operation of the element in the specified collections.
