Kotlin ForEach

The Kotlin forEach function can be used to perform any operation of the element in the specified collections. The kotlin is a loop statement that is more conventionally used to make other loops like a while loop. In the article, we will have a clear and simple concept of using the forEach loop function in the kotlin language. The for loop and forEach loop are the same; therefore, it is comparable to the function approach towards the traditional for loop techniques.

What is ForEach Function in Kotlin:

As the forEach function name suggested, it derived its name from the fact that it goes over each collection item one by one. The function always starts with the modifier “forEach.” The for expression is a looping statement that is frequently used in kotlin. It also includes an initialization statement that defines an initial value of an index. The conditional expression then decides whether the loop is continued or not. The last iteration expression permits the index to be adjusted at each pass ends.

Syntax of the ForEach Function in Kotlin:

The general syntax of the forEach loop function we used in Kotlin is given below:

Variable_name.forEach {

Statements for forEach loop function


ForeEach function takes a condition as a parameter that decides what action should be taken in each collection specified. It allows iterating over each item inside a specified collection of lists and performing some particular function. We could also refer to an item within the forEach loop function code block with the “it” keyword. It is a practical way for performing the conventional task by using a loop path.

How to use the forEach function in Kotlin:

To understand the basics of using the forEach loop function in Kotlin language, we should take a look at the different case examples given below:

Example # 1: Program of ForEach function to iterate String in Kotlin:

We are using a forEach function that will iterate over each string, and each element of string in the collection will be printed. Let’s execute the simple program of using the forEach function in kotlin.

In the above code example, we have defined the main function we will perform forEach function. We created a variable with the keyword “var” and defined it with “myList.” The variable “myList” is initialized with a collection of lists by using the listOf function. The list is of string type property and contains three string elements. Then we have a called foreach function that will iterate over “myList” each element string. The kotlin println function is used in forEach function that has the “it” modifier passed as a single parameter.

The iteration by using the forEach function gives an output of each element’s list shown on the screen below.

Example # 2: Program of ForEach function to iterate Integer in Kotlin in Ubuntu 20.04:

We have a ForEach function for printing the elements in the array. The integers are used in an array that forEach function will iterate over.

In the above example code, we have declared the main function for executing forEach function for Integers. First, we have created a variable as “var” and defined the variable as “Array1” with the int property. The “Array1” is initialized with the array of integers by using the array method. The Array1 has forEach loop function applied. The forEach function will iterate each element in the array and return each integer of an array. We have used the kotlin println function in the forEach block for printing the integers.

Here we can see that the output of every integer element from the array is printed on the terminal screen below.

Example # 3: Program of ForEach function to create a custom object in Kotlin:

Using the forEach function, we will display a message for each element in a list. We have created a custom object in the below example code.

We have the main function declaration for the code execution in the above code. First, we have defined some variables. The variable is defined with the “val” keyword that tells the score value of the variable. Here we have “Novel1”, the variable stated with the string value. We have another variable, “Novel2,” defined with some string value and an integer value. There is a variable “myList,” which is taking “Novel1” and “Novel2” as a list collection. Then in the forEach function, we have “NovelName” and “NovelPrice,” which we have accessed from the class “Novel.” Through the kotlin println function, we can print the items in the list. We have defined a data class, “Novel,” which holds the variable “NovelName” and “NovelPrice” with the values assigned.

The data of class Novel is printed as output by using the forEach function in the image below.

Example # 4: Program of ForEachIndex function in Kotlin in Ubuntu 20.04:

We can also use the forEachIndexed() loop function instead of the forEach() loop function in the kotlin. The forEachIndexed is an inline function that takes an array as input and allows us to access its index and values independently.

We have a variable as “var” in the main function and assign the variable name as “colors.” The variable “colors” is initialized with a listOf function with four different string elements. Then, we have used a forEachIndex function which will traverse a list of colors and print the index value of an element and the element contained in the list.

The output shown in the image has an index value of the elements and the elements.


The main aim of the article is to give you the easy concept of function and expression in the kotlin language. The forEach function improves the performance of the code. For your better understanding, we have different examples of using the forEach function and demonstrate the use of forEachIndex over a forEach function. It will help you understand the implementation of the forEach function in kotlin.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.