Online Tutorials & Training Materials | STechies.com
Register Login

Difference between FOR and WHILE Loop with Comparison chart

|| 0

Difference between FOR and WHILE Loop with Comparison chart
Stechies

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 till a specific condition is true. The loop terminates as soon as the condition is false.

It is common for interviewees to come across questions pertaining to the difference in the for loop and while loop. In most cases these differences are at the pragmatic level as they are both guided by the same conditional go to statement. Most programmers end up getting confused with which loop to use as they are both entry-controlled loops; in other words, both for loop and while loop check for the given test conditions before they enter into the body of the loop. This article throws light upon the main differences between the for and while loop in Java and C.

For loop vs While loop

The following comparison chart depicts the difference between for and while loops in c on the basis of certain factors of distinction.

Basis of Difference

For Loop

While Loop

Format

In the for loop, the initialization, checking of condition and the iteration statement are all written atop the loop.

In case of the while loop only initialization and checking of 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 clearly beforehand.

No condition

In case the condition is absent in the 'for' loop, then the loop will iterate infinite 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 again.

In case of the ‘while’ loop, in case the initialization is carried out during the stage of condition checking, then the initialization has to be performed every time the loop iterates.

Iteration statement

In the 'for' loop, the statement for the purpose of iteration is inscribed at top. Given this, the iteration statement is executed only after all the statements are run.

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 in the nature of 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 the way in which 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 of 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 true. As soon as the condition becomes false, the program control will pass to the line of coding that comes immediately after the loop.

Definition of 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 singular execution. This takes place during the first iteration of the for loop.

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

Whenever the loop command is executed, the initialization condition specific to it is executed before the condition is checked. In case the condition is true, 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

Definition of while loop

The while loop is the most fundamental of all loops present in Java and C++ and its working is 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 become 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, an empty 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

Key difference between while and for loop

The difference between for and while loop can be further understood via the points given below:

1. When it come 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.

2. In 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 important difference between these two loops in c.

3. In case the condition command is absent in the for loop the loop will end up iterating infinite 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. This loop is considered to be more compact and holds all loop control statements in one place effectively.