Introduction
The for
loop in C++ is a control flow statement that allows code to be executed repeatedly based on a given condition. It is particularly useful when you know in advance how many times you need to iterate through a block of code. The for
loop is one of the most commonly used loops in programming due to its compact structure and clarity.
Syntax
The basic syntax of a for
loop is as follows:
for (initialization; condition; increment) {
// code to be executed
}
Key Points
- Initialization: Sets up a loop control variable and is executed once at the beginning.
- Condition: Evaluated before each iteration. If
true
, the loop body is executed. Iffalse
, the loop terminates. - Increment: Updates the loop control variable after each iteration.
Example: Basic for Loop
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 5; i++) {
cout << "i = " << i << endl;
}
return 0;
}
Output
i = 0
i = 1
i = 2
i = 3
i = 4
Explanation
- Initialization:
int i = 0
initializes the loop control variablei
to 0. - Condition:
i < 5
checks ifi
is less than 5. Iftrue
, the loop body executes. - Increment:
i++
incrementsi
by 1 after each iteration.
Example: for Loop with Different Steps
You can control the step size in a for
loop by modifying the increment part.
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 10; i += 2) {
cout << "i = " << i << endl;
}
return 0;
}
Output
i = 0
i = 2
i = 4
i = 6
i = 8
Explanation
- Increment:
i += 2
incrementsi
by 2 after each iteration.
Example: for Loop in Reverse Order
You can also use a for
loop to iterate in reverse order.
#include <iostream>
using namespace std;
int main() {
for (int i = 5; i > 0; i--) {
cout << "i = " << i << endl;
}
return 0;
}
Output
i = 5
i = 4
i = 3
i = 2
i = 1
Explanation
- Initialization:
int i = 5
initializes the loop control variablei
to 5. - Condition:
i > 0
checks ifi
is greater than 0. Iftrue
, the loop body executes. - Increment:
i--
decrementsi
by 1 after each iteration.
Example: Nested for Loop
You can nest for
loops to create a loop within a loop, which is useful for multidimensional data structures like matrices.
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
cout << "i = " << i << ", j = " << j << endl;
}
}
return 0;
}
Output
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
i = 2, j = 2
i = 2, j = 3
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3
Explanation
- The outer loop (
for (int i = 1; i <= 3; i++)
) iterates 3 times. - The inner loop (
for (int j = 1; j <= 3; j++)
) iterates 3 times for each iteration of the outer loop.
Example: Sum of First N Natural Numbers
A for
loop can be used to calculate the sum of the first N natural numbers.
#include <iostream>
using namespace std;
int main() {
int n, sum = 0;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= n; ++i) {
sum += i;
}
cout << "Sum of the first " << n << " natural numbers is: " << sum << endl;
return 0;
}
Output
Enter a positive integer: 10
Sum of the first 10 natural numbers is: 55
Example: Multiplication Table
A for
loop can be used to print the multiplication table of a given number.
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
for (int i = 1; i <= 10; ++i) {
cout << num << " * " << i << " = " << num * i << endl;
}
return 0;
}
Output
Enter a number: 5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Conclusion
The for
loop is a powerful and flexible control flow statement in C++ that allows you to execute a block of code multiple times based on a given condition. This chapter covered the basic syntax of the for
loop, examples of using different step sizes, iterating in reverse order, and using nested for
loops. Additionally, we demonstrated simple programs to calculate the sum of the first N natural numbers and print a multiplication table. Understanding how to use the for
loop effectively will help you write more efficient and readable code. In the next chapter, we will explore the while
loop in C++.