C++ for Keyword
The for
keyword in C++ is used to implement a for loop, which is a control flow statement that allows code to be executed repeatedly for a specified number of iterations. It is ideal when the number of iterations is known beforehand.
A for
loop consists of three parts: initialization, condition, and iteration. These parts control the starting point, continuation condition, and progression of the loop.
Syntax
</>
Copy
for (initialization; condition; iteration) {
// Code to execute in each iteration
}
- initialization
- Executed once, it sets the initial state of the loop variable.
- condition
- A boolean expression checked before each iteration. The loop continues as long as this evaluates to
true
. - iteration
- Executed after each iteration, it updates the loop variable.
- Code block
- The code to be executed on each iteration if the condition evaluates to
true
.
Examples
Example 1: Simple for
Loop
In this example, we will learn how to use a basic for
loop to print numbers from 1 to 5.
</>
Copy
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; ++i) {
cout << "Number: " << i << endl;
}
return 0;
}
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Explanation:
- The loop starts with
i = 1
(initialization). - The condition
i <= 5
is checked before each iteration. Iftrue
, the code block executes. - After each iteration,
i
is incremented by 1 (iteration). - The loop stops when
i > 5
.
Example 2: Nested for
Loop
In this example, we will learn how to use a nested for
loop to print a multiplication table.
</>
Copy
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 3; ++i) {
for (int j = 1; j <= 3; ++j) {
cout << i << " x " << j << " = " << (i * j) << endl;
}
cout << endl;
}
return 0;
}
Output:
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
Explanation:
- The outer loop runs from
i = 1
toi = 3
. - For each iteration of the outer loop, the inner loop runs from
j = 1
toj = 3
. - The product of
i
andj
is printed in each inner loop iteration.
Example 3: Infinite for
Loop
In this example, we will learn how to write an infinite loop using the for
keyword.
</>
Copy
#include <iostream>
using namespace std;
int main() {
for (;;) {
cout << "This is an infinite loop." << endl;
}
return 0;
}
Output:
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
This is an infinite loop.
...
Explanation:
- The
for
loop has no initialization, condition, or iteration parts, creating an infinite loop. - The
break
statement is used to exit the loop and prevent continuous execution.
Key Points about for
Keyword
- The
for
keyword is used for loops with a known number of iterations. - It consists of initialization, condition, and iteration parts, which control the loop.
- The initialization part runs once before the loop starts.
- The condition is checked before each iteration, and the loop exits if it evaluates to
false
. - The iteration part executes after each loop iteration to update the loop variable.
- Infinite loops can be created with
for (;;)
, but they should be used cautiously.