100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

for Loop in C++

Learn the C++ `for` loop syntax, how init/condition/increment control iteration, and when it is the best choice for a known number of repetitions.

LoopsBeginner6 min readJul 7, 2026
Analogies

1. Intro

The for loop is C++'s go-to construct when you know in advance how many times a block of code should run — for example, iterating over an array index or counting from 1 to n. It packs initialization, condition-checking, and updating into a single line, which makes the loop's control logic easy to see at a glance.

🏏

Cricket analogy: A captain setting a fielder to bowl exactly 10 overs decides the count upfront, just like a for loop fixes how many times it will run before the first ball is bowled.

2. Syntax

cpp
for (initialization; condition; update) {
    // loop body
}

3. Explanation

A for loop has three parts separated by semicolons. The initialization runs exactly once, before the loop starts (typically declaring a counter like int i = 0). The condition is a boolean expression checked before every iteration — if it evaluates to false, the loop ends immediately without running the body. The update expression (commonly i++) runs after each iteration's body completes, then control returns to the condition check. Because the condition is tested first, a for loop can execute zero times if the condition is false on the very first check (e.g. for (int i = 0; i < 0; i++)).

🏏

Cricket analogy: The umpire checks over-limit before every over (condition), the bowler runs in and bowls (body), then the over count increments (update) — and if the limit is already reached, no over is bowled at all.

4. Example

cpp
#include <iostream>
using namespace std;

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

5. Output

text
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

6. Key Takeaways

  • A for loop groups initialization, condition, and update in one header, ideal for a known iteration count.
  • The condition is checked before each iteration, so the body may run zero times.
  • Any of the three clauses can be omitted (e.g. for (;;)), but the semicolons must stay.
  • Variables declared in the initialization (like i) are scoped to the loop only.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#ForLoopInC#Loop#Syntax#Explanation#Example#Loops#StudyNotes#SkillVeris