C++ for Loop (With Examples)
Prerequisite: Loops in C++
C++ for loop is a repetition control structure that allows us to write a loop that is executed a specific number of times. for loop is generally preferred over while and do-while loops when the number of iteration are known beforehand.
for loop is an entry-controlled loop where the test condition is checked before entering the body.
Syntax:
for(initialization; test condition; updation)
{
// body of for loop
}The various parts of the for loop are:
1. Initialization Expression in for Loop
In this expression, we have to initialize the loop variable to some value.
Example:
int i = 1;
2. Test Condition in for Loop
In this expression, we have to test the condition. If the condition evaluates to true then we will execute the body of the loop and go to the update expression. Otherwise, we will exit from the for loop.
Example:
int i < 6;
3. Update Expression in for Loop
After executing the loop body, this expression increments/decrements the loop variable by some value.
Example:
int i = i + 1;
Flowchart of for Loop in C++ (For Control Flow)

Flowchart of for Loop in C++
How does a for loop execute?
- Control falls into the for loop. Initialization is done
- The flow jumps to Condition
- Condition is tested.
- If the Condition yields true, the flow goes into the Body
- If the Condition yields false, the flow goes outside the loop
- The statements inside the body of the loop get executed.
- The flow goes to the Updation
- Updation takes place and the flow goes to Step 3 again
- The for loop has ended and the flow has gone outside.
The below example demonstrates the use of for loop in a C++ program.
Example:
C++
// C++ program to illustrate for loop#include <iostream>using namespace std;int main(){ // Writing a for loop // to print Hello World 5 times int i; for (i = 1; i <= 5; i++) { cout << "Hello World\n"; } return 0;} |
Hello World Hello World Hello World Hello World Hello World
Algorithm of the Above Example:
1. Program starts. 2. i is initialized with value 1. 3. Condition is checked. 1 <= 5 yields true. 3.a) "Hello World" gets printed 1st time. 3.b) Updation is done. Now i = 2. 4. Condition is checked. 2 <= 5 yields true. 4.a) "Hello World" gets printed 2nd time. 4.b) Updation is done. Now i = 3. 5. Condition is checked. 3 <= 5 yields true. 5.a) "Hello World" gets printed 3rd time 5.b) Updation is done. Now i = 4. 6. Condition is checked. 4 <= 5 yields true. 6.a) "Hello World" gets printed 4th time 6.b) Updation is done. Now i = 5. 7. Condition is checked. 5 <= 5 yields true. 7.a) "Hello World" gets printed 5th time 7.b) Updation is done. Now i = 6. 8. Condition is checked. 6 <= 5 yields false. 9. Flow goes outside the loop to return 0
In the above program, we have separate declaration and initialization of the loop variable. In for loop, we can declare and initialize the loop variable in the initialization section.
Example:
C++
// c++ program to demonstrate the use of for loop#include <iostream>using namespace std;int main(){ // for loop with variable declaration in the // initialization block for (int i = 1; i < 6; i++) { cout << "GeeksforGeeks\n"; } return 0;} |
GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks
Note: Scope of the loop variables that are declared in the initialization section is limited to the for loop block.
Multiple Variables in for Loop
We can initialize, test, and update multiple variables in the for loop.
Example:
C++
// C++ program to demonstrate the// use of multiple variables// in for loops#include <iostream>using namespace std;int main(){ int i, j; for (i = 1, j = 5; i < 5; i++, j++); cout << "Value of i = " << i << endl; cout << "Value of j = " << j << endl; return 0;} |
Value of i = 5 Value of j = 9
Infinite for Loop/NULL Parameter Loop
When no parameters are given to the for loop, it repeats endlessly due to the lack of input parameters, making it a kind of infinite loop.
Example:
C++
// C++ Program to demonstrate infinite loop#include <iostream>using namespace std;int main(){ // condition 1, 2 and 3 are not entered for ( ; ; ) { cout << "GeeksforGeeks\n"; } // Return statement to tell that everything executed // safely return 0;} |
Output
GeeksforGeeks GeeksforGeeks . . . (infinite)
Range-Based for loop in C++
C++ range-based for loops execute for loops over a range of values, such as all the elements in a container, in a more readable way than the traditional for loops.
Syntax:
for ( range_declaration : range_expression )
{
loop_body
}Example:
C++
// C++ Program to demonstrate the// use of range based for// loop in C++#include <iostream>#include <vector>using namespace std;int main(){ cout << "Array Values are: " << endl; int arr[] = { 0, 1, 2, 3, 4, 5 }; // using range based for loop for iterating array for (int x : arr) { cout << x << " "; } cout << endl; cout << "Vector Values are: " << endl; vector<int> v = { 0, 1, 2, 3, 4, 5 }; // using range-based for loop for iterating over vector for (auto x : v) { cout << x << " "; } cout << endl; return 0;} |
Array Values are: 0 1 2 3 4 5 Vector Values are: 0 1 2 3 4 5
To know more about range-based loops, click here.
for_each Loop in C++
C++ for_each loop accepts a function that executes over each of the container elements. This loop is defined in the header file “algorithm”: #include<algorithm>, and hence has to be included for the successful operation of this loop.
Syntax:
for_each (InputIterator start_iter, InputIterator last_iter, Function fnc);
Parameters:
- start_iter: Iterator to the beginning position from where function operations have to be executed.
- last_iter: Iterator to the ending position till where function has to be executed.
- fnc: The 3rd argument is a function or a function object which would be executed for each element.
Example:
C++
// C++ Program to show use of for_each loop#include <bits/stdc++.h>using namespace std;int main(){ vector<int> vec{ 0,1, 2, 3, 4 }; // this will increase all the values in the vector by 2 for_each(vec.begin(), vec.end(), [](int& i) { i += 2; }); // this for_each loop will print all the values in the vector for_each(vec.begin(), vec.end(), [](int i) { cout << i << " " << endl; }); return 0;} |
2 3 4 5 6
To know more about for_each loop, refer to the article – for_each loop in C++
Related Articles:
- C++ while loop with Examples
- C/C++ do while loop with Examples
- Difference between while and do-while loop in C++
- Difference between for and while loop in C++


Please Login to comment...