Difference between While loop and For loop. Where we need to use While/For?
Please explain with example
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Prashantkumar DPosted Oct 9, 2012, 7:28 AM
Prashantkumar DPosted Oct 9, 2012, 7:27 AM
Prashantkumar DPosted Oct 9, 2012, 7:27 AM
VulpesPosted Oct 9, 2012, 6:02 AM
So it's just a matter of which is the more convenient (or even personal taste) for a given scenario.
R ACHUTHAPosted Oct 9, 2012, 5:54 AM
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.
Santhosh Kumar JayaramanPosted Oct 9, 2012, 5:53 AM
only difference is before using variable in while loop condtion, you need to declare it before but with for its not required. You can declare in for loop also.
While( int i=1>0)
{
}
The above is not possible. We have to declare i earlier and should assign some value
int i=2;
while(i<5)
{
}
but for (int i=1;i>0;i++)
{
}
This is allowed