IMO, the first one is much easier to read, the second is just noise on top of it.
But also, in this situation, none of these are idiomatic, it would be better to use
Copy code
repeat(10) {
println(it)
}
CLOVIS
05/30/2024, 8:10 AM
Also, your article is named "stop using the wrong loop", but all it does is list all the different kinds of loops.
CLOVIS
05/30/2024, 8:14 AM
The
while
and
do … while
examples contradict your text: in both situations, you know exactly how many loops are going to be executed.
They should be written with
Copy code
for (x in 5 downTo 1) {
println(x)
}
c
Ch8n
05/30/2024, 8:20 AM
Thanks mate for feedback, aim was to help understand when each one is most appropriate to use by providing an overview of possible options available.
Definitely you can loop however you like, I like to be explicit about my intentions while looping and classification in this article helps me alot.
However, I appreciate your point.
m
Michael Krussel
05/30/2024, 11:44 AM
JetBrains style guide has guidance on when to use the for loop and the forEach function.