Loops

C++ gives you three loop forms — for, while, and do/while — plus a fourth, the range-based for, purpose-built for stepping through a collection. Which one you reach for usually comes down to one question: do you know in advance how many times you need to repeat, or are you repeating until some condition changes?

for: when you know the count

A for loop packs its setup, its continue-condition, and its per-iteration step into one line, which makes it the natural choice whenever you're counting through a known range:

</> countdown.cpp
#include <iostream>

int main() {
    for (int i = 5; i >= 1; i--) {
        std::cout << i << "..." << std::endl;
    }
    std::cout << "Liftoff!" << std::endl;
    return 0;
}
Terminal output
5...
4...
3...
2...
1...
Liftoff!

int i = 5 runs once, before the loop starts. i >= 1 is checked before every iteration, including the first — as soon as it's false the loop ends without running its body again. i-- runs after each iteration's body finishes. Notice that i is declared inside the parentheses, which means it doesn't exist anymore once the loop is done — that's usually exactly what you want, since a loop counter rarely needs to outlive the loop.

while: when you're watching a condition

A while loop has no built-in counter — it just re-checks its condition before every pass and keeps going as long as it holds. That makes it the right tool when you don't know the number of iterations ahead of time:

</> stock-depletion.cpp
#include <iostream>

int main() {
    int stock = 100;
    int day = 0;

    while (stock > 0) {
        stock -= 30;
        day++;
    }
    std::cout << "Stock ran out after day " << day << std::endl;
    return 0;
}
Terminal output
Stock ran out after day 4

Stock goes 100 → 70 → 40 → 10 → -20, crossing zero on the fourth day, at which point stock > 0 finally fails and the loop stops. You didn't know that number in advance — it fell out of the simulation — which is exactly the situation while is for.

do/while: when the body must run at least once

do/while checks its condition after the body runs instead of before, guaranteeing at least one execution regardless of whether the condition would ever have been true. This matters for things like reading input or displaying a menu, where you need to do the work once before you have anything to check:

</> do-while.cpp
#include <iostream>

int main() {
    int n = 10;

    do {
        std::cout << "n is " << n << ", but this still runs once." << std::endl;
    } while (n < 5);

    return 0;
}
Terminal output
n is 10, but this still runs once.

n < 5 is false from the start, so a regular while loop with the same condition would never run its body at all. do/while runs it exactly once anyway, then checks — and since the condition is still false, it stops there.

Range-based for, break, and continue

C++11 added a simpler form of for for walking through every element of a collection without managing an index at all. Inside any loop, break exits it immediately, and continue skips straight to the next iteration:

</> scores.cpp
#include <iostream>

int main() {
    int scores[] = {72, 88, -1, 95, 60};

    for (int s : scores) {
        if (s == -1) {
            break;       // -1 marks the end of real data
        }
        if (s < 75) {
            continue;    // skip scores below the passing mark
        }
        std::cout << "Passing score: " << s << std::endl;
    }
    return 0;
}
Terminal output
Passing score: 88

The loop reads 72 first — below 75, so continue skips straight past it without printing. 88 clears the bar and gets printed. Then it hits -1, the sentinel value meaning "no more real scores," and break stops the loop before 95 and 60 are ever examined, even though 95 would otherwise have passed. for (int s : scores) reads naturally as "for each s in scores" — you'll see this same syntax used with std::vector and std::string in the next couple of lessons.

Note: every loop needs some way to eventually stop — a counter that reaches its limit, a condition that becomes false, or a break. A while loop whose condition never changes is an infinite loop, and unlike a webpage freezing in a browser tab, an infinite loop in a compiled program will pin a CPU core at 100% until you kill the process.