Lesson 12 of 50 beginner

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

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. 1. We read n from the user to know how many numbers to work with.
  2. 2. int sum = 0; — Start with a sum of zero.
  3. 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. 4. sum += i; — Each time through the loop, add the current i to sum.
  5. 5. After the loop, sum holds 1 + 2 + 3 + ... + n.
  6. 6. For factorial, we start fact at 1 (not 0, because we are multiplying).
  7. 7. fact *= i; means fact = fact * i. After the loop, fact = 1 × 2 × 3 × ... × n.
  8. 8. The countdown loop starts i at n and decreases: i-- subtracts 1 each time.
  9. 9. The even numbers loop uses i += 2 to skip by 2 each time, starting from 2.
  10. 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

Open interactive version (quiz + challenge) ← Back to course: CP Zero to Hero