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
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
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
cout << "Count: " << i << endl;
}
return 0;
}5. Output
Count: 1
Count: 2
Count: 3
Count: 4
Count: 56. Key Takeaways
- A
forloop groups initialization, condition, and update in one header, ideal for a known iteration count. - The
conditionis 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(likei) are scoped to the loop only.
Practice what you learned
1. Which part of a `for` loop executes only once, before the loop begins?
2. How many times does the loop body of `for (int i = 0; i < 0; i++)` execute?
3. What is the output of: `for (int i = 1; i <= 3; i++) cout << i;`?
4. In `for (init; condition; update)`, when does `update` run relative to the body?
5. Which of these is a syntactically valid `for` loop in C++?
Was this page helpful?
You May Also Like
while Loop in C++
Understand the C++ `while` loop, how its entry condition is tested before every iteration, and when to use it over a `for` loop.
do-while Loop in C++
Learn how the C++ `do-while` loop guarantees at least one execution by checking its condition after the body runs, with a full iteration trace.
Nested Loops in C++
See how placing one loop inside another lets you process grids and patterns in C++, with a full trace of outer × inner iteration counts.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics