for Loop in C++ (original) (raw)
The for loop is an entry-controlled loop used to execute a block of code repeatedly as long as a specified condition remains true. It is commonly used when the number of iterations is known in advance.
- Combines initialization, condition checking, and update in a single statement.
- Widely used for counting, traversing arrays, processing containers, and pattern generation. 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;}
`
**Explanation: The loop variable i starts from 1 and continues until i becomes greater than 5. After each iteration, i is incremented by 1, causing the loop body to execute five times.
Syntax
for (initialization; condition; update) {
// Loop body
}
**where:
- **Initialization: Executes once before the loop starts and is generally used to initialize the loop variable.
- **Condition: Evaluated before every iteration. If it becomes false, the loop terminates.
- **Update: Executes after every iteration and is generally used to modify the loop variable.
**Note: Variables declared in the initialization section are accessible only within the loop.
Working of for Loop in C++
The working of a for loop is as follows:
- Initialization is executed once.
- The condition is evaluated.
- If the condition is true, the loop body executes.
- The update expression is executed.
- Control returns to Step 2.
- When the condition becomes false, the loop terminates.
Flowchart of for Loop
The following flowchart illustrates the execution flow of a for loop in C++.

Flowchart of for Loop in C++
Examples
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
The following program prints numbers from 5 to 1 using a for loop.
C++ `
#include 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;}
`
**Explanation: The loop starts from 5 and decrements the value of i after every iteration until it reaches 1.
Calculate Sum from 1 to 20
The following program calculates the sum of the first 20 natural numbers.
C++ `
#include using namespace std;
int main() {
int sum = 0;
for (int i = 1; i <= 20; i++) {
sum += i;
}
cout << "Sum: " << sum;
return 0;}
`
**Explanation: The loop iterates from 1 to 20 and continuously adds each value to sum.
Nested for Loop
A nested for loop is a loop placed inside another for loop. It is commonly used for matrix operations and pattern printing.
Print a Square Pattern
C++ `
#include 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;}
`
Output
**Explanation: The outer loop controls rows, while the inner loop prints the columns of each row.
**Note: To learn more about nested loops, refer to the article on Nested Loops in C++.
Using Multiple Loop Variables
A for loop can contain multiple initialization and update expressions.
C++ `
#include using namespace std;
int main() {
int m, n;
for (m = 1, n = 1; m <= 3; m++, n += 2) {
cout << "m = " << m
<< ", n = " << n << endl;
}
return 0;}
`
Output
m = 1, n = 1 m = 2, n = 3 m = 3, n = 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 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.
C++ `
#include using namespace std;
int main() {
// Skip Initialization, test
// and update conditions
// for infinite for loop
for (;;) {
cout << "gfg" << endl;
}
return 0;}
`
**Output
gfg
gfg
.
.
.
_infinite times
**Note: Infinite loops should be used carefully, as they can cause a program to run indefinitely.
C++ supports additional looping constructs such as range-based for loops and the for_each() algorithm.