We break down how
forEach differs from the
for loop in Kotlin: why
forEach is a function, why it returns
Unit, where side effects (printing, logging, function calls) are appropriate, and where it is better to choose collection transformations. We also look at convenient patterns like
?.
forEach for nullable collections, the callable reference
::
println, and common mistakes: expecting a “result”, accumulating hidden state, and trying to use
break/
continue.