+ 1

What is the main difference between while and for loop ?

10th Feb 2018, 2:05 AM
suresh
suresh - avatar
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 }
10th Feb 2018, 2:26 AM
jay
jay - avatar
+ 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.
10th Feb 2018, 2:54 AM
Duncan
Duncan - avatar
10th Feb 2018, 2:21 AM
Tim Millar
Tim Millar - avatar
+ 3
https://code.sololearn.com/c2LFXiVvcE7H/?ref=app
10th Feb 2018, 2:21 AM
Tim Millar
Tim Millar - avatar
+ 1
while is the doing same thing and for loop will doing same thing so why do introduced in for loop....?
10th Feb 2018, 2:07 AM
suresh
suresh - avatar