-3

It seems like, at least in all the languages I'm used to, a while loop can do all the things that a for loop can, and more. Since I'm most acquainted with Java, I'll use that for an example:

int foo = 6;
while (foo > 0)
{
    this.bar();
    foo--;
}

seems functionally identical to

for (int foo = 6; foo > 0; foo--)
    this.bar();

From this, it looks to me like the for loop is wholly redundant in function to the while one. What am I missing here? Is one more faster or more streamlined than the other once compiled? Does one automatically ditch the foo timer once it's no longer needed? Are they exactly the same in some compilers?

I'd be really surprised if they were completely identical, because, you know, DRY.

I've seen similar questions asked before, but none of them sought a really detailed answer.

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
user3338441
  • 89
  • 1
  • 3
  • I answered this here: http://stackoverflow.com/questions/21869399/disadvantage-of-for-loop/21869513#21869513 Also a `while` can not do *more* things than a for, they are equivalent because `for(;;)` is exactly a `while()` – DSquare Feb 21 '14 at 19:11
  • Anything, a for-loop can do, can also be done by a while-loop, and vice-versa. There is no "better" loop. Usually, you use for loops if the number of iterations is fixed at the beginning of the loop, and while loops when the number of iterations is not known a-priori. But you do not have to so it this way. – exception1 Feb 21 '14 at 19:13

3 Answers3

2

Internally, both loops compile to the same machine code. The existence of two ways to repeat execution of code stems from two different use cases for each of them: for loops are usually used to iterate through a finite collection of identical objects, processing them in the same way. while loops, on the other hand, are slightly more versatile, and are usually used to repeat a piece of code until a condition is fulfilled. For example, in this piece of pseudocode:

while(document.nextLine())
    document.doStuff();

the while loop iterates through the lines of a document, and processing each line. It is not known in advance how many lines are there. This could not be done easily in a for loop, where you need to know in advance when you are going to stop.

Ermir
  • 1,391
  • 11
  • 25
0

A for loop is a while loop. The only difference is that the for loop includes an initialize and state-change options, whereas a while loop requires you to do those separately.

The distinction is really more for ease of use and readability than it is purely functional. Say you want to iterate through a list:

for(int i = 0; i < list.size(); i++){
    //code
}

is a lot nicer looking than:

int i = 0;
while(i<list.size()){
    //code
    i++;
}

Although they are (almost) functionally identical. Why did I say almost? Well the scope of the variable i is different in those two cases. If, say, you wanted to do another iteration through that list after doing the first one, you could re-use the i variable if you used the for loop. But if you used the while loop, the i would still be in scope (and you might not want that as it's purpose was only to assist in that iteration).

Jake Haller-Roby
  • 6,335
  • 1
  • 18
  • 31
0

The two are functionally equivalent. However, there are dowhile loops which will execute at least once (something that for loops cannot do) and there are enhanced for loops: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

Terry Chern
  • 694
  • 1
  • 8
  • 15