still don't know the difference btn the for loop and the while loop. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

still don't know the difference btn the for loop and the while loop.

3rd Jul 2016, 6:40 PM
Navigator Moses
Navigator Moses - avatar
6 Answers
+ 2
Almost the same. With 'for-loop' you know beforehand how many times it will iterate. For 'while-loop' it will loop until a condition is no longer met.
3rd Jul 2016, 9:10 PM
ZinC
ZinC - avatar
+ 1
thank you pals but I still need more clarifications.
6th Jul 2016, 6:13 AM
Navigator Moses
Navigator Moses - avatar
0
it's your choice which to choose, if you know before hand how many times the loop must run, then use for loop.
4th Jul 2016, 12:20 AM
Shahin
Shahin - avatar
0
In 'for loop' each iteration is done after the body is completed. In 'while loop' you can do iteration anywhere in the body of loop.
4th Jul 2016, 8:06 PM
Muhammad Nauman Bootter
0
While loop is basically a for loop with only the condition statement. The initialization statement is given before the while loop and the increment or decrement statement is given inside the while loop.
1st Aug 2016, 6:56 AM
Sumitabha Banerjee
Sumitabha Banerjee - avatar
0
Lets say your program flips a coin until you get heads. You don't know how many flips it will take, so you can't use a for loop, instead use the while loop and say while (tails) {flip again} ;
4th Sep 2016, 9:31 PM
Felix