Kotlin: repeat n time while condition is true
I need to repeat a piece of code a maximum of n times, but terminate the loop once a condition is false, like in the following:
val runs = 100
var ctr = 0
var condition = true
while (ctr++ < runs && condition) {
//stuff and updates
}
Can this logic be stated more idiomatic, maybe with repeat?
You can nest lambdas and non-locally return to the outer one - I won't say this is pretty but it's an option:
run loop@{
repeat(100) {
if(!condition) return@loop
// stuff and updates
}
}