For Loops — The Counter Loop
A compact loop with init, condition, and update all in one line
Open interactive version (quiz + challenge)Real-world analogy
Imagine you are running laps around a track. Before you start, you reset your lap counter to 0 (initialization). Before each lap, you check: have I done all my required laps? (condition). After finishing a lap, you add 1 to your counter (update). A for loop puts all three steps in one neat line.
What is it?
A for loop is a compact loop structure that combines initialization, condition checking, and updating into a single line. It is the most commonly used loop in competitive programming because it neatly handles counted repetition — when you know exactly how many times to repeat.
Real-world relevance
For loops dominate competitive programming. Reading N numbers, processing each element of an array, generating patterns, computing sums, products, and powers — nearly every CP problem uses at least one for loop. Once you master for loops, you can solve hundreds of basic problems.
Key points
- The For Loop Syntax — A for loop has three parts in one line: for(init; condition; update) { body }. First the init runs once. Then before each repetition, the condition is checked. After each repetition, the update runs. It is a while loop with everything organized in one place.
- Breaking Down the Three Parts — init: usually declares and sets a counter like int i = 0. condition: checked before each iteration like i < n. update: runs after each iteration like i++. If the condition is false from the start, the body never executes.
- Counting Up — for(int i = 1; i <= 10; i++) counts from 1 to 10. The variable i starts at 1, goes up by 1 each time, and the loop stops when i exceeds 10. This is the most common loop pattern in CP.
- Counting Down — for(int i = 10; i >= 1; i--) counts from 10 down to 1. Use i-- to decrease by 1 each time. Useful for reversing arrays or counting backwards.
- Running Sum with For — To sum 1 to N: int sum = 0; for(int i = 1; i <= n; i++) sum += i; This is cleaner than a while loop because the counter setup is all in the for line. The variable i is only visible inside the loop.
- Factorial with For — Factorial of N (written N!) means multiply 1 × 2 × 3 × ... × N. Use: long long fact = 1; for(int i = 1; i <= n; i++) fact *= i; We use long long because factorials grow extremely fast — 20! is already over 2 quintillion.
- For vs While — When to Use Which — Use a for loop when you know how many times to repeat (count from 1 to N, process N items). Use a while loop when you do not know in advance (read until end of input, repeat until a condition changes). In CP, for loops are used about 80% of the time.
- Loop Variable Scope — When you declare a variable inside the for statement like for(int i = 0; ...), that variable only exists inside the loop. After the loop ends, i is gone. This prevents accidental reuse and keeps your code clean.
Code example
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
// Sum from 1 to n
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
cout << "Sum = " << sum << endl;
// Factorial of n
long long fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
cout << n << "! = " << fact << endl;
// Count down from n to 1
for (int i = n; i >= 1; i--) {
cout << i << " ";
}
cout << endl;
// Print even numbers from 2 to 20
for (int i = 2; i <= 20; i += 2) {
cout << i << " ";
}
cout << endl;
return 0;
}Line-by-line walkthrough
- 1. We read n from the user to know how many numbers to work with.
- 2. int sum = 0; — Start with a sum of zero.
- 3. for (int i = 1; i <= n; i++) — Start i at 1, keep going while i is at most n, add 1 to i after each round.
- 4. sum += i; — Each time through the loop, add the current i to sum.
- 5. After the loop, sum holds 1 + 2 + 3 + ... + n.
- 6. For factorial, we start fact at 1 (not 0, because we are multiplying).
- 7. fact *= i; means fact = fact * i. After the loop, fact = 1 × 2 × 3 × ... × n.
- 8. The countdown loop starts i at n and decreases: i-- subtracts 1 each time.
- 9. The even numbers loop uses i += 2 to skip by 2 each time, starting from 2.
- 10. All four examples show different ways to use the three parts of a for loop.
Spot the bug
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 5;
int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
cout << n << "! = " << fact << endl;
return 0;
}Need a hint?
This code looks correct for small values of n. But what happens if n is 25 or 30? What is the maximum value an int can hold? Think about overflow.
Show answer
The bug is using int for fact instead of long long. Factorials grow extremely fast — 13! = 6,227,020,800 which already overflows a 32-bit int (max ~2.1 billion). For n = 25 or higher, the result is completely wrong due to integer overflow. The fix: change int fact to long long fact.
Explain like I'm 5
A for loop is like a teacher telling you: 'Start at page 1, keep going while you have not passed page 10, and turn one page after each reading.' All the instructions for how to go through the book are written in one sentence at the top.
Fun fact
The famous mathematician Carl Friedrich Gauss supposedly figured out the sum of 1 to 100 in seconds as a child by noticing that 1+100 = 2+99 = 3+98 = ... = 101, giving 50 pairs of 101, so the answer is 5050. Your for loop does the same thing, just one step at a time!
Hands-on challenge
Write a program that reads a number N and prints the first N odd numbers on one line separated by spaces. For example, if N = 4, print: 1 3 5 7. Use a for loop. Hint: the k-th odd number is 2*k - 1.
More resources
- C++ For Loops for Beginners (YouTube)
- For Loop — cppreference (cppreference.com)
- Loop Problems — CSES (CSES Problem Set)