https://kotlinlang.org logo
#getting-started
Title
# getting-started
s

Saliou seck

12/14/2023, 12:36 PM
Hi all i have a question about kotlin loop foreach In Kotlin, is it possible for an instruction after a forEach loop to be executed before the forEach loop finishes its execution? Thanks!
s

Shahzad Ansari

12/14/2023, 12:42 PM
You could use
forEachIndexed
and perform some operations before or after the last iteration.
s

Saliou seck

12/14/2023, 12:51 PM
Thank's but I want to avoid check in every iteration if it is possible.
m

mkrussel

12/14/2023, 12:54 PM
Only other option I can think of is to drop the last item, and then process the last item after the forEach.
s

Saliou seck

12/14/2023, 1:00 PM
I think my question was not clear .
Copy code
val list = listOf(1, 2, 3, 4, 5)

list.forEach { element ->
    println(element)
}
println("Done")
with this code i want to know if the foreach is synchrone (compiler execute all print of foreach elements before printing done)?
m

mkrussel

12/14/2023, 1:01 PM
Then yes, done will not be printed before all the elements in the list have been processed.
👍 1
s

Saliou seck

12/14/2023, 1:02 PM
Thank's very much @Shahzad Ansari @mkrussel
🤝 1
2 Views