What is the difference between while and for loop in c




















In Java, the initialization statement and the iteration statement may include more than one statement. The iteration variable will receive the element from the collection variable. The type must be the same as the type of elements stored in the collection variable. The for-each form of for loop automates the iteration of the loop from starting to end accessing the values in sequential order.

There are various types of collection used with for loop. Here, the loop iterates until all the elements of the array are examined. However, the change in the iteration variable does not affect the array, as it is only a read-only variable. The while loop initially checks the condition and then executes the statements till the condition in while loop turns out to be true.

The condition in while loop can be any boolean expression. When an expression returns any non-zero value, then the condition is true, and if the expression returns a zero value, the condition becomes false. If the condition becomes true, then loop iterates itself, and if the condition becomes false, then the control passes to the next line of the code immediately followed by the loop. The statements or the body loop can either be an empty statement or a single statement or a block of statements.

This loop requires initialization only once. After initialization, the compiler checks whether the condition is true or not, and if it is true, the loop continues to iterate till the predefined number of iterations are obtained. A while loop is when the command iterates for an uncertain number of times till the condition is true.

Once the condition is proved false, the iteration of command stops. Initialization in while loop is done each time the loop iterates.

The compiler checks the condition and if it is proved false, the loop jumps to the next statement. A while loop works on the following syntax —. If the starting condition in the while loop is missing, the loop iterates infinitely. In while loop, no new value is created for the variable, only the condition is satisfied. In while loop, the condition is checked before iteration therefore it is also known as the pre-test loop.

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.

But, there could be many complex problems where number of iterations depend upon a certain condition and can't be predicated beforehand, in those situation programmers usually prefer to use while loop. The following table lists differences between for and while loop. The initialization is an assignment statement that is used to set the loop control variable. The condition is a relational expression that determines when the loop exits.

The increment defines how the loop control variable changes each time the loop is repeated. The body of loop can either be empty or a single statement or a block of statements.



0コメント

  • 1000 / 1000