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’s better to choose collection transformations. We also look at handy patterns like
?.
forEach for nullable collections, the callable reference
::
println, and typical mistakes: expecting a “result,” accumulating hidden state, and trying to use
break/
continue.