Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For-loop condition conventions [closed]

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);
like image 213
Genesis Rock Avatar asked Feb 04 '16 10:02

Genesis Rock


1 Answers

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..

like image 58
Marcin Szymczak Avatar answered Sep 22 '22 10:09

Marcin Szymczak