+ 4
What is difference between for and while loop?
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.
+ 13
Before posting any question you should search that question in discuss.
https://www.sololearn.com/discuss/442950/?ref=app
https://www.sololearn.com/discuss/1317523/?ref=app
https://www.sololearn.com/discuss/423213/?ref=app
https://www.sololearn.com/discuss/888483/?ref=app
+ 12
please like it if my answered helped you thanks
+ 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>
}
+ 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
+ 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
+ 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)...
+ 3
Thank you.
+ 3
When we know in advance the exact number of iteration for this purpose we use for loop
+ 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?