tags:

views:

2229

answers:

14

I often see code like:

Iterator i = list.iterator();
while(i.hasNext()) {
    ...
}

but I write that (when Java 1.5 isn't available or for each can't be used) as:

for(Iterator i = list.iterator(); i.hasNext(); ) {
    ...
}

because

  • It is shorter
  • It keeps i in a smaller scope
  • It reduces the chance of confusion. (Is i used outside the while? Where is i declared?)

I think code should be as simple to understand as possible so that I only have to make complex code to do complex things. What do you think? Which is better?

From: http://jamesjava.blogspot.com/2006/04/iterating.html

A: 

Either is fine. I use for () myself, and I don't know if there are compile issues. I suspect they both get optimized down to pretty much the same thing.

scubabbl
+30  A: 

I prefer the for loop because it also sets the scope of the iterator to just the for loop.

Lou Franco
Wasn't thinking of that. That's true.
scubabbl
I was 50/50 on that question but with the scope argument you made a point.
Pascal Paradis
+2  A: 

if you're only going to use the iterator once and throw it away, the second form is preferred; otherwise you must use the first form

Steven A. Lowe
+2  A: 

Why not use the for-each construct? (I haven't used Java in a while, but this exists in C# and I'm pretty sure Java 1.5 has this too):

List names = new ArrayList(); names.add("a"); names.add("b"); names.add("c");

for (String name: names) System.out.println(name.charAt(0));

Giorgio Galante
I suspect this is in the context of a pre-Java 5 codebase.
Aaron Maenpaa
I specified cases where it couldn't be used.
James A. N. Stauffer
The one time you can't use this construct is if you need to call Iterator.remove(), since the iterator is only ever inferred, not explicit.
Bill Michell
+1  A: 

I would agree that the "for" loop is clearer and more appropriate when iterating.

The "while" loop is appropriate for polling, or where the number of loops to meet exit condition will change based on activity inside the loop.

Guy Starbuck
+3  A: 

I think scope is the biggest issue here, as you have pointed out.

In the "while" example, the iterator is declared outside the loop, so it will continue to exist after the loop is done. This may cause issues if this same iterator is used again at some later point. E. g. you may forget to initialize it before using it in another loop.

In the "for" example, the iterator is declared inside the loop, so its scope is limited to the loop. If you try to use it after the loop, you will get a compiler error.

Dima
A: 

Although both are really fine, I tend to use the first example because it is easier to read.

There are fewer operations happening on each line with the while() loop, making the code easier for someone new to the code to understand what's going on.

That type of construct also allows me to group initializations in a common location (at the top of the method) which also simplifies commenting for me, and conceptualization for someone reading it for the first time.

Ron

Ron Savage
I think it is a good practice to declare variables only when they are needed. Imagine a big method: when you start using the declared variables, they might not even be on the same page anymore!
André Neves
A: 

I agree that the for loop should be used whenever possible but sometimes there's more complex logic that controls the iterator in the body of the loop. In that case you have to go with while.

Svet
+2  A: 

There are appropriate uses for the while, the for, and the foreach constructs:

  • while - Use this if you are iterating and the deciding factor for looping or not is based merely on a condition. In this loop construct, keeping an index is only a secondary concern; everything should be based on the condition

  • for - Use this if you are looping and your primary concern is the index of the array/collection/list. It is more useful to use a for if you are most likely to go through all the elements anyway, and in a particular order (e.g., going backwards through a sorted list, for example).

  • foreach - Use this if you merely need to go through your collection regardless of order.

Obviously there are exceptions to the above, but that's the general rule I use when deciding to use which. That being said I tend to use foreach more often.

Jon Limjap
A: 

I was the for loop for clarity. While I use the while loop when faced with some undeterministic condition.

+1  A: 

Not that it probably matters in this case, but Compilers, VMs and CPU's normally have special optimization techniques they user under the hood that will make for loops performance better (and in the near future parallel), in general they don't do that with while loops (because its harder to determine how it's actually going to run). But in most cases code clarity should trump optimization.

Robert Gould
A: 

Both are fine, but remember that sometimes access to the Iterator directly is useful (such as if you are removing elements that match a certain condition - you will get a ConcurrentModificationException if you do collection.remove(o) inside a for(T o : collection) loop).

I prefer to write the for(blah : blah) [foreach] syntax almost all of the time because it seems more naturally readable to me. The concept of iterators in general don't really have parallels outside of programming

Mat Mannion
A: 

Academia tends to prefer the while-loop as it makes for less complicated reasoning about programs. I tend to prefer the for- or foreach-loop structures as they make for easier-to-read code.

James Muscat
A: 

ITERATIVE while loop: . . . with limited scope

/*LOOP_DESCRIPTION*/ {

    Iterator iter = list.iterator();

    while (iter.hasNext()) {

     // loop body
    }
}

One good thing about this layout is it reads badly without comments, thereby encouraging them.

LINK TO: Variable Naming Suggestions for Loops

_ande_turner_