What is difference between for and while loop? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

What is difference between for and while loop?

19th Nov 2019, 6:39 AM
Nitya
Nitya - avatar
10 Answers
+ 14
THIS WILL HElP YoU In while loop if initialization is done during condition checking, then initialization is done each time the loop iterate. In 'for' loop iteration statement is written at top, hence, executes only after all statements in loop are executed. In 'while' loop, the iteration statement can be written anywhere in the loop.
20th Nov 2019, 2:48 PM
abdiwahab jama said
abdiwahab jama said - avatar
+ 12
please like it if my answered helped you thanks
20th Nov 2019, 2:49 PM
abdiwahab jama said
abdiwahab jama said - avatar
+ 11
I use the while loop when I don't know the number of iterations and the for loop when I know the number of iterations. While loop general syntax: while(<condition>) { <block of code> } For loop general syntax: for(<initialization><condition><increment/decrememt>) { <block of code> }
19th Nov 2019, 9:09 AM
MASANGA KAUKA MUDENDA
MASANGA KAUKA MUDENDA - avatar
+ 9
Before anyone answers, can you tell me what difference you notice amongst the loop types? by telling what you know of, at least people know what to suggest or to correct, in case you misunderstood something. (Edit) Please avoid writing a question or sentence in question tags. Tags are used by search feature for finding similar questions by matching up the words in tags. For future reference, follow this guide to posting a question 👍 https://www.sololearn.com/Discuss/333866/?ref=app
19th Nov 2019, 7:37 AM
Ipang
+ 8
Basically the only difference between them is in thier syntax(the way they are written). Appart from that I don't find any huge difference between them But generally I use for loop when I know that how many times a loop will execute otherwise I go for while loop
19th Nov 2019, 8:36 AM
Arsenic
Arsenic - avatar
+ 4
While loop runs until the condition is satisfy and we use while loop when number of iteration are not exactly known to us ,the while loop has initialization. For loop is used when we know the number of iteration,the for loop has all its declaration (initialization,condition,iteration)...
20th Nov 2019, 6:27 PM
Nikhil Suresh Pandav.
Nikhil Suresh Pandav. - avatar
+ 3
Thank you.
19th Nov 2019, 9:25 AM
Nitya
Nitya - avatar
+ 3
When we know in advance the exact number of iteration for this purpose we use for loop
20th Nov 2019, 5:13 PM
Waheed Khan
Waheed Khan - avatar
+ 3
Those saying there is no difference between a for and a while loop need to go back and learn the basics again. Those upvoting for such inaccurate statements are equally misinformed, and are leading others astray. There are more accurate answers in this thread. If there was no difference, why would most languages have both?
20th Nov 2019, 5:28 PM
Logomonic Learning
Logomonic Learning - avatar