C++ Reference ยท Keywords and Statements
The for loop
Reference for the classic C++ for loop: init, condition and step clauses, multiple variables, empty clauses, scope, off-by-one and unsigned traps.
The classic for loop packs a loop's setup, test and update into one header. It runs the init-statement once, then repeats: check the condition, run the body, run the step expression. The loop ends when the condition becomes false.
Use it when you need an index, a step other than one, or iterators. When you only need each element, a range-based for is shorter and safer.
for (int i = 0; i < 5; ++i) { // body runs for i = 0, 1, 2, 3, 4 }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
for (init-statement condition; expression) statement
| Part | Description |
|---|---|
init-statement |
Runs once before the first test: an expression statement, a declaration (which may declare several variables of the same base type) or just ;. Variables declared here are scoped to the loop. |
condition |
Tested before every iteration. May be an expression or a declaration with an initializer. If omitted, it counts as true. |
expression |
Evaluated after every iteration (including after continue), before the next test. May be empty. |
statement |
The body. Usually a braced block. |
Usage notes
continuejumps to the step expression;breakleaves the loop; the step is not run afterbreak.- Prefer
++ioveri++for iterators and class types: it can avoid a temporary copy. For built-in types the compiler generates identical code. - With an unsigned counter,
for (std::size_t i = n - 1; i >= 0; --i)never ends, because an unsigned value is never negative. Use a signed type or thei-- > 0idiom. - Comparing a signed
intwithvector::size()triggers a sign-compare warning. Usestd::size_t, orstd::ssizeand a signed index. - Changing the container's size while iterating with indexes or iterators can invalidate them. See the iterator rules of each container.
- Floating-point loop counters accumulate rounding error:
for (double x = 0; x != 1; x += 0.1)never reaches exactly 1. Loop over an integer and compute the double inside. - A loop that never ends and does no I/O, volatile access or atomic operation breaks the forward-progress guarantee, which makes the program undefined (the compiler may assume the loop ends). C++26 relaxes this for loops whose condition is a constant such as
for (;;); do not depend on it. - The variable declared in the header does not leak into the enclosing scope. Declare it before the loop if you need its final value.