In the example below the code will print from 1 to This continues till the condition is true, as soon as the condition becomes false, the loop is terminated. May it be a for loop or a while loop, if there is only one statement in the body of the loop, the curly braces are not required in that condition.
The for loop and while loop both are iteration statement, but both have their distinct feature. The for loop do have all its declaration initialization, condition, iteration at the top of the body of the loop.
Adversely, in while loop only initialization and condition is at the top of the body of loop and iteration may be written anywhere in the body of the loop. Your email address will not be published. Key Differences Between for and while loop In for loop, initialization, condition checking, and increment or decrement of iteration variable is done explicitly in the syntax of a loop only. As against, in the while loop we can only initialize and check condition in the syntax of the loop.
When we are aware of the number of iterations that has to occur in an execution of a loop, then we use for loop. On the other hand, if we are not aware of the number of iteration that has to occur in a loop, then we use while loop. If you fail to put condition statement in for loop, it will lead to an infinite iteration of a loop.
In contrast, if you fail to put condition statement in the while loop it will lead to a compilation error. The initialization statement in the syntax of for loop executes only once at the start of the loop. Conversely, if while loop is carrying initialization statement in its syntax, then the initialization statement in the while loop will execute each time the loop iterates. The iteration statement in the for loop will execute after the body for loop executes.
On the contrary, the iteration statement can be written anywhere in the body of while loop so, there can be some statements that execute after the execution of iteration statement in the body of while loop. Peter Peter Why inverting the order of the printing statement and the operation?
There was no comment about which was the desired behaviour, so I focused on explaining the differences and on one option for getting the two samples producing the same output. I see, good answer though : — Fareanor. Hope this helps, Thanks. Rajkumar Ananthu Rajkumar Ananthu 2 2 silver badges 6 6 bronze badges. The Overflow Blog. Does ES6 make JavaScript frameworks obsolete? Podcast Do polyglots have an edge when it comes to mastering programming Featured on Meta.
Now live: A fully responsive profile. Related Hot Network Questions. The same question is repeated until the new statement is applied. The result in the looping process executes continually until the program reaches a breakpoint. If the breaking point is not reached, it will lead to the crashing of the program. Both the for loop and the while loop are conditional statements.
For loop is a single lined command to be executed repeatedly and While loop may be a single-lined command or may contain various commands for a single condition.
For loop and while loop both play an important role in computer languages to get results. If the command syntax is correct the condition is reached. What is For loop? What is While loop? Main Differences Between For loop and While loop BIn for loop, the number of iterations to be conducted is already known whereas in while loop the number of iterations are not known.
For loop contains only a single condition whereas while loop may contain a set of commands to be executed together. In for loop, initialization of command is done only once but in while loop initialization of command is needed each time the iteration of command is done. If the condition is absent in for loop, the loop iterates for an infinite number of times whereas the while loop shows an error in case of the absence of the condition.
For loop can be used only in case of a known number of iterations whereas while loop is used only when the number of iterations is not known.
0コメント