C++ for Loop

🔁 C++ for Loop – Count & Repeat with Ease

The for loop is perfect when you want to repeat something a fixed number of times. Like counting from 1 to 10, printing stars, or doing squats in a fitness app! 🏋️‍♂️⭐

🧠 Syntax of for Loop

for (initialization; condition; update) {
    // code to repeat
}
  • Initialization: Runs once at the start (e.g., int i = 1)
  • Condition: Checked before every loop (e.g., i <= 5)
  • Update: Runs after each loop (e.g., i++)

🔧 Example: Print 1 to 5

#include <iostream>
using namespace std;

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

Try It Now

✨ Example: Print Even Numbers

#include <iostream>
using namespace std;

int main() {
    for (int i = 2; i <= 10; i += 2) {
        cout << i << " is even" << endl;
    }
    return 0;
}

Try It Now

🎯 Why Use for Loop?

  • Best when the number of iterations is known.
  • Makes code clean and short.
  • Easy to control using variables.

With for loops, repetition becomes your programming superpower! 💪🧠