Reputation: 60081
I want to loop through x times.
for (i in 0 until x - 1) {
// Do something.
}
But I don't need to use the i
. Is there a way better way to write the for-loop, without need to set the i
?
Upvotes: 4
Views: 2598
Reputation: 272467
A range is iterable. So to loop exactly x
times, something like this would work:
(0 until x).forEach {
// ...
}
An even simpler approach is this:
repeat(x) {
// ...
}
Note that in both cases, the index is still accessible via the implicit it
lambda parameter.
Upvotes: 15