I've read an article in the Effective Java about preferring For
loops to While
loops.
For
loops have a lot of advantages over While
loops. But is there any disadvantages in preferring For
to While
?

- 2,426
- 7
- 28
- 35
-
1what is the advantage of the `for` loop? – DevZer0 Jul 01 '13 at 07:00
-
1possible duplicate of [Java for loop vs. while loop. Performance difference?](http://stackoverflow.com/questions/1165457/java-for-loop-vs-while-loop-performance-difference) – Micha Wiedenmann Jul 01 '13 at 07:01
-
2`for` loops and `while` loops are the same. – Micha Wiedenmann Jul 01 '13 at 07:01
-
It's ok that performance is the same. Maybe there aresome other downsides of a `for` loop? – Chechulin Jul 01 '13 at 07:04
-
@TheNewIdiot this is not a duplicate as it does not focus on performance. It is about what is written in a book and why. Read my answer as there is a slight difference in variable visibility. – Uwe Plonus Jul 01 '13 at 08:16
5 Answers
There is no disadvantage. But for the below case using while loop is more conventional
bool condition = false;
while(!condition)
{
}

- 54,068
- 14
- 92
- 112
-
Seems like there is really no disadvantages since there are only one not-offtopic answer. – Chechulin Jul 01 '13 at 08:12
The Item 45 (in 2nd Edition of the book) talks about the scope of variables. To minimize the scope of a local variable the while
loop has a disadvantage:
boolean condition = false;
while (!condition) {
// do stuff
}
// Here the variable condition still exists
The for
loop can limit the visibility
for (boolean condition = false; !condition;) {
// do stuff
}
// Here the variable condition is out of scope and can be garbage collected
This is all that is preferable according to the book.

- 9,803
- 4
- 41
- 48
For loop is widly used and has more advantages over while loop but ther are some cases when while loop is perferable
Case 1.
when you are playing on booleans. In that case if you are using for loop you explicity define a variable to check or you creat for loop with only condition value in taht case while loop is preferrable
Boolean condition = false;
while(!condition)
{
condition = doSomething();
}
is preferrable then use of
Boolean condition = false;
for(;condition;)
{
condition = doSomething();
}
case 2.
for better visibilty and understanding. When you are working on iterators it is better to use while loop it gives to more clear view of code .
while (iterator.hasNext()) {
String string = (String) iterator.next();
}

- 3,018
- 2
- 23
- 46
The main advantage of a for loop over a while loop is readability. A For loop is a lot cleaner and a lot nicer to look at. It's also much easier to get stuck in an infinite loop with a while loop. I would say that I agree with your knowledge that if you can use a for loop, you should, so as long as you stick to that, your programming experiences will be much more enjoyable.

- 624
- 8
- 23
The first thing that comes to mind is JIT optimisations for for loops are easier so more likely to be applied i.e. if we know the number of times a loop will execute then we can unroll it.
Some discussions of loop optimisations and JIT

- 4,834
- 1
- 20
- 37