evkaky
09/11/2021, 6:49 AMrepeat(3) {
if (it == 0) return@repeat
println("inside repeat")
}
it prints “inside repeat” twice instead of 0 times
Why is that?ephemient
09/11/2021, 6:54 AMrepeat(3, block)
≈ block(0); block(1); block(2)
return
prevents println()
when it == 0
, but not 1
or 2
evkaky
09/11/2021, 6:56 AMrepeat
or forEach
(like we can do in regular loops)?ephemient
09/11/2021, 7:01 AMrun {
repeat(3) {
if (it == 0) return@run
println("inside repeat")
}
}
but if that's necessary, IMO you should consider using a normal for
loop instead.
for (i in 0 until 3) {
if (i == 0) break
println("inside for")
}