Skip to content

Exercise 5 of 10 · Loops

Countdown

What you will make

A launch sequence that counts itself down from five to one, announces liftoff and draws a small rocket, with the whole countdown written as one line that runs five times.

The one new idea: a for loop repeats an action

Repeating work is most of what a computer is for: every row of a table, every message in a mailbox, every frame of an animation. A for loop is how C++ asks for that, and the three parts you write here are the same three parts in almost every language you meet afterwards.

Go straight to the code ↓

Writing a line once and running it many times

A countdown is one sentence said over and over with a different number in it. You could write five separate std::cout lines by hand, but five is the gentle case: a program printing a thousand rows cannot be typed out.

A for loop is how you write the line once and say how many times to run it.

C++
for (int n = 1; n <= 4; n++) {
    // the lines in here run four times
}

Everything the loop needs sits between the round brackets, in three parts separated by semicolons — never commas, which is the single easiest slip to make coming from a language that formats things differently. Read the three parts as three questions.

  • Where does it start? int n = 1 declares a counter called n, scoped to this loop alone, and sets it to 1 once, before the first pass.
  • How long does it keep going? n <= 4 is checked before every pass. True and the block runs again; false and the loop ends, handing control to whatever comes after the closing brace.
  • What changes each time? n++ runs at the end of every pass, after the block. Two plus signs mean add one; two minus signs mean take one away.

Because n is declared inside the parentheses, it exists only for the loop's own body — reaching for it after the closing brace would be reaching for a name the compiler has already forgotten.

A worked example

Seats rather than a rocket, so the countdown in the editor stays yours to write:

C++
#include <iostream>

int main() {
    for (int seat = 1; seat <= 3; seat++) {
        std::cout << "Seat " << seat << " ready" << std::endl;
    }
    std::cout << "All seats checked." << std::endl;
}
Output
Seat 1 ready
Seat 2 ready
Seat 3 ready
All seats checked.

Three passes, and seat holds a different number on each one without you ever setting it again. The last line sits outside the braces, so it runs once, after the loop has finished.

Your turn

The editor holds a launch sequence that already compiles and runs. Press Run before changing anything and the trouble shows itself: the count reads 0, 1, 2 and then announces liftoff. No rocket has ever left the ground that way.

One line needs rewriting, the one beginning with for:

C++
for (int i = 0; i <= 2; i++) {

The banners, the rocket and the line printing inside the braces are finished — leave them alone.

Rewrite the three parts so the count reads 5, 4, 3, 2, 1 and then hands over to the liftoff banner. Start at five, keep going while the counter has not yet dropped below one, and take one away each pass instead of adding one.

If something goes wrong

A countdown one number too long or too short is the likeliest result, and it is usually the middle part. The test is checked before each pass, so the last number printed is the last one for which it was still true. Let the test survive zero and a T minus 0 line sneaks in before the banner.

The next likeliest is a loop that runs for far longer than intended, which happens when the counter travels away from the finish rather than towards it. The page notices and stops the run, and no damage is done — the fix is to move the counter the way the test is waiting for.

If the compiler complains before anything runs at all, check the punctuation between the three parts: semicolons separate them, never commas.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Separating the three parts with commas instead of semicolons
The compiler stops before running a single line: error: expected ';' in 'for' statement specifier. Not even the banners appear, and that total silence is the tell that nothing built at all, rather than that something ran wrong.
Counting upwards while the test waits for the counter to come down
for (int i = 5; i >= 1; i++) moves the counter away from the finish instead of towards it, so the test never turns false. The page notices the loop has run far too long and stops it for you — nothing is damaged, but liftoff never arrives.
Letting the test stay true one number too long
A test that is still true at zero, such as i >= 0, prints a T minus 0 line before liftoff. The test is checked once before every pass, so the last number you see is the last one for which it was still true.
Declaring i outside the for line and expecting it to still work the same way
int i; for (i = 5; ...) compiles and runs identically inside this program, but i now exists after the loop ends too, taking up a name that was only ever meant to count. Declaring it inside the parentheses, as the starter code does, keeps it scoped to exactly where it is needed.

Want a blank editor instead? Open the C++ playground.