What are the differences between a while loop and a for loop? It seems to me that they are the same.
2 Answers
The while
loop is usually used when you need to repeat something until a given condition is true:
inputInvalid = true;
while(inputInvalid)
{
//ask user for input
invalidInput = checkValidInput();
}
On the other hand, the for
loop is usually used when you need to iterate a given number of times:
for(var i = 0; i < 100; i++)
{
...//do something for a 100 times.
}
You can use them interchangeably if you like:
inputInvalid = true;
for(;;)
{
if(!inputInvalid)
{
break;
}
//ask user for input
invalidInput = checkValidInput();
}
Or
inputInvalid = true;
for(;inputInvalid;)
{
//ask user for input
invalidInput = checkValidInput();
}
And:
var i = 0;
while(i < 100)
{
//do your logic here
i++;
}
-
3As you can see though, these non-standard usages produce less elegant, harder to read code. Under the hood, the different loop types are all the same conditional goto, so try to use the one that syntactically best fits what you are trying to achieve. Commented Jun 9, 2014 at 10:49
-
In the below code, I can also say do something 10 times. (It fills your argument as for loop @npinti) var count = 0; while (count < 10) { document.writeln("looping away!"); count++; } Commented Jun 9, 2014 at 14:32
-
Note that in C, you can rewrite your third example as
for(invalidInput = checkValidInput();inputInvalid;invalidInput = checkValidInput()){}
. But yes, your basic point is entirely correct, and afor
loop is really just syntax sugar. Some languages do without it entirely. The Cfor
loop is a strange beast, really more of a bastardizedwhile
unlike languages like Pascal, where thefor
loop can only be used to step through discrete integers.– user53141Commented Jun 9, 2014 at 16:47 -
what does it for(;;) mean. I cannot find explaination of for(;;) it . Commented Jun 10, 2014 at 14:32
-
@mistichor the C/C++ for loop takes 3 statements for(;;) is a for loop with 3 empty statements, the semicolon separates the blank statements.– esoterikCommented Aug 10, 2015 at 17:33
There is a fundamental difference between the two: with a for
loop, you need to know beforehand how often the loop body will be executed. This is a major restriction, since there are many problems where you simply don't know that. Sometimes you don't even know whether or not that number is finite at all!
Consider, for example, a program asking the user to input a series of names. Say, a patient management system for a dentist. How would you know beforehand how many patients the dentist is going to enter? You don't! You can't write a for
loop for that.
-
1There are several ways to terminate a for loop early, so you only need to know the maximum times you intend to iterate around the loop. Further, if you're willing to mis-use the conditional part of the for loop, you can make it do anything that a while loop could.– Simon BCommented Jun 10, 2014 at 10:42
-
Which language are you talking about? In pascal your argument has some merit (hardcoding a large number as limit is ugly), but in C you can use arbitrary conditions with
for
. No difference betweenfor(;xxx;)
andwhile(xxx)
. Commented Jun 10, 2014 at 15:00 -
2down arrow. Chaos is precisely correct.
initializer;
followed bywhile(condition){ ... increment;}
is the same asfor(initializer; condition; increment){ ... }
. Commented Aug 10, 2015 at 23:16
for
loop is just syntax sugar, supporting a subset of use cases thatwhile
supports. Sometimes "syntax sugar" is looked down upon, but it should really be looked at as a way to create cleaner, easier to understand code.for
loop is actually awhile
loop in fancy clothing.