Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is While Loop?
2.1.
Flow Diagram for While loop
2.2.
While loop Syntax
2.3.
Examples of While loop
2.4.
C++
2.5.
C++
3.
What is For Loop?
3.1.
Flow Diagram for For loop
3.2.
For loop syntax
3.3.
Examples of For Loop
3.4.
C++
3.5.
C++
4.
Difference Between For Loop and While Loop
5.
Frequently Asked Questions
5.1.
What is the difference between for loop and for in loop?
5.2.
What is the difference between for and for each loop?
5.3.
Is a for loop or a while loop more efficient?
5.4.
What are the 3 types of loops?
6.
Conclusion
Last Updated: Jul 11, 2024
Easy

Difference between for Loop and while Loop

Author Riya Singh
0 upvote

Introduction

When you are introduced to the concept of programming, you might have heard about loops. It executes the set of commands repeatedly until the given condition is true.

In computer programming, loops are blocks of code that execute multiple times. Without loops, you would have to write things you want to execute repeatedly.

Difference between for and while Loop

Loops are the control structure of a program. Using loops in a program can simplify the coding process. In this article, we talk about the difference between for loop and while loop with the help of programs and relevant definitions. So without any further ado, let's get started!

Also see, Fibonacci Series in C++

What is While Loop?

While loop is also known as an entry-controlled loop. In the while loop, we check the condition at the start of the loop. The code block will run until the condition is true, and if the condition is false, the loop gets terminated or ended. If the condition remains true, then it will be an infinite loop.

Flow Diagram for While loop

The following flowchart can help you visualize the flow of the while loop.

Flow Diagram for While loop

Explanation:
Step 1. The condition is evaluated and if the condition is true, the statements start executing.

Step 2.  The process will continue until the condition returns true. As it gets false, then the loop will be terminated. 

Let's see the syntax to implement a while loop:

While loop Syntax

while (condition) {
	statements;
}

The while loop checks the condition which is given. The statements inside the loop will be executed if the condition is true. This process continues until the condition is true. This is a major difference between for loop and while loop.

Examples of While loop

Following are some examples of while loop for better understanding.

Example 1:

Here in this example, we are printing numbers from 10 to 0 in descending order using a while loop. The code for the same is:

  • C++

C++

#include <bits/stdc++.h>
using namespace std;
int main() {
   int count = 10;
   while(count >=  0){
       cout << count << " ";
       count--;
   }
   cout << endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output:

10 9 8 7 6 5 4 3 2 1 0

Example 2:

Here in this example, we iterate over the vector “arr” and print all its elements: 

  • C++

C++

#include <bits/stdc++.h>
using namespace std;


int main() {
   vector<int>arr = {2,4,6,8,10,12};
   int iter = 0;
   while(iter < arr.size()){
       cout << arr[iter] << " ";
       iter++;
   }
   cout << endl;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

2 4 6 8 10 12

What is For Loop?

For loop is used where we know when to start and end, i.e., when the number of iterations is known. For loop is also known as an entry-controlled loop. Here also, we iterate over the loop until the condition is true and execute the statements in the loop. We can iterate over arrays, strings, etc., easily using a for loop. 

Flow Diagram for For loop

Following is the flow diagram of execution for the For loop.

Flow Diagram for For loop

Explanation:
Step 1. The condition is evaluated and if the condition is true, the statements start executing.

Step 2. The process will go on until the given number of iterations, then the loop will be terminated, and the compiler will move to the next block of code.

Following is the structure of the For loop.

For loop syntax

for(variable(s) initialization; condition(s); increment(s) {
	statement(s);
}

In initialization, we define the variables with their starting value. The variable works as an iterator or counter that controls the flow of the loop. The condition(s) is until when the loop has to repeat, and increment updates the variable(s). 

Examples of For Loop

The following example will help you understand the for loop in a better way.

Example 1:

Here in this example, we print the multiples of 9 to 5 using For loop. We already knew that the loop would only execute 5 times as variable ‘i’ goes from 1 to 5.

  • C++

C++

#include <bits/stdc++.h>
using namespace std;

int main() {
   for(int i = 1; i <= 5 ;i++){
       cout << 9*i << " ";
   }
   cout << endl;
}
You can also try this code with Online C++ Compiler
Run Code

Output:

9 18 27 36 45

Example 2:

In this example, we will print all the elements of a given vector using for loop. Auto can also be used in a for loop. It automatically specifies the data type of the variable that is being returned.

  • C++

C++

#include <bits/stdc++.h>
using namespace std;


int main() {
   vector<int>arr = {2,4,6,8,10,12};
   cout << "Output through basic for loop-> ";
   for(int i = 0; i < arr.size() ;i++){
       cout << arr[i] << " ";
   }
   cout << endl;
   cout << "Output using auto in for loop-> ";
   for(auto i:arr){
       cout << i << " ";
   }
   cout << endl;
}
You can also try this code with Online C++ Compiler
Run Code

Output:

Output through basic for loop → 2 4 6 8 10 12
Output using auto in for loop → 2 4 6 8 10 12

Now let us move to the next section of the blog to know more about the difference between for loop and while loop.

Read about: Four Pillars of OOPS

Difference Between For Loop and While Loop

In this section of the blog, we will compare the two loops we learned in detail until now. Now it's time to discuss the difference between for loop and while loop. 

Parameters

For loop

While loop

IterationsWe know the number of iterations in for loop.The number of iterations is unknown in a while loop.
InitializationInitialization of variables can be in or outside the loop.Initialization is always outside the loop.
ErrorsIf no condition is specified in for loop, we will encounter a compile-time error occurs.If no condition is specified in the while loop, then the loop executes for infinite times.
SpeedFor loop is faster than a while loop.While loop is comparatively slower.
UsageIt is used when initialization and increment are simple.It is used when initialization is complex.

Related Article What are loops in Python.

Frequently Asked Questions

What is the difference between for loop and for in loop?

A "for" loop in Python iterates through items in a sequence, such as lists or strings. On the other hand, a "for...in" loop access both key and value as iterates over elements in an iterable object, such as dictionaries or sets.

What is the difference between for and for each loop?

The "for" loop is used for a specific number of iterations, while the "for each" loop is used to iterate over elements in a collection, like an array.

Is a for loop or a while loop more efficient?

Efficiency depends on the specific use case. "For" loops are often more efficient for a predetermined number of iterations, while "while" loops are flexible but may require more conditional checks.

What are the 3 types of loops?

The three types of loops are "for" loops, "while" loops, and "do-while" loops. They serve different purposes and are used in various programming situations.

Conclusion

We hope this article was insightful and you learned something new. In this blog, we learned in detail about the while loop and its implementation. We also learned about the for loop and its implementation. We discussed their syntax and some examples. In the end, we saw the difference between for loop and while loop.

If you want to learn about loops and the difference between for loop and while loop in Python, do visit Loops in Python

You also check the following links to know more about loops.

Do visit our website to read more such blogs. You can take mock testssolve problems, and interview puzzles. You can also visit interview experiences and interview bundles for exciting placement preparations. 

Do upvote our blogs if you find them helpful and engaging!

Live masterclass