Last Updated : 12 Jul, 2025
In C++, for loop is an entry-controlled loop that is used to execute a block of code repeatedly for the given number of times. It is generally preferred over while and do-while loops in case the number of iterations is known beforehand.
Let's take a look at an example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
// for loop to print "Hi" 5 times
for (int i = 5; i < 10; i++) {
cout << "Hi" << endl;
}
return 0;
}
In the above program, for loop prints the text "Hi" 5 times. It starts with the loop variable i set to 0 and increments i by 1 after each iteration. The loop continues as long as i is less than 5, so it runs for i values 0, 1, 2, 3, and 4, printing "Hi" in each iteration.
for Loop SyntaxThe syntax of for loop in C++ is shown below:
C++
for ( initialization; test condition; updation) {
// body of for loop
}
where,
Working of a for Loop in C++Note: The loop variable can also be declared in the initialization section but the scope of the loop variables that are declared in the initialization section is limited to the for loop block.
The working of for loop is as shown below:
The below examples demonstrate how to use the for loop in a C++ program along with the different possible variations of the loop.
Print Numbers in Reverse Order C++
#include <iostream>
using namespace std;
int main() {
// Initial value of number
int n = 5;
// Initialization of loop variable
int i;
for (i = n; i >= 1; i--)
cout << i << " ";
return 0;
}
In the above program, the loop variable i is iterated from n to 1 and in each test condition is checked (is i>=1). If true then it prints the value of i followed by a space and decrement i. When the condition is false loop terminates.
You may have noticed that we have not used braces {} in the body of the loop. We can skip braces {} till there is only one statement in the loop.
Print a Square Pattern using Nested Loops C++
#include <iostream>
using namespace std;
int main() {
// Outer loop to print each row
for (int i = 0; i < 4; i++) {
// Inner loop to print each
// character in each row
for (int j = 0; j < 4; j++) {
cout << "*" << " ";
}
cout << endl;
}
return 0;
}
* * * * * * * * * * * * * * * *
The above program uses nested for loops to print a 4x4 matrix of asterisks (*). Here, the outer loop (i) iterates over rows and the inner loop (j) iterates over columns. In each iteration, inner loop prints an asterisk, and a space. Also, a new line is added after each row is printed to shift the output to the next line.
Use Multiple Loop Variables in for Loop C++
#include <iostream>
using namespace std;
int main() {
// Defining two variable
int m, n;
// Loop having multiple variable and updations
for (m = 1, n = 1; m <= 3; m += 1, n += 2) {
cout << "iteration " << m << endl;
cout << "m is: " << m << endl;
cout << "j is: " << n << endl;
}
return 0;
}
iteration 1 m is: 1 j is: 1 iteration 2 m is: 2 j is: 3 iteration 3 m is: 3 j is: 5
The above program uses for loop with multiple variables (here m and n). It increments and updates both variables in each iteration and prints their respective values.
Infinite for LoopWhen 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.
C++
#include <iostream>
using namespace std;
int main() {
// Skip Initialization, test
// and update conditions
// for infinite for loop
for (;;) {
cout << "gfg" << endl;
}
return 0;
}
Output
gfgOther Types of for Loop in C++
gfg
.
.
.
infinite times
The above explained for loop is the actual legacy for loop that has been the part of the language since the beginning. But the different versions of for loop were added later in the languages. They are:
1. 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. It is much simpler as compared to traditional for loop. But the disadvantage of this is that it has limited applications.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
int nums[] = {1, 2, 3, 4, 5};
// Range-based for loop to print
// elements of the an array
for (int num : nums)
cout << num << " ";
return 0;
}
In the above code, we use a range-based for loop to print each element of the array, which automatically handles the iteration without requiring explicit variables to update or check conditions, unlike a traditional for loop where you need to manually manage the index and loop condition.
2. for_each Loop in C++C++ for_each loop is not a loop but an algorithm that mimics the range based for loop. It accepts a function that executes over each of the container elements. This loop is defined in the header file <algorithm> and hence has to be included for the successful operation of this loop.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
void print(int num) {
cout << num << " ";
}
int main() {
int nums[] = {1, 2, 3, 4, 5};
// Using for_each to print
// each element of the array
for_each(begin(nums), end(nums), print);
return 0;
}
In the above code, we initializes an array nums with values {1, 2, 3, 4, 5}. It then uses for_each to apply the print function to each element, printing each number in the array.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4