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:

for(initialization; condition; increment/decrement) {
    // Code to execute
}

Example: Counting from 1 to 5

#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:

while(condition) {
    // Code to execute
}

Example: User enters a number until they type 0

#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:

do {
    // Code to execute
} while (condition);

Example: Ask for a password until correct

#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.

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.

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

Featurefor Loopwhile Loopdo-while Loop
Used When?Fixed number of iterationsUnknown number of iterationsAt least one execution needed
Condition CheckBefore iterationBefore iterationAfter iteration
Best For?Counting, iterating over arraysWaiting for user inputMenus, 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++