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

forwhiledo-while
Condition checkedBefore each passBefore each passAfter each pass
Minimum runs001
Best forKnown count, countersUnknown count, "until event"Menus, input validation

The classic bugs (memorize these, save hours)

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

  1. Print the 5-times table (1×5 to 10×5) with a for loop.
  2. Redo it with a while loop — same output, different header.
  3. Read numbers until a negative one appears, then print the average (careful: sum / count will bite you — see why).

Related: what int main() is, and the beginner interview question list.