The main difference comes into picture when you use continue with them i.e. for and while.
In a while loop if continue is used before the increment of a variable is done it converts into a infinite loop.
i=1;
while(i<10)
{
/* do stuff */
if(i==6);
continue;
i++;
}
The above piece of code will turn into an infinite loop.
for(i=1;i<10;i++)
{
/* do stuff */
if(i==6);
continue;
}
In the above for loop the value of will be incremented once it attains the value 6.
Therefore it will not turn into a infinite loop.
Cheers.
astvansh> 'for' statement takes the start condition, stop condition, and the increment. 'while' statement just takes the stop condition.
One scenario (which i can think of) where while 'needs' to be used rather than a for, would be when the counter needs to be incremented/decremented conditionally...
string[] arr = {"a", "b", "b", "c"};
int i = 0;
while( i< 10)
{
// do something constructive
if(arr[i] == "b")
{
i = i + 2;
}
else
{
i++;
}
}
Cheers,
Ajeesh
Another main difference between the two: a 'for' loop is called a determinate loop, meaning that we usually use it when we know ahead of time how many iterations we want. The 'while' loop is an 'indeterminate' loop, because we usually use it in situations where we do not know the number of times the loop may iterate.
Copyright © 2026 eLLeNow.com All Rights Reserved.