What is the difference between for and while statement
The FOR loop is often used when you usually know how many times you would like the program, which means it will run that program until the number of times is complete before it terminates itself. The FOR loop works by executing the initialization statement only once at the beginning of the loop. The test expression is then checked against the program. If the text expression is false, the loop is terminated; but if the expression is true then the code inside the body is executed and then update expression is updated.
The loop continues until the text expression becomes false. The WHILE loop checks if the test expression is true, based on which the loop will continue to terminate. The for loop is quite similar to the while loop in terms of memory consumption and speed.
However, the for loop is preferable when you know exactly the number of times the loop has to be repeated. On the contrary, while loop is appropriate when the exact number of iterations is not known, meaning you do not know how many times the loop has to be repeated.
Here, statement can be a single or a block of statements. The loop will continue to execute until the condition is true and will terminate once the condition is false. It is an entry control statement used for the repeated execution of certain statements.
This is preferable when we know exactly how many times the loop will be repeated in advance. The while loop, on the other hand, is used for indefinite loops, meaning we do not have any idea on exactly how many times the loop is going to be repeated. The while loop will continue to run infinite number of times until the condition is met. In C programming, for loops can have their counter variables declared in the declaration itself.
On the contrary, there is no built-in loop control variable with a while loop. Instead, you can specify any condition that evaluates to either a True or a False value.
If the condition is not specified for a for loop, then the loop iterates infinite number of times, while in case of while loop, it shows a compilation error. While both the entry control loops are quite similar and they serve basically the same purpose, the anatomy of a for loop is slightly different than a while loop. A while loop has no built-in loop control variable as there is with the for loop; instead, an expression needs to be specified similar to a test expression specified in a for loop.
However, with a while loop, the expression is specified to evaluate the condition to a True or False value. Unlike for loop, while loop is used when we do not have any idea about how many times the loop will be executed.
Difference Between For and While Loop. Difference Between Similar Terms and Objects. MLA 8 Khillar, Sagar. Name required. Email required. Please note: comment moderation is enabled and may delay your comment. There is no need to resubmit your comment. Notify me of followup comments via e-mail. While loop The following comparison chart depicts the difference between for and while loops: Basis of Difference For Loop While Loop Format In the for loop, the initialization , checking of the condition, and the iteration statement are all written atop the loop.
In the case of the while loop, only initialization and checking of the condition is carried out atop the loop. Usage The 'for' loop is more appropriate for use in case the number of iterations is known in advance. The 'while' loop is preferably used in case the number of iterations is not specified beforehand. No condition In case the condition is absent in the 'for' loop , then the loop will iterate countless times. In case of the condition being absent in the 'while' loop , a compilation error will be provided by the program.
Initialization In the 'for' loop, once the initialization is carried out, it is not repeated. The initialization has to be performed every time the loop iterates. Iteration statement In the 'for' loop , the statement for iteration is inscribed at the top. Given this, the iteration statement is executed only after all the statements are running. In the 'while' loop , the statement for iteration is capable of being written at any place within the loop.
Parts of the declaration The initialization is like an assignment statement. It is used for setting the loop control variable. The condition is in the form of a relational expression.
It will determine when the loop exits. The increment refers to how the loop control variable is made to change every time the loop is iterated. It is true when any non-zero value is returned.
The loop will iterate while the defined condition is actual. As soon as the state becomes false, the program control will pass to the line of coding that comes immediately after the loop. What is 'for' loop? A key difference between while and for loop When it comes to the definition of the conditions present in the iteration statements, they are usually predefined in case of for loop in C.
0コメント