Module 2 · Variables, Operators and Control Flow — topic 4 of 4
Loops
while, do-while, the counting for loop and the range-based for loop, plus break and continue and how to avoid off-by-one errors.
Learning objectives
- Choose between while, do-while, for and range-for
- Iterate containers safely with range-for
- Use break and continue correctly
Definition
Iteration statements repeat a statement while a condition holds. The range-based for loop (C++11) iterates over every element of an array, container or initializer list.
Introduction
C++ has C's three loops plus the range-based for, which removes index arithmetic entirely when you just need each element.
The loop variable of a range-for can be a copy, a reference (to modify elements) or a const reference (to read large elements without copying).
Why it matters
Most bugs in array processing are index mistakes; range-for eliminates them.
Loops are where programs spend their time; knowing which loop fits each task makes code clearer.
Detailed explanation
while (cond) tests first; do { } while (cond); runs at least once; for (init; cond; step) gathers the three parts of a counting loop.
for (auto x : v) copies each element; for (auto& x : v) lets you modify elements; for (const auto& x : v) reads without copying.
break leaves the innermost loop; continue jumps to the next iteration.
Use std::size_t (or range-for) when comparing with size() to avoid signed/unsigned warnings.
Syntax
i < nfrom 0 gives exactly n iterations.- Use
auto&to modify container elements. - Reading in the condition stops at end of input.
Code examples
Range-for to modify elements
10 20 30 40
Sum until input ends
4 8 15 16 23 42
6 numbers, sum 108
More examples
Primes with nested loops, break and continue
2 3 5 7 11 13 17 19 23 29
Step-by-step explanation
- The outer loop keeps testing numbers until ten primes are collected.
- The inner loop tries divisors up to √n and breaks at the first one.
continueskips non-primes.- The range-for prints the vector.
Expected output
Running “Primes with nested loops, break and continue” prints exactly:
Practical applications
- Servers loop reading requests until shutdown.
- Data processing iterates containers with range-for.
- Simulations loop over time steps.
Best practices
- Prefer range-for when you don't need the index.
- Use
const auto&for large elements. - Keep loop bodies short; move work into functions.
Common mistakes
Copying in range-for when you meant to modify
Avoid
Prefer
Without &, x is a copy and the vector is unchanged.
Modifying a vector while iterating it
Avoid
Prefer
push_back can reallocate and invalidate the iterators the loop uses — undefined behaviour.
Important notes
- C++20 ranges allow loops like
for (int i : std::views::iota(0, 10)). - An infinite loop without observable side effects can be assumed to terminate by the compiler; always make progress.
Key takeaways
- Range-for iterates every element; use
&to modify. - do-while runs at least once.
- Don't change a container's size while range-iterating it.
Try it yourself
- 1Print the multiplication table of n using a for loop.
- 2Read words until end of input and print the longest one.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
7 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat is printed?
- BeginnerWhat is printed?