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:

  1. The loop starts with i = 1 (initialization).
  2. The condition i <= 5 is checked before each iteration. If true, the code block executes.
  3. After each iteration, i is incremented by 1 (iteration).
  4. 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:

  1. The outer loop runs from i = 1 to i = 3.
  2. For each iteration of the outer loop, the inner loop runs from j = 1 to j = 3.
  3. The product of i and j 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:

  1. The for loop has no initialization, condition, or iteration parts, creating an infinite loop.
  2. The break statement is used to exit the loop and prevent continuous execution.

Key Points about for Keyword

  1. The for keyword is used for loops with a known number of iterations.
  2. It consists of initialization, condition, and iteration parts, which control the loop.
  3. The initialization part runs once before the loop starts.
  4. The condition is checked before each iteration, and the loop exits if it evaluates to false.
  5. The iteration part executes after each loop iteration to update the loop variable.
  6. Infinite loops can be created with for (;;), but they should be used cautiously.