I am new the Kotlin programming and getting confused about returning something from a lambda. I think I understood the most but the following code is really creating problem to me.
When it is written that if(it==0) return@forEach then it should mean that return out of the forEach loop. Or in other words, exit the forEach loop. But it still continues to check the remaining items in the list.
Here is the code I am following
fun main(args: Array<String>) {
val listOf10 = listOf(2, 4, 0, 9, 8)
listOf10.forEach {
if (it == 0) return@forEach
println(it)
}
}
The expected output is 2,4 but it gives 2,4,9,8.
Can anyone here help me out with this?
Thank you
return@forEachdoes not mean to return from theforEach()function. It means returning from the lambda passed toforEach().