+ 1
What is the main difference between while and for loop ?
5 Answers
+ 7
for loop includes it's own counter, condition and increment.
This makes it very useful when iterating through things.
consider the following while statement
int counter = 0;
while(condition == true) {
if(counter >= 3) }
condition = false;
}
else {
// do stuff
}
++counter;
}
compared to:
for(int counter = 0; counter >= 3; ++counter) {
// do stuff
}
+ 5
Typically you would use a 'for loop' when you know how many times you want to repeat something and you're not usually checking a condition.
A while loop on the other hand is used when you don't know how many timed you need to repeat some and checks a condition to make the loop stop.
+ 3
take a look to understand
https://code.sololearn.com/c938vAyuCByR/?ref=app
+ 3
https://code.sololearn.com/c2LFXiVvcE7H/?ref=app
+ 1
while is the doing same thing and for loop will doing same thing
so why do introduced in for loop....?