Iteration in C++

Iteration in C++ refers to the process of repeating a set of instructions until a specific condition is met. This is typically achieved using loops. C++ provides several types of loops for iteration:

 1. **`for` Loop**
The `for` loop is commonly used when the number of iterations is known in advance.

```cpp
for (initialization; condition; update) {
    // Code to be executed
}
```

**Example:**
```cpp
#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 5; i++) {
        cout << "Iteration: " << i << endl;
    }
    return 0;
}
```

**Output:**
```
Iteration: 0
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
```

---

 2. **`while` Loop**
The `while` loop is used when the number of iterations is not known in advance, and the loop continues as long as the condition is true.

```cpp
while (condition) {
    // Code to be executed
}
```

**Example:**
```cpp
#include <iostream>
using namespace std;

int main() {
    int i = 0;
    while (i < 5) {
        cout << "Iteration: " << i << endl;
        i++;
    }
    return 0;
}
```

**Output:**
```
Iteration: 0
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
```

---

 3. **`do-while` Loop**
The `do-while` loop is similar to the `while` loop, but it guarantees that the loop body is executed at least once, even if the condition is false.

```cpp
do {
    // Code to be executed
} while (condition);
```

**Example:**
```cpp
#include <iostream>
using namespace std;

int main() {
    int i = 0;
    do {
        cout << "Iteration: " << i << endl;
        i++;
    } while (i < 5);
    return 0;
}
```

**Output:**
```
Iteration: 0
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
```

---

4. **Range-based `for` Loop (C++11 and later)**
The range-based `for` loop is used to iterate over elements in a container (e.g., arrays, vectors, etc.).

```cpp
for (element : container) {
    // Code to be executed
}
```

**Example:**
```cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> numbers = {1, 2, 3, 4, 5};
    for (int num : numbers) {
        cout << "Number: " << num << endl;
    }
    return 0;
}
```

**Output:**
```
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
```

---
 5. **Nested Loops**
Loops can be nested within each other to handle more complex iteration scenarios.

**Example:**
```cpp
#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 2; j++) {
            cout << "i = " << i << ", j = " << j << endl;
        }
    }
    return 0;
}
```

**Output:**
```
i = 0, j = 0
i = 0, j = 1
i = 1, j = 0
i = 1, j = 1
i = 2, j = 0
i = 2, j = 1
```

---

6. **Control Statements in Loops**
- **`break`**: Exits the loop immediately.
- **`continue`**: Skips the rest of the loop body and proceeds to the next iteration.
- **`return`**: Exits the function (and thus the loop).

**Example:**
```cpp
#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 10; i++) {
        if (i == 5) {
            break; // Exit the loop when i is 5
        }
        if (i % 2 == 0) {
            continue; // Skip even numbers
        }
        cout << "Odd Number: " << i << endl;
    }
    return 0;
}
```

**Output:**
```
Odd Number: 1
Odd Number: 3
```

---
 Summary
- Use `for` loops when the number of iterations is known.
- Use `while` or `do-while` loops when the number of iterations is not known.
- Use range-based `for` loops for iterating over containers.
- Use `break`, `continue`, and `return` to control loop execution.


Comments

Popular posts from this blog

Kotlin Math Operations and Functions Overview

Kotlin Strings: Features and Operations Guide

Kotlin Android Program (QCR) Application Codes That Read Text in Photos