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.
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!
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.
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
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.
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.
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
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
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
Iterations
We know the number of iterations in for loop.
The number of iterations is unknown in a while loop.
Initialization
Initialization of variables can be in or outside the loop.
Initialization is always outside the loop.
Errors
If 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.
Speed
For loop is faster than a while loop.
While loop is comparatively slower.
Usage
It is used when initialization and increment are simple.
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.