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.