hamburger

Difference Between For Loop and While Loop

By BYJU'S Exam Prep

Updated on: September 25th, 2023

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 repeatedly while the program runs. The major difference between for loop and the while loop is that for loop is used when the number of iterations is known, whereas execution is done in the while loop 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 after that, we will discuss the difference between for loop and a while loop based on the various factors. Read the article provided below to have the clarity to know where and when to use which loop statement.

Differences Between For Loop and While Loop PDF

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 Loop While 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. It can be written at any place.
Initialization can be in or out of the loop Initialization 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
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

int main () {

/* definition */
int a = 5;

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

return 0;
}

 

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

Related GATE 2023 links
Difference Between drop and truncate Difference Between Website and Web application
Difference Between POP and OOP Difference Between Website and Webpage
Difference Between Abstraction and Encapsulation in Java Difference Between Constructor and Method in Java
Our Apps Playstore
POPULAR EXAMS
SSC and Bank
Other Exams
GradeStack Learning Pvt. Ltd.Windsor IT Park, Tower - A, 2nd Floor, Sector 125, Noida, Uttar Pradesh 201303 help@byjusexamprep.com
Home Practice Test Series Premium