> ## Documentation Index
> Fetch the complete documentation index at: https://programming-for-career.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Loops in C++

> A comprehensive guide to loops in C++ with real-life examples, use cases, and differences.

## Introduction

Loops allow us to execute a block of code multiple times. In real life, we often repeat tasks:

* **Brushing teeth every morning** 🪥
* **Checking messages repeatedly** 📱
* **Counting steps while walking** 🚶‍♂️

Similarly, in programming, loops help in running repetitive tasks efficiently.

## Types of Loops in C++

1. `for` loop
2. `while` loop
3. `do-while` loop

Each serves a different purpose. Let's explore them one by one. 🔍

## 1️⃣ `for` Loop

The `for` loop is used when **we know how many times** the loop should run.

### Syntax:

```cpp theme={null}
for(initialization; condition; increment/decrement) {
    // Code to execute
}
```

### Example: Counting from 1 to 5

```cpp theme={null}
#include <iostream>
using namespace std;

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

### Real-Life Analogy:

* Imagine setting an **alarm to ring 5 times.** ⏰
* The `for` loop ensures it rings exactly 5 times.

### When to Use `for` Loop?

✅ When the number of iterations is **known in advance.**

## 2️⃣ `while` Loop

The `while` loop is used when **we don't know how many times** it should run but have a stopping condition.

### Syntax:

```cpp theme={null}
while(condition) {
    // Code to execute
}
```

### Example: User enters a number until they type 0

```cpp theme={null}
#include <iostream>
using namespace std;

int main() {
    int num;
    cout << "Enter numbers (0 to stop): ";
    cin >> num;

    while(num != 0) {
        cout << "You entered: " << num << endl;
        cin >> num;
    }
    return 0;
}
```

### Real-Life Analogy:

* **Waiting at a traffic signal 🚦**: You stop only when the light turns green.
* The loop keeps running **until** a stopping condition is met.

### When to Use `while` Loop?

✅ When the **number of iterations is unknown** and depends on a condition.

## 3️⃣ `do-while` Loop

The `do-while` loop is similar to `while`, but it ensures **the loop runs at least once.**

### Syntax:

```cpp theme={null}
do {
    // Code to execute
} while (condition);
```

### Example: Ask for a password until correct

```cpp theme={null}
#include <iostream>
using namespace std;

int main() {
    string password;
    do {
        cout << "Enter password: ";
        cin >> password;
    } while(password != "secret");

    cout << "Access granted!" << endl;
    return 0;
}
```

### Real-Life Analogy:

* **Asking for Wi-Fi password** until you enter the correct one. 📶

### When to Use `do-while` Loop?

✅ When you **must run the loop at least once,** regardless of conditions.

## `break` and `continue` in Loops ⚡

### `break`: Exits the loop immediately.

Example: Stop counting at 3.

```cpp theme={null}
for(int i = 1; i <= 5; i++) {
    if(i == 3) break;
    cout << i << endl;
}
```

Output:

```
1
2
```

### `continue`: Skips the current iteration and moves to the next.

Example: Skip number 3.

```cpp theme={null}
for(int i = 1; i <= 5; i++) {
    if(i == 3) continue;
    cout << i << endl;
}
```

Output:

```
1
2
4
5
```

### Real-Life Analogy:

* **break** → Leaving a movie theater 🍿 when it gets boring.
* **continue** → Skipping an ad while watching YouTube. 🎥

## Differences Between Loops

| Feature             | `for` Loop                      | `while` Loop                 | `do-while` Loop               |
| ------------------- | ------------------------------- | ---------------------------- | ----------------------------- |
| **Used When?**      | Fixed number of iterations      | Unknown number of iterations | At least one execution needed |
| **Condition Check** | Before iteration                | Before iteration             | After iteration               |
| **Best For?**       | Counting, iterating over arrays | Waiting for user input       | Menus, login attempts         |

## Conclusion

Loops make programs efficient by handling repetition automatically. We explored:

* `for` → When the number of iterations is known.
* `while` → When iterations depend on a condition.
* `do-while` → When at least one execution is required.
* `break` & `continue` for controlling loop flow.

Mastering loops is key to writing efficient C++ programs!

### Up Next: Functions in C++
