In C++ and also Java, the iteration statements, for loop, while loop and also do-while loop, enable the set of instructions come be consistently executed, till the condition is true and also terminates as soon as the condition becomes false. Conditions in iteration statements might be predefined as in because that loop or open-ended as in if loop.

You are watching: Difference between for and while loops

There are number of ‘for’ loop sport in C++ space implied to rise its applicability, power and flexibility. For example, the for loop allows us come use much more than one variable within the loop in bespeak to regulate it, and the use of converge function with ‘for’ loop. Conversely, v while loop we deserve to not use numerous variations, that need to be provided with the typical syntax.

There are some major differences between for and while loops, which are described further v the aid of a comparison chart.

Content: for Loop Vs when Loop

Comparison Chart

Basis for Comparisonfor while
Declarationfor(initialization; condition; iteration)//body of "for" loopwhile ( condition) statements; //body of loop
FormatInitialization, problem checking, iteration statement room written at the peak of the loop.Only initialization and condition checking is excellent at the height of the loop.
UseThe "for" loop provided only when we already knew the variety of iterations.The "while" loop provided only as soon as the variety of iteration room not exactly known.
ConditionIf the condition is not put up in "for" loop, climate loop iterates limitless times.If the condition is not placed up in "while" loop, it gives compilation error.
InitializationIn "for" loop the initialization once done is never ever 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 explain is written at top, hence, executes only after all statements in loop are executed.In "while" loop, the iteration statement have the right to be written almost everywhere in the loop.

Definition of because that loop

In Java, there space two creates of for loops. The an initial form is “traditional” type and the 2nd is “for-each” form.


The general type of timeless for loop statement.

for (initialization; condition; iteration)//body of because that loopInitialization: The initialization the the loop controlling variable of because that loop is executed just once, throughout the first iteration of the loop. Here, the loop managing variable is initialized, sometimes if the loop change is not offered again all over in the program and is only offered as the managing variable of the loop, then it is both declared and also 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 statement is one expression the increment or decrements the loop regulating variable.Whenever the loop is executed, that is initialization condition is enforcement first; climate the problem is checked. If the problem is satisfied, the body of the loop is executed, climate the iteration declare is executed. Then again, the condition is checked to recognize whether the loop will iterate further or will certainly terminate.

In Java, the initialization statement and also the iteration statement may include an ext than one statement. Every statement is be separate by various other by a comma, in Java, a comma is a separator whereas, in C++, “comma” is an operator that have the right to be supplied in any kind of valid expression.

for-each loop and its syntax

The “for-each” kind is an enhanced for loop. The general kind of the for-each loop is together follow.

for(type iter_variable: collection) statement-blockHere, the “type” states the form of iteration variable, followed by the iteration variable. The iteration change will receive the element from the arsenal variable. The form must be the very same as the kind of aspects stored in the repertoire variable. The for-each form of for loop automates the iteration the the loop from beginning to finish accessing the worths in sequential order.


There are various species of collection supplied with because that loop. Let’s comment on it with an array as a collection.

public class 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); add = add+c; System.out.println("additon of array elements is " +add); // outputvalue in c 10value in c 20value in c 30value in c 40value in c 50value in c 60additon of selection elements is 210Here, ‘c’ is one iteration variable; the receives the values from array< >, one in ~ a time, indigenous the lowest table of contents to the highest index in the array. Here, the loop iterates until all the elements of the variety are examined. The is feasible to terminate the loop in in between by making use of “break”. However, the adjust in the iteration variable does not impact the array, as it is just a read-only variable.

Definition of when loop

The while loop is the most an essential loop easily accessible in C++ and Java. The working of a if loop is similar in both C++ and Java.


The explanation of a if loop is as follows

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

If the condition becomes true, climate loop iterates itself, and if the problem becomes false, climate the manage passes come the next line of the code automatically followed through the loop. The explanation or the body loop deserve to either it is in an north statement or a solitary statement or a block of statements.


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

//example is in Java.public course Main{ public static void key (String args<>) { int n=0; while(nHere, the initial worth of ‘n’ is 0, which makes the problem in while loop true. The manage then start the human body of the if loop and also the worth of ‘n’ is incremented follow to the first statement in the body of a when loop.

The value of ‘n’ is printed, then the regulate goes ago to the condition in a if loop, currently the value of ‘n’ is 1 i beg your pardon again satisfies the condition, and also the human body of the loop is enforcement again. This proceeds till the problem is true, as shortly as the problem becomes false, the loop is terminated.

Like ‘for’ loop, the ‘while’ loop can also initialize the control variable at the top of the loop i.e. During condition checking.

//for examplewhile((ch = getchar( ) ) != "A")System.out.println(" The intake alphabet " +ch);Here the regulate variable ‘ch’ is initialized, and also the condition of the loop is showed at the optimal of the loop.

See more: Who Is Lucilius In Julius Caesar ), Who Is Lucilius


May it be a for loop or a when loop, if there is only one explain in the body of the loop, the curly braces space not forced in the condition.