Hi all i have a question about kotlin loop foreach...
# getting-started
s
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
You could use
forEachIndexed
and perform some operations before or after the last iteration.
s
Thank's but I want to avoid check in every iteration if it is possible.
m
Only other option I can think of is to drop the last item, and then process the last item after the forEach.
s
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
Then yes, done will not be printed before all the elements in the list have been processed.
👍 1
s
Thank's very much @Shahzad Ansari @mkrussel
🤝 1