3

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?

5
  • 1
    what is the advantage of the for loop? Commented Jul 1, 2013 at 7:00
  • 1
    possible duplicate of Java for loop vs. while loop. Performance difference? Commented Jul 1, 2013 at 7:01
  • 2
    for loops and while loops are the same. Commented Jul 1, 2013 at 7:01
  • It's ok that performance is the same. Maybe there aresome other downsides of a for loop? Commented Jul 1, 2013 at 7: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. Commented Jul 1, 2013 at 8:16

5 Answers 5

2

There is no disadvantage. But for the below case using while loop is more conventional

bool condition = false;
while(!condition)
{

}
Sign up to request clarification or add additional context in comments.

1 Comment

Seems like there is really no disadvantages since there are only one not-offtopic answer.
2

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.

Comments

1

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();
}

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.