In 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.
Content: for Loop Vs while Loop
Comparison Chart
Basis for Comparison | for | while |
---|---|---|
Declaration | for(initialization; condition; iteration){ //body of 'for' loop } | while ( condition) { statements; //body of loop } |
Format | Initialization, 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. |
Use | The '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. |
Condition | If 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. |
Initialization | In '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 statement | In '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. |
Definition of for loop
In Java, there are two forms of for loops. The first form is “traditional” form and the second is “for-each” form.
Syntax
The general form of traditional for loop statement.
for (initialization; condition; iteration) { //body of for loop }
- Initialization: The initialization of the loop controlling variable of for loop is executed only once, during the first iteration of the loop. Here, the loop controlling variable is initialized, sometimes if the loop variable is not used again anywhere in the program and is only used as the controlling variable of 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 statement is an expression that increment or decrements the loop controlling variable.
Whenever the loop is executed, its initialization condition is executed first; then the condition is checked. If the condition is satisfied, the body of the loop is executed, then the iteration statement is executed. Then again, the condition is checked to know whether the loop will iterate further or will terminate.
In Java, the initialization statement and the iteration statement may include more than one statement. Each statement is separated by other by a comma, in Java, a comma is a separator whereas, in C++, “comma” is an operator that can be used in any valid expression.
for-each loop and its syntax
The “for-each” form is an enhanced for loop. The general form of the for-each loop is as follow.
for(type iter_variable: collection) statement-block
Here, the “type” specifies the type of iteration variable, followed by the iteration variable. 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.
Example
There are various types of collection used with for loop. Let’s discuss it with an array as a collection.
public class Main { public static 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); } } // output value in c 10 value in c 20 value in c 30 value in c 40 value in c 50 value in c 60 additon of array elements is 210
Here, ‘c’ is an iteration variable; it receives the values from array[ ], one at a time, from the lowest index to the highest index in the array. Here, the loop iterates until all the elements of the array are examined. It is possible to terminate the loop in between by using “break”. However, the change in the iteration variable does not affect the array, as it is only a read-only variable.
Definition of while loop
The while loop is the most fundamental loop available in C++ and Java. The working of a while loop is similar in both C++ and Java.
Syntax
The declaration of a while loop is as follows
while ( condition) { statements; //body of loop }
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.
Example
Let’s discuss the working of a while loop. In the example below the code will print from 1 to 10.
//example is in Java. public class Main { public static void main (String args[]) { int n=0; while(n<10) { n++; System.out.println("n=" +n); } } } //output n=1 n=2 n=3 n=4 n=5 n=6 n=7 n=8 n=9 n=10
Here, the initial value of ‘n’ is 0, which makes the condition in while loop true. The control then enters the body of the while loop and the value of ‘n’ is incremented according to the first statement in the body of a while loop.
The value of ‘n’ is printed, then the control goes back to the condition in a while loop, now the value of ‘n’ is 1 which again satisfies the condition, and the body of the loop is executed again. This continues till the condition is true, as soon as the condition 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 example while((ch = getchar( ) ) != 'A') { System.out.println(" The input alphabet " +ch); }
Here the control variable ‘ch’ is initialized, and the condition of the loop is verified at the top of the loop.
Note
May it be a for loop or a while loop, if there is only one statement in the body of the loop, the curly braces are not required in that condition.
Key Differences Between for and while loop
- 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.
- 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.
- 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.
- 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.
- 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.
Conclusion
The for loop and while loop both are iteration statement, but both have their distinct feature. The for loop do have all its declaration (initialization, condition, iteration) at the top of the body of the loop. Adversely, in while loop only initialization and condition is at the top of the body of loop and iteration may be written anywhere in the body of the loop.
Tanzina Rahman Hera says
This site is just love. It is very helpful
Nicollas says
God bless You.
Thank You for sharing
MOUREEN says
I AM MORE GREATFUL THAN YOU ARE
ditya says
Nicely explained
Chhotu Kumar chourasiya says
what is looping ? How many types of loop c user. differentiate between for and while loop.
Sherley says
Hi in simple words i would like to know what is the difference between for and while loop and can you provide .NET syntax for both loops.
Neha Sharma says
I love your Content 🙂
Luqmaan Shaik says
When comparing the ‘for’ loop and the ‘while’ loop in Python, what are the key differences in terms of syntax, usage, and the execution of initialization and iteration statements within the loop?