The C++ while loop, explained
Short answer: a while loop repeats its body as long as a condition is true, checking before every pass. It's the loop for "I don't know how many times yet" — everything else is a for in disguise.
A while loop has three moving parts, and only the first is built into the syntax:
- Condition — checked before each pass; false ends the loop.
- Body — the work.
- Update — something inside the body must move the world toward the condition becoming false. Forget this and you have an infinite loop.
Countdown: the minimal example
#include <iostream>
using namespace std;
int main() {
int n = 5;
while (n > 0) {
cout << n << " ";
n--; // the update — remove it and loop forever
}
cout << endl;
return 0;
}
Where while shines #1: chewing through digits
When you don't know how many digits a number has, a for is awkward and a while is natural. This one sums the digits of 12345 (the pattern behind our Sum of Digits practice problem):
#include <iostream>
using namespace std;
int main() {
int n = 12345;
int sum = 0;
while (n > 0) {
sum += n % 10; // last digit
n /= 10; // chop it off — see why 12345/10 is 1234 in our
// integer division guide
}
cout << sum << endl; // 15
return 0;
}
Where while shines #2: reading until a sentinel
Read numbers until the user types 0. The count is unknown — that's while territory (numbers go in the input box):
#include <iostream>
using namespace std;
int main() {
int x, sum = 0;
cin >> x;
while (x != 0) {
sum += x;
cin >> x; // read again INSIDE the loop
}
cout << "sum = " << sum << endl;
return 0;
}
A slicker variant works directly on the read itself — cin >> x evaluates to the stream, which converts to true while reads keep succeeding, so the loop ends at end-of-input or a broken read:
#include <iostream>
using namespace std;
int main() {
int x, sum = 0;
while (cin >> x) { // loop while reading succeeds
sum += x;
}
cout << "sum = " << sum << endl;
return 0;
}
Anatomy of an infinite loop
int n = 5;
while (n > 0) {
cout << n << " ";
// forgot n--; → condition never becomes false
}
Three classic causes: no update, an update that skips over the exit (n-- from 5 with condition n == 0), and a read that happens outside the loop. On this site, a runaway loop freezes the browser tab (the code runs on your device) — so double-check the update line before hitting Run. A break; statement is an emergency exit, but a loop you can only leave by breaking is usually a design smell.
Common beginner bugs
- Semicolon after the condition:
while (n > 0);— that stray;is an empty body; the loop either spins forever or does nothing. - Off-by-one in the condition:
>vs>=. Trace one iteration by hand with a tiny input. - Updating the wrong variable — chopping
copywhile testingn, or vice versa.
Interview questions on while
- "When do you choose while over for?" — when the iteration count is unknown in advance: sentinel reads, digit processing, waiting for a condition.
- "What does
while (cin >> x)mean?" — the stream converts to true while reads succeed; the loop ends at end-of-input or invalid input. - "Difference between while and do-while?" — do-while checks the condition after the body, so it always runs at least once (menus, input validation).
- "How would you debug an infinite loop?" — print the loop variables each pass; the variable that never moves is your missing update.
Practice it
These problems in our practice section are while-loop territory: Count the Digits, Sum of Digits, Prime Check. And the companion piece: for vs while, side by side.