I had recently a discussion about the use of non-counter related conditions in for-loops in Java:
for(int i = 0; o.getC() < 10; i++)
o.addC(i);
Does anyone know if there are any "official" conventions for for-conditions like this? In my opinion it's easier to read compared to an equivalent while-loop because all loop-parameters are together in the first line:
int i = 0;
while(o.getC() < 10) {
i++;
o.addC(i);
}
Or even worse:
int i = 0;
while(o.getC() < 10)
o.addC(++i);
for
loops are used in pretty much every situation over equivalent while
solution. Arrays, lists, standard data structures.
On the other hand while
is commonly used with streams and for infinitely long iterations..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With