Beginner⏱️ 9 min📘 Topic 9 of 32

🔁 C++ Loops — for, while and do-while Explained with Examples

Master C++ loops — for, while, do-while and range-based for. Learn break, continue, infinite loops, nested loops and common pitfalls — with examples and interview Q&A.

Loops execute a block repeatedly. C++ has three classic forms plus a modern range-based one.

🔁 for — known number of iterations

for (int i = 0; i < 5; i++) {
  std::cout << i << ' ';
}
// 0 1 2 3 4

🔂 while — repeat while condition is true

int n = 5;
while (n > 0) {
  std::cout << n--;
}

🔃 do-while — run at least once

int x;
do {
  std::cout << "Enter positive: ";
  std::cin >> x;
} while (x <= 0);

🆕 Range-based for (C++11) — best for collections

std::vector<int> nums = {10, 20, 30};
for (int n : nums) std::cout << n;

🛑 break and continue

  • break — exit the loop immediately
  • continue — skip to the next iteration

💻 Code Examples

Sum of 1 to 100

int sum = 0;
for (int i = 1; i <= 100; i++) {
  sum += i;
}
std::cout << sum;
Output:
5050

Skip even numbers, stop at 50

for (int i = 0; ; i++) {
  if (i > 50) break;
  if (i % 2 == 0) continue;
  std::cout << i << ' ';
}
Output:
1 3 5 7 ... 49

Nested loops — multiplication table

for (int i = 1; i <= 3; i++) {
  for (int j = 1; j <= 3; j++) {
    std::cout << i*j << '\t';
  }
  std::cout << '\n';
}
Output:
1  2  3
2  4  6
3  6  9

⚠️ Common Mistakes

  • Off-by-one errors: `i <= n` when you meant `i < n` (or vice versa).
  • Modifying the loop counter inside the body — confusing, prefer a clear condition.
  • Forgetting to update the condition in a while loop — infinite loop.
  • Using `int` for loop counter when iterating a container — use `size_t` or auto.

🎯 Interview Questions

Real questions asked at top product and service-based companies.

Q1.What's the difference between while and do-while?Beginner
while checks the condition BEFORE the body — may skip entirely. do-while checks AFTER — guarantees the body runs at least once. Use do-while for menus or retry-input patterns.
Q2.What's an infinite loop and how do you exit it?Beginner
A loop whose condition never becomes false: `while (true) { ... }`. Exit with `break`, `return`, or `exit()`. Useful for event loops, servers, REPLs.
Q3.What does `continue` do inside a loop?Intermediate
Skips the rest of the current iteration and jumps to the loop's update step (for) or condition check (while). The loop continues with the next iteration.
Q4.What is a range-based for loop?Intermediate
C++11 feature: `for (auto x : container)` iterates over every element. Pass `&` for reference (modify in place), `const&` to avoid copies, no `&` for a value copy.
Q5.Why use `++i` instead of `i++` as a loop step?Advanced
For primitive ints both are identical. For complex types (iterators, custom classes), `++i` is potentially faster because `i++` makes a temporary copy. Habit of using `++i` keeps code consistent.

🧠 Quick Summary

  • for: known count. while: condition first. do-while: body first.
  • Range-based for is the cleanest way to iterate containers.
  • break exits the loop; continue skips to the next iteration.
  • Beware off-by-one and infinite loops.
  • Prefer `++i` for iterators in for-loop steps.