📅 Date: Jan 4, 2026
🔥 Topic: Loops (For Loop), Break & Continue


🔄 Why Loop? (DRY Principle)

Imagine I want to print "I love Coding" 100 times. Writing 100 cout statements is stupid. In programming, we follow the DRY Principle (Don't Repeat Yourself).

Loops allow us to run the same block of code multiple times until a condition is met.

[attachment_0](attachment)

⚙️ The For Loop Syntax

The for loop is used when we know exactly how many times we want to iterate.

for (initialization; condition; update) {
    // Body of the loop
}
  • Initialization: Starting point (e.g., int i = 1).
  • Condition: Keep running as long as this is true (e.g., i <= 10).
  • Update: Change the value after every round (e.g., i++).

🛑 Break vs. ⏩ Continue

Sometimes we need to control the loop manually:

  • Break: Kills the loop immediately. "Stop right here!"
  • Continue: Skips the current round and goes to the next one. "Skip this, do the next."

💻 Day 4 Code: The Skipping Game

I wrote a program that prints numbers from 1 to 10, but it skips '5' (Superstition? Maybe) and stops completely at '8'.

#include <iostream>
using namespace std;

int main() {
    cout << "--- Loop Start ---" << endl;

    for (int i = 1; i <= 10; i++) {
        
        if (i == 5) {
            cout << "Skipping 5..." << endl;
            continue; // Go to next iteration
        }

        if (i == 8) {
            cout << "Breaking at 8!" << endl;
            break; // Stop the loop
        }

        cout << "Count: " << i << endl;
    }

    cout << "--- Loop End ---" << endl;
    return 0;
}

💭 Thoughts

Loops are powerful. If I mess up the condition (like forgetting i++), I can create an Infinite Loop that crashes my program. Scary, but fun to learn.