Sooner or later you need to do the same action many times—check several candidates, accumulate a sum over steps, run a procedure for a fixed number of iterations. Writing the same line ten times won’t scale. A loop solves this by repeating a block of code automatically.
The classic C++ for loop is the simplest way to say “do this block exactly N times.” It uses a counter (an integer that changes each round) and a condition that decides whether another round should happen. Understanding this control flow is often the main hurdle for new students; once the mental model clicks, loops become routine.
Anatomy of a for loop
for (initialization; condition; step) {
// body: the work to repeat
}
- initialization — runs once, before the loop starts (commonly sets a counter).
- condition — checked before each round; if
true, the body runs; iffalse, the loop ends. - step — runs after each round (commonly updates the counter).
Execution order:
- initialization → 2) check condition → 3) body → 4) step → back to (2)
Always use braces {} for the body, even for a single statement.
First example: count from 1 to 5
#include <iostream>
int main() {
for (int i = 1; i <= 5; i = i + 1) {
std::cout << i << '\n';
}
return 0;
}
- Starts with
i = 1. - Checks
i <= 5. If true, printsi, then updatesi = i + 1. - Repeats until the condition becomes false.
- The body runs five times, printing 1, 2, 3, 4, 5 on separate lines.
i = i + 1is the most explicit way to increment. You’ll often see the shorthand++i; we’ll return to that later.
Off-by-one: inclusive vs. exclusive bounds
Two common patterns:
Inclusive end (1 through N)
for (int i = 1; i <= N; i = i + 1) {
// i takes values: 1, 2, ..., N
}
Exclusive end (0 up to N, but not N)
for (int i = 0; i < N; i = i + 1) {
// i takes values: 0, 1, ..., N-1
}
Pick the one that matches your problem. Many formulas and data structures use the exclusive pattern because “count N things” naturally maps to 0...N-1.
Reading the count from the user
#include <iostream>
int main() {
int N;
std::cout << "How many lines? ";
std::cin >> N;
for (int i = 0; i < N; i = i + 1) {
std::cout << "Line " << (i + 1) << '\n';
}
return 0;
}
If N is 0 or negative, the condition i < N is false immediately and the loop body doesn’t run at all. Zero iterations are perfectly valid.
Counting down
You can run the counter backwards. Just make the initialization, condition, and step match:
#include <iostream>
int main() {
for (int i = 5; i >= 1; i = i - 1) {
std::cout << i << '\n';
}
std::cout << "Lift-off!\n";
return 0;
}
Accumulating a result
Loops often build up a value, like a sum:
#include <iostream>
int main() {
int N;
std::cout << "Sum from 1 to N. Enter N: ";
std::cin >> N;
int sum = 0;
for (int i = 1; i <= N; i = i + 1) {
sum = sum + i; // add the current i
}
std::cout << "Sum = " << sum << '\n';
return 0;
}
Scope of the loop variable
When you declare the counter inside the for parentheses, it belongs to the loop and disappears after it ends:
for (int i = 0; i < 10; i = i + 1) {
// i is usable here
}
// i is NOT visible here
If you need the counter afterward, declare it before the loop:
int i;
for (i = 0; i < 10; i = i + 1) {
// ...
}
// i is visible here (its final value is 10)
Practical tips
- Match the three parts so the loop makes progress toward ending. If the condition can never become false, you’ve made an infinite loop.
- Keep the condition simple and predictable. Most bugs are off-by-one: re-check your start, comparison (
<vs<=), and step. - Avoid changing the counter inside the body unless you have a strong reason—that’s a common source of confusion.
- Prefer explicit
i = i + 1ori = i - 1when starting out; switch to++i/--ionce the idea is solid.
Key takeaways
- A
forloop repeats a block using three parts: initialization, condition, step. - The execution order is: init → check → body → step → check → …
- Choose bounds carefully (
<=vs<) to avoid off-by-one errors. - The loop counter declared in the
forheader is scoped to the loop.
Coming up: we’ll extend loops with break/continue, cover range-based loops, and show patterns for nested loops.










