views:

2422

answers:

29

Possible Duplicates:
Iterate with for loop or while loop?
Loops in C - for() or while() - which is BEST?

When should one use a for loop instead of a while loop?

I think the following loops are identical, except for their syntax. If so, then why choose one over the other?

int i;
for (i = 0; i < arr.length; i++) {
  // do work
}

int i = 0;
while (i < arr.length) {
  // do work
  i++;
}
+29  A: 

I use 'while' when I don't necessarily need a counter, for example when you are reading from a file and you are waiting to reach EOF. In this case 'for' may not be the best alternative. When you are going through items on an array I'd rather use 'for' because it conveys intent better.

Otávio Décio
Yeah, using a while with a variable is *counter* intuitive.
Bill K
@Bill K, lol terrible joke
Gary
+1, true and practical.
Karthik
+68  A: 

There is one reason to choose for over while: Readability.

By using a for loop, you're expressly saying your intent is to perform some type of operating that requires an initialization, a "step" operation, and a completion condition.

With while, on the other hand, you're only saying you need the completion condition.

Reed Copsey
Is there really only *one* reason? What about the other one I mentioned below?
jjnguy
@Justin: I didn't say "Only one" - but frankly, I think your reason mainly falls under readability anyways...
Reed Copsey
@Reed, touche. It is mostly readability, true.
jjnguy
I think initialization at the time is most import for me. for(int i = 0; int j = 0 ; ; )
@Justin/Reed: I would argue that Justin's alternative is not only good for readability. The fact that you get to keep the `i` inside the scope of the loop is a good thing. Helps to keep the code clean!
Nailuj
in fact this very same reason is given in "the elements of programming style" by Kernighan and Plauger. a for loop is one of the programming idioms that the reader understands instantaneously.
akonsu
The for loop syntax won't let you forget to initialize or increment your counter. So in case of looping with counters for loop is not only more readable but more safe as well.
Unmesh Kondolikar
@Unmesh: In most languages, it's perfectly acceptable to do: for(;i<arr.Length;) { ++i; } - you can skip the initialization if you really want to (not that I would, but I don't know if I agree that it's a safety issue)
Reed Copsey
@Reed - with for(;i<arr.Length;) you are intentionally skipping the initialization and increment, not inadvertently. My point is that it is easier to miss initializing or incrementing your counter with While than For.
Unmesh Kondolikar
Another difference is changing counter and condition value in **while** but in **for** it's impossible to change it! So **for** is suitable for counting issues!
Jalal Amini
@Jalal: I completely disagree. You can change the counter and condition value inside of a for loop in most languages. There is no technical reason this isn't possible. Try this in C++ - it's perfectly valid--- int i = 0; int finish = 100; for (;i<finish;) { ++i; printf("%d",i); --finish; }
Reed Copsey
@reed: Sorry! I was mean in C#! Yea, your right! but changing counter value in *for* is not a good idea!!! Is it?
Jalal Amini
A: 

Use for-loop when it's appropriate. The while solution has syntactic drawbacks (it's longer) and you have to check that you increment i before any continue call within the loop.

dark_charlie
A: 

To be honest there is very little difference in performance between the two types of loops for most programming languages. Given that, the choice is really one of preference. I happen to prefer for loops over while loops.

Brian Driscoll
+66  A: 

In your case, you don't gain much besides one less line of code in the for loop.

However, if you declare the loop like so:

for(int i = 0; i < x; i++)

You manage to keep i within the scope of the loop, instead of letting it escape to the rest of the code.

Also, in a while loop, you have the variable declaration, the condition, and the increment in 3 different places. With the for loop, it is all in one convenient, easy-to-read place.

Last thought:
One more important note. There is a semantic difference between the two. While loops, in general, are meant to have an indefinite number of iterations. (ie. until the file has been read..no matter how many lines are in it), and for loops should have a more definite number of iterations. (loop through all of the elements in a collection, which we can count based on the size of the collection.)

jjnguy
... in some languages. In others, not.
Pointy
@sbi, thanks for fixing my typo.
jjnguy
`+1` from me for mentioning the scope of the counter variable.
sbi
is it desirable to keep i inside the for loop? Why?
Ziggy
@Ziggy: Higher locality of data in general is good. In this case, one advantage is that you can use the same counter variable name (the classic `i`) in different loops in the same scope. Also, if counter variables "live on" behind their loop, they might get "recycled" for later code, which then might, accidentally, depend on them having a specific value as a left-over from the loop. I guess one could come up with myriads of such scenarios.
sbi
@sbi , exactly.
jjnguy
@Ziggy Well, lets say you have more than one loop. With a for loop, all your loop counters are local to the loop. You can use a generic name (like 'i') in your loop without worrying about declaring a variable with the same name as one that already exists.
quanticle
@sbi: you can reuse the variable name in multiple loops regardless of whether you declared it inside or outside. In the latter case, you are reusing the variable itself. The problem is not that it's *impossible*, but that it's *risky*, as you might forget to reinitialise it.
Porculus
This is not always true. For example, Microsoft Visual C++ 6.0 will extend the scope of such variables. The only workaround is to `#define for if(false){} else for` which creates a scope inside which the `for` loop will run.
Benoit
@Benoit, what do you mean?
jjnguy
What I mean is that in Visual C++ 6.0, `for(int i = 0;;){} for(int i = 0;;){}` is illegal because the compiler will tell you that `i` is defined twice.
Benoit
@Benoit, No way! That makes no sense to me...
jjnguy
@Justin: see [this topic](http://stackoverflow.com/questions/984878/what-is-the-possible-use-for-define-for-if-false-else-for)
Benoit
@Benoit, wow, thanks. That really clears things up for me. It's a good thing that this is only a bug in an old version of VC++.
jjnguy
Actually newer versions of Visual Studio still implement this bug as a feature for backwards compatibility, but I cannot tell you whether it's default behaviour or not.
Benoit
@Porculus: Well, I claim that there's a difference between re-using a _name_ for a different object and reusing the _object_ itself. @Benoit: That used to be the behavior but about a decade after that was changed MS finally got around changing its compiler's default to implement the new behavior.
sbi
ActionScript lets the i escape too. Using it in two loops works, but you get a warning about a re-defined variable.
Lizzan
@Lizz, whoever thought that was a good idea was on drugs.
jjnguy
@quanticle @sbi ANSI C will not even allow you to declare `i` inside the for loop declaration. Even if a language does allow it, it is senseless to do so because you are just limiting yourself to how you can use `i`. As long as you don't declare `i` global and try to use it in two places at the same time (one of those places in a separate thread) then having a conflict regarding the usage of `i` would be pretty dang tough.
typoknig
@typoknig: Maybe C doesn't have this. So? The question didn't specify a language, so C++ is just as good as any other curly-braces language where the syntax fits. So let's treat this as language-agnostic. That leaves the question whether higher locality is desirable. I have stated my opinion and the reasons why I have it. As for your arguments: Very often, _limiting_ what you can do with something is very helpful. After all, the type system is just a set of limitations ("can't write a `float` into a dereferenced `int*`") and who would argue that the type system is to limiting?
sbi
@Justin I'm choosing this as the answer because Mr Nelson mentions scope, readability, and intention. Good job Mr Nelson!
Ziggy
A: 

Preference. For me all the looping logic is built into the for statement so you do not have to declare the counter variable or do the increment/decrement stuff in the actual loop. Also, the you can declare the counter/index local to the for statement.

Wix
If that is C/C++, you have to _define_ the counter variable, [not _declare_ it](http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration/1410632#1410632).
sbi
+3  A: 

They'll usually compile to the same end result. I prefer for's or for-each most of the time. Writing

for (;x<5;x++){ 
    //do work 
} 

isn't so sinister, but writing

while(x<5){
    x++
    //do work
}

instead of

while(x<5){
    //do work
    x++
}

could confuse some people and cause off-by-one errors.

Gary
+3  A: 

They are functionally identical, however, it can be argued that the for loop is less error prone, because all of the loop functionality is right there together. if you have several lines separating the declaration of i, the while declaration, and the index iterator, you could forget, or be confused by how it is used. This is all subjective of course.

Matthew Vines
+7  A: 

As you say, the reasons are semantical and aesthetical, rather than performance (their compiled code tends to be very similar).

That said, for is usual in cycles which have a known number of iterations, whereas while implies that the condition isn't necessarily correlated to what you're doing inside the cycle, or that you don't know at the beginning how many iterations there will be.

Piskvor
This is, I think, what I was secretly asking about. In a linear search, for instance, where by definition I am uncertain how long it will take, would you then use a while loop over a for loop?
Ziggy
@Ziggy: Ah, that depends on your intentions, mostly. If I were searching a structure of known dimensions, I'd use `for`, even though I may have to break out of it when I find the match. On the other hand, if I were searching in a stream without a known size, I'd use `while`.
Piskvor
+3  A: 

In the early times of programming a prevalent data structure was the linear array (aka vector) and a repeating pattern was to traverse and use each element of the array. So usual was this scenario that the languages included a specific syntax for this pattern: the for loop. Aside from this historical heritage you could use the while loop all right (well nowadays with functional languages like F# you'd rather use List.map or List.fold ;-)

Edgar Sánchez
+1  A: 

Generally, the difference between a while loop and for loop is syntax and not performance related. That said, it looks like this question was already addressed for Java: http://stackoverflow.com/questions/1165457/java-for-loop-vs-while-loop-performance-difference

simeonwillbanks
Haha, I was heading to that link to see what the results were...and then I found my answer. Thanks for the link, and the memories.
jjnguy
Glad I could brighten up your day!
simeonwillbanks
+3  A: 

It's mostly an issue of readability. Generally, use for loops when you have a defined range of values to iterate over. Use while loops when you aren't iterating, or don't know when your exit condition will be true.

From a java perspective, I find for loops very handy for collections:

for (Object o : objects){
    o.process();
}

is a lot easier to read than:

int i=0;
while(i < objects.size()){
    objects.get(i).process();
    i++;
}
dpatch
On the while-loop, there's also the possibility of using an Iterator. while(iterator.hasNext()) iterator.next().process.
daniel
the first example is technically a for-each construct
Gary
+19  A: 

The one notable difference between a for() and while() loop is that a "continue" statement in a while() loop will branch to the top of the loop, while one in a for() loop will branch to the third part of the for() clause [the one after the condition, usually used to bump variables].

supercat
+13  A: 

Why choose Coke over Pepsi?

Between a WHILE and FOR loop, you can use them interchangeably. To be a purist, you could make your decision base on the nature of the conditions. If you're performing a count-based loop, then a FOR loop would make the most sense.

for( cur = 0; cur < myList.Length; cur++ ){
    doSomething( myList[cur] );
}

If you're performing a logic-based loop, then a WHILE would make for the cleanest implementation

Iterator i = myObject.getIterator();
while( i.hasNext() ){
    doSomething( i.next() );
}
Babak Naffas
I think that this is the answer I wanted to hear (a good reason not to accept it right away). Do people generally agree with you?
Ziggy
I still think when using iterators for-loop still is the cleaner implementation: `for (Iterator i = myObject.getIterator(); i.hasNext(); i.next())`; the iterator version of for-loop looks extraordinarily similar to using for-int-looping. while-loop is cleaner when you want to emphasize that the loop stops when reaching a certain condition, and the stopping condition is determined by neither the loop counter nor an iterator (e.g. a GUI mainloop, termination condition is when the quit button is pressed).
Lie Ryan
All things said and done, it could just come down to personal preference. I don't think any of the answers provided here are wrong.
Babak Naffas
@Lie Ryan: the problem with that is that in languages like Java that have this style of iterator, `i.next()` usually *returns* the element -- there is no way to access an element without advancing the iterator. (Of course, you use `foreach` instead. C++ iterators do not have this problem, and `for` is natural there.)
Porculus
+1  A: 

Readability. And from the old VB 6 days of wend whenever I see a while I want to cry.

There are some cases while loops are good to use. Calling a function on an object that returns true/false but iterates a value inside that object.

Example being a DataReader in C#:

List<string> strings = new List<string>();
while(DataReader.Read())
{
  strings.Add(DataReader["foo"].ToString());
}

A for loop allows you to encapsulate your work within a function, which gives better memory management. Also, if there is an error in your while loop, it could spiral into an out of control loop that threatens all life:

bool keepGoing = true;
while(keepgoing)
{
   try
   {
     //something here that causes exception, fails to set keepGoing = false;
     keepGoing = false;
   }
   catch(Exception)
   {
    //Do something, but forget to set keepGoing.
    //maybe there is always an exception...
   }

}

I also think it's just general practice. Much like in construction. You use nails for putting roofing flats onto the studs. You "could" use screws, they would do the same job, but it's just not common practice.

No one is saying it's wrong, we're just saying "please fold under the pressure of your peers to keep the coding world functioning".

Ryan Ternier
"wend" is older than VB6 - my first exposure to it was in Fortran.
Harper Shelby
+13  A: 

As a teacher I have been pondering this in various shapes and forms; my suggestion always boils down to

  • for-loops are for counting. counting up, counting down.
  • while / do-while constructs are for all other conditions. c!=EOF, diff<0.02, etc. Iterators/Enumerators are counters very suitable for for-loops.

i.e. your int=0; while( ... ) is hideous to my eyes.

Captain Giraffe
A: 

For loops are often easier to parallelize. Since the program can tell in advance the number of iterations that will be performed and the value of the iterator at any point, parallelization using an API such as OpenMP is trivial. Of course if you get into breaks, continues, and changing the iterator manually this advantage disappears.

Rob
+1  A: 

Just decide on one and then copy and paste / adjust it throughout your projects. Less decisions, less typing, faster coding. Negligible performance difference.

Also I am not with Justin keeping i in the scope of the loop, AND I use var aL = array.length outside the loop too. Then use i and aL within the for loop constructor so you are not recalculating the length of the array on each hit (along with a var creation over and over ;)). Again neg. performance really (see first bit of my answer), unless you have very big arrays flying around. If you happen to know the array length upfront initiate it with its fixed length first (slight help with memory allocation on crappy browsers ;) if this is client side and you have a lot of data).

js example

<script>
var i=0;
var myArray = ["D","R","Y"];//literal style will do for now;)
var aL=myArray.length; //or use 3 if known upfront
for(i;i<aL;i++){/*always do DRY things here*/}
</script>

also ensure when a condition is met, ask yourself "do I still need to complete the whole loop?" If not don't forget to break out of the loop as early as you can.

dryprogrammers
You don't need to init i outside the loop's scope unless you plan to use it after the loop is over. The array length hack would only make sense on very large arrays - for a few iterations I'd stick with the cleaner code.
lunixbochs
+1  A: 

There is a semantic difference between for and while loops. With for loops, the number of iterations is pre-defined, whereas with while loops it is not necessary.

Some languages (such as Ada) enforce this, by making the for loop counter a constant within the loop body. Hence it would be illegal to say, for example:

for i in values'range loop
   ...
   i := i + 1;   -- this is illegal, as i is a constant within the loop body
end if;

This semantic difference, however, is not so clear with most languages, which do not treat the for loop counter as a constant.

Schedler
lots of languages don't even require a counter at all in a for, for(;cond;) { ... } is identical to while(cond) { ... } in C and C++ for example.
Logan Capaldo
@Logan: Exactly - which is why most of the people do not care about the semantic difference, as the for loop is simply syntactic sugar for a while loop.
Schedler
+3  A: 

In a language like Pascal, for and while constructs had different applications. You would write a for loop in Pascal as follows:

for i := 1 to 5 do
begin
{ some code here }
end

So you explicitly state that i is incremented by 1. There is no termination condition explicitly specified over here. Thus you could use a for loop only in places where you know the loop will run for certain pre-determined number of times before the actual loop execution starts. On the other hand, you can specify an explicit termination condition in the case of a while loop.

i:=0;
while (i<>5) do
begin
{ some code here }
i:=i+1;
end

Thus a while loop is much more flexible. Later on when C was developed, although for and while were supported, the meaning of the for construct changed and you could add in explicit termination conditions using boolean operators. Thus, the power of while and for in C and C-like languages is same and thus it becomes more of a readability issue than a usability issue.

Meher
+5  A: 

Generally, I always prefer for loops, even for cases where the loop has nothing to do with counting, because it localizes maintenance of the loop in a single location. When someone else comes along and decides to add/modify the loop, if they are using a FOR loop, the person doing the modification can generally be confident that, so long as they don't mess with items inside the FOR expression(s), they aren't going to break iteration in the loop.

However, something like a while loop requires the programmer to parse the loop body completely, and understand how it iterates, to make sure they keep loop maintenance code unmodified and in the correct order when they make changes.

Therefore, I only use WHILE in cases where I only effectively need the middle part of the FOR.

Billy ONeal
+2  A: 

Glancing over the other (very good) answers, there's a point I didn't see getting made. (If it did get made I missed it and apologize.)

I'm going to give you two snippets of seemingly semantically identical code.

Snippet #1:

for (int a = 0; a < arr.length; a++) {
  /* do some work here */
}

Snippet #2:

int b = 0;
while (b < arr.length) {
  // do work
  b++;
}

They look like they do the same thing, right? Now let me add one single line to each snippet and let's see what happens:

Snippet #3:

for (int c = 0; c < arr.length; c++) {
  /* do some work here */
}
printf("%d\n", c);

Snippet #4:

int d = 0;
while (d < arr.length) {
  // do work
  d++;
}
printf("%d\n", d);

So when I compile a junk file with those four snippets embedded (plus some glue to have arr.length mean something) I get the following error:

$ clang junk.c
junk.c:20:17: error: use of undeclared identifier 'c'
        printf("%d\n", c);
                       ^
1 diagnostic generated.

Using the for loop, it turns out, allows you to provide more locality to your dummy counter variables, etc. This gives you greater leeway in reusing variable names without chance of collision (albeit at the expense of increasing potential shadowing).

JUST MY correct OPINION
A: 

because latter can be one liner: while (++i < arr.length) { // do work }

Devrim
A: 

One reason is that a while's condition can be a string.

Also, if we add 'Do' to the mix, we can use a do while loop when we want the operation to run at least once regardless of its current state.

Mild Fuzz
A: 

Related trivia:

In Google's new Go language, they have decided not to include the while statement. Instead they allow you to write a for statement that only has the condition clause. So the equivalent of

while (a < b) { ... }

is simply

for a < b { ... }
Samuel Jack
A: 

Its just my logic that we generally use for loop when we know the exact count of our itterations and we use a while loop when we generally want to check a condition.Then we use them according to ur requirments. Otherwise they both show same behaviour.

Prateek
A: 

Simply put:

A 'for' loop tends to be used for iterational use. You need to iterate through an array, you need the numbers 1 through 100 (or other ranges) for various reasons.

A 'while' loop tends to be a conditional loop. You want to keep doing something until a contition is false. Assuming you have a class called 'Toast' and you wanted to cook the toast until it is toasted, you would have something like the following:

while (!toast.isToasted) cook(toast);

You can most always (I can't think of an example where you can't) write a for loop as a while loop and a while loop as a for loop, but picking the one which best first the situation usually comes down to my above points.

Shynthriir
+1  A: 

Readability is a very important point, but it works both ways. I'll convert a for-loop to a while-loop—even when I'm using a counter variable or it would otherwise be "natural" to use for—when it's more readable that way. This is common when you have a long or complex expression in any of for's three parts or multiple counters (which is related to being complex).

Knuth's loop-and-a-half also works better, in several languages, as an infinite loop with a break, rather than a for-loop.

However, there is a very important semantic difference:

for (int i = 0; i < arr.length; i++) {
  if (filter(arr[i])) continue;
  use(arr[i]);
}

int i = 0;
while (i < arr.length) {
  if (filter(arr[i])) continue;
  use(arr[i]);
  i++;
}
Roger Pate
A: 

A while loop, defined as

while(condition)

represents this programming pattern:

1:
if(!condition) goto 2;
...
goto 1;
2:

Whereas a for loop, defined as

for(var i = seed; condition; operation)

represents this pattern:

var i = seed
1:
if(!condition) goto 2;
...
operation;
goto 1;
2:

Both patterns are so commonly used that they have been named and built into languages.

If you need to define a counter to control your loop, use a for loop. If not, use a while loop.

Chris Johnson