For now or While away?
FOR i between 1 and 20 step 3
was the same as
for (i = 1; i < 20; i += 3)
Where the for(;;) loop shines is in other boolean loops.
But consider:
for (o = first(); o != null; o = next())
Here we are rapidly losing readability. In truth the for(;;) loop was never very logical. Generations of IT teachers have had to explain that part one occurs above the loop, part two as the first statement of the loop and part 3 at the end. In an industry where logic rules, we have to take the for(;;)loop on faith. The above loop is easier and quicker to read as:
MyObject o = first();
while (o != null)
{
...
o = next();
}
By all means use for(;;)for simple iterations, but as soon as the logic becomes complicated, consider changing it to a while() loop.








0 Comments:
Post a Comment
<< Home