James A. N. Stauffer
James A. N. Stauffer

Reputation: 2659

Iterate with for loop or while loop?

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

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

Upvotes: 46

Views: 68109

Answers (15)

Abhishek Kaushik
Abhishek Kaushik

Reputation: 1159

Using for loop you can work with a single variable, as it sets the scope of variable for a current working for loop only. However this is not possible in while loop. For Example:
int i; for(i=0; in1;i++) do something..

for(i=0;i n2;i+=2) do something.

So after 1st loop i=n1-1 at the end. But while using second loop you can set i again to 0. However

int i=0;

while(i less than limit) { do something ..; i++; }

Hence i is set to limit-1 at the end. So you cant use same i in another while loop.

Upvotes: 1

Giorgio Galante
Giorgio Galante

Reputation: 295

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<String> names = new ArrayList<String>();
names.add("a");
names.add("b");
names.add("c");

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

Upvotes: 7

Igor Vaschuk
Igor Vaschuk

Reputation: 2924

IMHO, the for loop is less readable in this scenario, if you look at this code from the perspective of English language. I am working on a code where author does abuse for loop, and it ain't pretty. Compare following:

for (; (currUserObjectIndex < _domainObjectReferences.Length) && (_domainObjectReferences[currUserObjectIndex].VisualIndex == index); ++currUserObjectIndex)
    ++currNumUserObjects;

vs

while (currUserObjectIndex < _domainObjectReferences.Length && _domainObjectReferences[currUserObjectIndex].VisualIndex == index)
{
    ++currNumUserObjects;
    ++currUserObjectIndex;
}

Upvotes: 2

Ron Savage
Ron Savage

Reputation: 11079

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.

Upvotes: 0

James Muscat
James Muscat

Reputation: 454

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.

Upvotes: 0

Mat Mannion
Mat Mannion

Reputation: 3365

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

Upvotes: 0

Robert Gould
Robert Gould

Reputation: 69825

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.

Upvotes: 1

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

Upvotes: 0

Jon Limjap
Jon Limjap

Reputation: 95432

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.

Upvotes: 16

Svet
Svet

Reputation: 3655

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.

Upvotes: 0

Dima
Dima

Reputation: 39389

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.

Upvotes: 5

Guy Starbuck
Guy Starbuck

Reputation: 21873

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.

Upvotes: 1

Steven A. Lowe
Steven A. Lowe

Reputation: 61233

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

Upvotes: 2

Lou Franco
Lou Franco

Reputation: 89152

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

Upvotes: 87

scubabbl
scubabbl

Reputation: 12797

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.

Upvotes: 0

Related Questions