C++ for vs while loop: which one, when?
Short answer: use for when you know how many times to loop (or you're walking a counter); use while when you loop until something happens and can't know in advance. Both can technically do both jobs — the choice is about readability.
The for loop: counting things
A for header has three parts — start; keep-going condition; step — which makes "do this 10 times" a single glance:
#include <iostream>
using namespace std;
int main() {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
cout << "1 + 2 + ... + 10 = " << sum << endl;
return 0;
}
Reading it out loud: start with i = 1; keep looping while i <= 10; after each pass, do i++. The variable i exists only inside the loop — trying to use it after the loop is a compile error, which is a good thing.
The while loop: until something happens
A while only has a condition. Classic use: keep reading input until you hit a stopping value (a "sentinel"). Put numbers in the input box — the 0 stops the loop:
#include <iostream>
using namespace std;
int main() {
int x, sum = 0;
cin >> x;
while (x != 0) {
sum += x;
cin >> x;
}
cout << "sum = " << sum << endl;
return 0;
}
Notice the structure: read once before the loop, read again at the bottom. A very common beginner bug is forgetting that second read, which turns the loop infinite.
do-while: run at least once
The condition is checked after the body, so the body always runs at least once — good for menus and "ask until valid input":
#include <iostream>
using namespace std;
int main() {
int n = 3;
do {
cout << "countdown: " << n << endl;
n--;
} while (n > 0);
cout << "liftoff" << endl;
return 0;
}
break and continue
break exits the loop immediately; continue skips the rest of this pass and moves to the next one:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) continue; // skip even numbers
if (i > 7) break; // stop once past 7
cout << i << " ";
}
cout << endl;
return 0;
}
Side-by-side
| for | while | do-while | |
|---|---|---|---|
| Condition checked | Before each pass | Before each pass | After each pass |
| Minimum runs | 0 | 0 | 1 |
| Best for | Known count, counters | Unknown count, "until event" | Menus, input validation |
The classic bugs (memorize these, save hours)
- Semicolon after the header:
for (int i = 0; i < 10; i++);— that stray;is an empty body, so the loop runs ten times doing nothing. Same trap withwhile (...);. - Off-by-one:
i <= 10runs 10 times for 1..10;i < 10runs 9. Neither is "wrong" — but pick deliberately, and check the boundary when debugging. - Infinite loop: forgetting
i++, or updating the variable inside but never re-reading input. Warning specific to this site: our playground runs in your browser, so a runaway loop will freeze the tab until you close it — double-check the step part before hitting Run. - Modifying the counter inside a for loop — legal but confusing; if the body changes
i, the loop is lying to its reader.
Interview classic: FizzBuzz
Asked constantly in junior screens — it's really a test of loops, %, and if/else order. Rules: multiples of 3 print "Fizz", of 5 print "Buzz", of both print "FizzBuzz", otherwise the number:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 15; i++) {
if (i % 15 == 0) cout << "FizzBuzz";
else if (i % 3 == 0) cout << "Fizz";
else if (i % 5 == 0) cout << "Buzz";
else cout << i;
cout << " ";
}
cout << endl;
return 0;
}
The trap: checking i % 3 first makes 15 print "Fizz" and never reach "FizzBuzz" — order matters. And if you're shaky on why 15 % 3 is 0 or what % does with negatives, that's our integer division guide.
Practice
- Print the 5-times table (1×5 to 10×5) with a
forloop. - Redo it with a
whileloop — same output, different header. - Read numbers until a negative one appears, then print the average (careful:
sum / countwill bite you — see why).
Related: what int main() is, and the beginner interview question list.