In C++ and also Java, the iteration statements, for loop, if loop and do-while loop, permit the collection of instructions come be consistently executed, it rotates the problem is true and also terminates as shortly as the problem becomes false. Conditions in iteration statements might be predefined as in because that loop or open-ended together in if loop.

You are watching: Difference between while and for loop

There are number of ‘for’ loop variations in C++ are implied to rise its applicability, power and also flexibility. Because that example, the for loop enables us come use more than one variable inside the loop in bespeak to control it, and the usage of converge role with ‘for’ loop. Conversely, through while loop we deserve to not use many variations, that need to be used with the traditional syntax.

There space some major differences in between for and also while loops, i m sorry are described further through the assist of a to compare chart.

Content: for Loop Vs if Loop

Comparison Chart

Basis for Comparisonfor while
Declarationfor(initialization; condition; iteration)//body the "for" loopwhile ( condition) statements; //body that loop
FormatInitialization, condition checking, iteration statement are written in ~ the height of the loop.Only initialization and also condition checking is excellent at the height of the loop.
UseThe "for" loop supplied only once we already knew the variety of iterations.The "while" loop offered only once the number of iteration are not specifically known.
ConditionIf the condition is not put up in "for" loop, then loop iterates limitless times.If the problem is not placed up in "while" loop, it provides compilation error.
InitializationIn "for" loop the initialization once done is never repeated.In while loop if initialization is excellent during problem checking, then initialization is done each time the loop iterate.
Iteration statementIn "for" loop iteration explain is composed at top, hence, executes just after every statements in loop space executed.In "while" loop, the iteration statement deserve to be written everywhere in the loop.

Definition of because that loop

In Java, there space two develops of for loops. The very first form is “traditional” type and the 2nd is “for-each” form.


The general form of classic for loop statement.

for (initialization; condition; iteration)//body of because that loopInitialization: The initialization that the loop regulating variable of because that loop is executed just once, during the very first iteration that the loop. Here, the loop managing variable is initialized, sometimes if the loop change is not used again anywhere in the program and is only provided as the managing variable that the loop, then it is both declared and initialized in the ‘for’ loop.Condition: The condition of the ‘for’ loop is executed each time the loop is iterated.Increment and iteration: The iteration declare is an expression that increment or decrements the loop managing variable.Whenever the loop is executed, the initialization condition is enforcement first; climate the condition is checked. If the problem is satisfied, the human body of the loop is executed, then the iteration statement is executed. Then again, the condition is confirm to recognize whether the loop will iterate further or will certainly terminate.

In Java, the initialization statement and the iteration statement might include much more than one statement. Each statement is be separated by other by a comma, in Java, a comma is a separator whereas, in C++, “comma” is one operator that can be supplied in any kind of valid expression.

for-each loop and its syntax

The “for-each” kind is an amplified for loop. The general type of the for-each loop is as follow.

for(type iter_variable: collection) statement-blockHere, the “type” states the form of iteration variable, adhered to by the iteration variable. The iteration change will get the facet from the repertoire variable. The kind must it is in the exact same as the kind of aspects stored in the collection variable. The for-each kind of because that loop automates the iteration that the loop from starting to end accessing the worths in sequential order.


There are various varieties of collection used with because that loop. Let’s comment on it with selection as a collection.

public course Main public revolution void main(String<> args) int array<>=10, 20, 30, 40, 50, 60; int add=0; for( int c: array) System.out.println( "value in c " + c); include = add+c; System.out.println("additon of range elements is " +add); // outputvalue in c 10value in c 20value in c 30value in c 40value in c 50value in c 60additon of range elements is 210Here, ‘c’ is an iteration variable; that receives the values from array< >, one at a time, from the lowest index to the highest possible index in the array. Here, the loop iterates till all the aspects of the array are examined. That is possible to end the loop in in between by utilizing “break”. However, the change in the iteration variable does not influence the array, together it is just a read-only variable.

Definition of while loop

The while loop is the most basic loop accessible in C++ and Java. The functioning of a if loop is similar in both C++ and Java.


The declaration of a when loop is together follows

while ( condition) statements; //body of loopThe while loop at first checks the condition and then executes the explanation till the condition in when loop transforms out to it is in true. The problem in when loop deserve to be any type of boolean expression. As soon as an expression returns any non-zero value, then the condition is true, and if the expression return a zero value, the problem becomes false.

If the condition becomes true, then loop iterates itself, and if the condition becomes false, climate the control passes to the next line that the code automatically followed by the loop. The statements or the body loop can either be an north statement or a single statement or a block the statements.


Let’s talk about the functioning of a when loop. In the example listed below the code will publish from 1 to 10.

//example is in Java.public course Main{ public static void main (String args<>) { int n=0; while(nHere, the initial worth of ‘n’ is 0, which renders the condition in while loop true. The regulate then beginning the human body of the while loop and also the worth of ‘n’ is incremented follow to the an initial statement in the human body of a if loop.

The worth of ‘n’ is printed, climate the manage goes ago to the condition in a while loop, currently the value of ‘n’ is 1 which again satisfies the condition, and also the body of the loop is executed again. This continues till the problem is true, as quickly as the problem becomes false, the loop is terminated.

Like ‘for’ loop, the ‘while’ loop can also initialize the manage variable at the optimal of the loop i.e. During problem checking.

//for examplewhile((ch = getchar( ) ) != "A")System.out.println(" The entry alphabet " +ch);Here the regulate variable ‘ch’ is initialized, and the problem of the loop is verified at the top of the loop.

See more: How Long Is A Nyc Block - How Many City Blocks Are In A Mile


May it it is in a for loop or a if loop, if there is just one explain in the body of the loop, the curly braces space not required in the condition.