Register Login

What is the difference between FOR Loop and WHILE Loop

Updated Nov 17, 2020

Interviewees often come across questions about the difference between for loop and while loop. In most cases, these differences are at the practical level as they are both guided by the same conditional go-to statement. This means that they check for a test condition before entering the loop’s code block. 

In this article, we will learn about the differences between for loop and while loop.

Let us start with a brief introduction to loops in programming.  

What are the loops?

In Java and C++ programming languages, there are different statements for iteration. These are the while loop, for loop, and the do-while loop. These loops allow any given set of instructions to be executed repeatedly until a specific condition is true. The loop terminates as soon as the state is false.

For loop vs. While loop

The following comparison chart depicts the difference between for and while loops:

Basis of Difference For Loop While Loop
Format In the for loop, the initialization, checking of the condition, and the iteration statement are all written atop the loop. In the case of the while loop, only initialization and checking of the condition is carried out atop the loop.
Usage The 'for' loop is more appropriate for use in case the number of iterations is known in advance. The 'while' loop is preferably used in case the number of iterations is not specified beforehand.
No condition In case the condition is absent in the 'for' loop, then the loop will iterate countless times. 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. In the case of the ‘while’ loop, in case the initialization is carried out during the stage of condition checking. 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
  1. The initialization is like an assignment statement. It is used for setting the loop control variable.
  2. The condition is in the form of a relational expression. It will determine when the loop exits.
  3. The increment refers to how the loop control variable is made to change every time the loop is iterated.
  4. The body of the for loop may either be empty or comprise a single/ block of statements.
The condition in case of the ‘while’ loop may be in the form of any expression. 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?

In Java and other related object-oriented programming languages, there are two different kinds of for loop – traditional and ‘for each.’ The syntax of the commonly found for loop statement is as follows:

for(initialization; condition; iteration){
//body of for loop
}

The loop controlling variable of the for loop is initialized only once through a single execution. This takes place during the first iteration of the for a loop.

The condition about the for loop is executed on each instance of the loop being iterated.

Whenever the loop command is executed, the initialization condition is completed before the condition is checked. In case the state is real, the commands given in the body of the for loop are executed. 

After that, the iteration statement comes into force and is executed. Once this stage is over, the iteration condition is checked all over again with the intent to decipher if the for loop has to iterate further or terminate.

The iteration and initialization statements in Java may comprise of multiple statements separated by commas.

Example of for loop:

#include <stdio.h>
 
int main () {
   int x;
   for( x = 1; x < 10; x = x + 1 ){
      printf("Value: %dn", x);
   }
   return 0;
}

Out Put:

Value: 1
Value: 2
Value: 3
Value: 4
Value: 5
Value: 6
Value: 7
Value: 8
Value: 9

What is 'while' loop?

The while loop is the most fundamental of all loops present in Java and C++, and its working is the same in both the languages. The while loop is declared as follows:

while (condition) {
statements; //body of loop
}

The while loop assesses the condition initially; post that, it executes the statements until the conditions specified in the while loop returns a ‘false.’

The conditions related to the while loop may be in the form of any boolean expression. The condition is true if a non-zero value is returned and becomes false in case zero is returned.

The loop repeats itself in case the condition becomes true. In case the condition becomes false, then the next line of the code, which is immediately after the iteration command, gets executed.

In the while loop, the body loop or statements may either be in the form of a block of statements, a clear statement, or just a single statement.

Example of While Loop:

#include <stdio.h>
 
int main () {
   int x = 1;

   /* Execute While Loop */
   while( x < 10 ) {
      printf("Value: %dn", x);
      x++;
   }
 
   return 0;
}

Value: 1
Value: 2
Value: 3
Value: 4
Value: 5
Value: 6
Value: 7
Value: 8
Value: 9

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. On the other hand. The conditions are open-ended in the while loop in C.
  • In the case of the for loop, the condition checking, initialization, and all increments or decrements of iteration variables are done explicitly. These acts are carried out within the syntax of the loop itself. On the other hand, the conditions can be checked and initialized only within the syntax of the loop in case of the while loop. This is an essential difference between these two loops in C.
  • In case the condition command is absent in the for loop, the loop will end up iterating countless times. On the other hand, any failure to add the condition command in the while loop would result in compilation errors.

Conclusion

The different points of difference between the for loop and while loop make it easy for programmers to consider their correct usage in Java and C++. The for loop is best used for loops wherein initialization and increment form single statements and tend to be logically related. Use this when you know the number of times the loop will run.

On the contrary, use while loop when you don’t know how many times the loop will execute. It is also a good choice when obtaining user input and reading the contents of a file into a variable.  


×