Difference Between For Loop and While Loop

By Mohit Uniyal|Updated : June 13th, 2022

Difference Between For Loop and While Loop: In a computer programming language, iteration statements like for loop and while loop and more are used for repeated execution of the instruction in a program. Both for loop and while loop is used to execute the statements again and again while the program is running. The major difference between for loop and while loop is that for loop is used when the number of iterations is known whereas, in the while loop, execution is done until the statement in the program is proved wrong.

Here, in this article, we will first see what is for loop is and what is while loop thereafter we will discuss the difference between for loop and a while loop based on the various factors. Read the article provided below to have clarity in order to know where and when to use which loop statement.

Table of Content

Difference Between For Loop and While Loop

Although both for loop and while loop are used for repetition of the statements, there exist various differences between the two which are elaborated in the table provided below:

Key Differences Between For Loop and While Loop

For LoopWhile Loop
It is used when the number of iterations is known.It is used when the number of iterations is not known.
In case of no condition, the loop is repeated infinite times.In case of no condition, an error will be shown.
Initialization is not repeated.Initialization is repeated if carried out during the stage of checking.
Statement of Iteration is written after running.Can be written at any place.
Initialization can be in or out of the loopInitialization is always out of the loop.
The nature of the increment is simple.The nature of the increment is complex.
Used when initialization is simple.Used when initialization is complex.

What is For Loop?

In a computer programming language, for loop is used to have a precise and concise loop structure. It is an entry control statement. With the help of for loop, repeated execution of a statement is done. They are used to reduce the size of the codes. 

The for loop is used to check the given specific condition and the program is run until the desired condition is met. With the use of for loop the following actions can be performed at once:

  • Initialization,
  • Evaluation and
  • Incrementation

Syntax of for loop:

for ( init; condition; increment ) {
statement(s);
}

Example:

#include <stdio.h>
int main() {
int num, count, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
for(count = 1; count <= num; ++count) {
sum += count;
}
printf("Sum = %d", sum);
return 0;}

What is While Loop?

While loop is also used for the repeated execution of a statement. In this loop, the number of iterations can be infinite unless the given statement is reached or satisfied. It is one of the simplest and fundamental loops in the computer programming language. We can learn more about the while loop by knowing the difference between while and do-while loops.

Syntax of while loop is: 

while(condition) {
statement(s);
}

Example:

#include <stdio.h>

int main () {

/* definition */
int a = 5;

/* while loop execution */
while( a < 10 ) {
printf("value of a: %d\n", a);
a+;
}

return 0;
}

Check out some important topics related to the difference between for loop and while loop in the table provided below:

Difference Between drop and truncateDifference Between Website and Web application
Difference Between POP and OOPDifference Between Website and Webpage
Difference Between Abstraction and Encapsulation in JavaDifference Between Constructor and Method in Java

Comments

write a comment

FAQs on Difference Between For Loop and While Loop

  • The major difference between for loop and while loop is that in the case of for loop the number of iterations is known whereas in the case of the while loop number of iterations is unknown and the statement will run until the condition is proved false.

  • Both for loop and while loop carry different methods of execution and can be useful if they are used as per their merit. If the iterations are known we use for loop and if the iterations are unknown, while loop is used. The while loop is used when the initialization is complex and if the initialization is complex, for loop is used.

  • While loop is a loop statement used for the repeated execution of the statements. It is preferred when the loop statement is required and a number of iterations are not defined. In case the condition is not known, it shows an error unlike for loop which will have infinite iterations.

  • A for loop in a programming language is a statement used when the repeated execution of the statement is required. It is used to have a concise structure of the loop. The size of the codes is drastically reduced after using this loop. It is different from the while loop in various ways such as it is used when iterations are known, used when initialization is simple, etc.

  • The syntax of the while loop is simple and similar to the for loop to an extent. This syntax can be used whenever a while loop is required in a program for the execution of the repeated statement.

    The syntax of the while loop is shown below:


    while(condition) {

    statement(s);

    }

Follow us for latest updates