• Register
499 points
8 4 3
 

Difference Between for and while loop

n C++ and Java, the iteration statements, for loop, while loop and do-while loop, allow the set of instructions to be repeatedly executed, till the condition is true and terminates as soon as the condition becomes false. Conditions in iteration statements may be predefined as in for loop or open-ended as in while loop.

 

There are several ‘for’ loop variations in C++ are implied to increase its applicability, power and flexibility. For example, the for loop allows us to use more than one variable inside the loop in order to control it, and the use of converge function with ‘for’ loop. Conversely, with while loop we can not use many variations, that must be used with the standard syntax.

There are some major differences between for and while loops, which are explained further with the help of a comparison chart

Key Differences Between for and while loop

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.

Comparison Chart

 

BASIS FOR COMPARISONFORWHILE
Declarationfor(initialization; condition; iteration){
//body of 'for' loop
}
while ( condition) {
statements; //body of loop
}
FormatInitialization, condition checking, iteration statement are written at the top of the loop.Only initialization and condition checking is done at the top of the loop.
UseThe 'for' loop used only when we already knew the number of iterations.The 'while' loop used only when the number of iteration are not exactly known.
ConditionIf the condition is not put up in 'for' loop, then loop iterates infinite times.If the condition is not put up in 'while' loop, it provides compilation error.
InitializationIn 'for' loop the initialization once done is never repeated.In while loop if initialization is done during condition checking, then initialization is done each time the loop iterate.
Iteration statementIn '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.

1 Comment

Informative and useful post, thanks for sharing.

More Posts

Difference between while and do while in java Sanjana Sagar - May 29
What is the difference between div and span amna - Jun 26
Difference between virtual function and pure virtual function in C++ Sanjana Sagar - Jun 22
Difference between a library and a framework Sanjana Sagar - May 29
Difference between encapsulation and data hiding Sanjana Sagar - May 28
What is the difference between padding and margin Sanjana Sagar - May 28
What is difference between arraylist and linkedlist Sanjana Sagar - May 22
What is the difference between pre and post Sanjana Sagar - May 20
Difference between pop and remove in python Sanjana Sagar - May 20
What is the difference between sql and tsql Sanjana Sagar - May 20