TExcelTAD
Home › C++ › C++ For Loop

C++ For Loop

When you know how many times something should repeat — or want to walk through a range of numbers — the for loop is the right tool. It puts the three parts of a counting loop on one line.

Syntax

cpp
for (initialisation; condition; update) {
    // body
}
  1. Initialisation runs once, before the loop starts (usually declares a counter).
  2. Condition is checked before every iteration; the loop stops when it is false.
  3. Update runs after every iteration (usually i++).
cpp · try it yourself
#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 5; i++) {
        cout << "Iteration " << i << "\n";
    }
}
Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 4

The counter i exists only inside the loop. Starting at 0 and using < is the C++ convention, because arrays and strings are indexed from 0.

Counting in other steps

The update can be any expression — count backwards, or in steps of more than one:

cpp · try it yourself
#include <iostream>
using namespace std;

int main() {
    for (int i = 10; i >= 0; i -= 2) {
        cout << i << " ";
    }
    cout << "\n";

    int sum = 0;
    for (int n = 1; n <= 100; n++) {
        sum += n;
    }
    cout << "1 + 2 + ... + 100 = " << sum << "\n";
}
10 8 6 4 2 0
1 + 2 + ... + 100 = 5050

Nested loops

A loop inside a loop runs the inner loop completely for every step of the outer loop. This is how you work with grids and tables:

cpp · try it yourself
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    for (int row = 1; row <= 4; row++) {
        for (int col = 1; col <= 4; col++) {
            cout << setw(4) << row * col;
        }
        cout << "\n";
    }
}
   1   2   3   4
   2   4   6   8
   3   6   9  12
   4   8  12  16

And a small triangle pattern, where the inner loop's limit depends on the outer counter:

cpp · try it yourself
#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 4; i++) {
        for (int j = 0; j < i; j++) {
            cout << '*';
        }
        cout << "\n";
    }
}
*
**
***
****

The range-based for loop

To visit every element of an array, string or container, C++11 offers a simpler form: for (type element : collection). No counter, no off-by-one mistakes:

cpp · try it yourself
#include <iostream>
#include <string>
using namespace std;

int main() {
    int primes[] = {2, 3, 5, 7, 11};
    for (int p : primes) {
        cout << p << " ";
    }
    cout << "\n";

    string word = "loop";
    for (char c : word) {
        cout << "[" << c << "]";
    }
    cout << "\n";
}
2 3 5 7 11
[l][o][o][p]

Tip: Use the range-based loop whenever you just need each element. Use the classic for when you need the index, want to skip elements, or go backwards.

Test yourself

How many times does for (int i = 0; i < 5; i++) run?

  1. 4
  2. 5
  3. 6
  4. Forever

When is the update part (e.g. i++) of a for loop executed?

  1. Before the first iteration
  2. After each iteration
  3. Only once at the end
  4. Before the condition on the first pass

Which loop prints every element of int a[] = {1,2,3} most simply?

  1. for (int x : a)
  2. for (a : int x)
  3. foreach (int x in a)
  4. for x in a