Skip to content

Exercise 5 of 10 · Loops

Countdown

What you will make

A launch sequence that counts itself down from five, then prints a liftoff banner and a small rocket.

The one new idea: A for loop's own three parts, not a helper function, decide how it counts

Doing the same thing a set number of times is most of what computers are for — every row of a table, every name on a list, every frame of an animation. A for loop is the shortest way to ask for it, and in Java you write out exactly how it starts, how it keeps going, and how it moves.

Go straight to the code ↓

The three parts of a for loop

A countdown is one line said over and over with a different number in it each time. You could write five separate println lines by hand. A loop is how you write the line once and tell Java exactly how the number should change.

Java
for (int n = 0; n < 3; n++) {
    System.out.println(n);
}

Three parts, separated by semicolons, inside the round brackets:

  • Start: int n = 0 runs once, before the loop takes a single turn. It creates the counter and gives it its first value.
  • Condition: n < 3 is checked before every turn, including the first. As long as it is true, the loop takes another turn. The moment it is false, the loop stops — and the line that made it false never runs.
  • Update: n++ runs at the end of every turn, after the braced block. It is what actually moves the counter — nothing about n < 3 on its own makes n go anywhere.

Nothing here is hidden the way range() hides it in some languages. All three parts are decisions you write yourself, which is exactly why a Java for loop can count downwards as easily as up: start high, keep the condition true while there is still counting to do, and subtract on each turn instead of adding.

A worked example

Java
public class Main {
    public static void main(String[] args) {
        for (int step = 0; step < 3; step++) {
            System.out.println("step " + step);
        }
        System.out.println("done");
    }
}

Running that prints four lines:

Output
step 0
step 1
step 2
done

Three turns, so the println inside the braces ran three times, holding a different value in step each time — 0 first, 2 last, and the loop stopped the moment step < 3 became false, which is why 3 itself is never printed. The final line sits outside the loop's closing brace, so it runs once, after the counting is over.

Your turn

The editor holds a launch sequence that already works. Press Run before changing anything and you can see the problem for yourself: it counts 0, 1, 2 and then announces liftoff. No rocket has ever left the ground that way.

One line needs changing — the for line. The banners, the rocket and the println inside the loop are all finished; leave them alone.

Rewrite the three parts so the count reads 5, 4, 3, 2, 1 before handing over to the liftoff banner: start at 5, keep going while the count has not dropped below 1, and move the counter downwards by one on every turn.

If something goes wrong

The likeliest result is a run that never finishes. If the countdown starts correctly but keeps climbing past 5 instead of stopping, the update is still adding one — n++ where n-- belongs. A loop whose condition can never become false is stopped automatically after a few seconds, as a timeout, rather than running forever in your browser.

The other likely result is a countdown one number short at one end — starting at 4, or finishing on 2. Both come from the condition: n >= 1 keeps going through 1 itself, while n > 1 stops one turn earlier than that.

Nothing you type here can break anything. The worst case is a countdown that reads oddly, or a run that gets stopped for taking too long — and then you change a number and run it again.

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

Leaving the counter moving upwards, as in for (int n = 5; n >= 1; n++)
n++ always adds one, so once n starts at 5 and the condition asks for n >= 1, that condition is true forever — 5, 6, 7 and onward, never dropping to 1. The loop never finishes on its own, and the run is stopped as a timeout rather than reaching the liftoff banner.
Using n > 1 as the condition instead of n >= 1
The loop keeps going only while n is strictly greater than 1, so it stops the moment n reaches 1 — before that pass runs. The countdown ends on 2, and the liftoff banner arrives one beat early.
Starting the count at 4 instead of 5
The first value a for loop uses is whatever the starting part sets it to, with nothing added first. Start at 4 and the countdown reads 4, 3, 2, 1 — a real countdown, just one number short of the one asked for.
Forgetting the semicolons between the three parts of the loop
The three parts of a for loop are separated by semicolons, not commas, and javac needs all three to make sense of the line. Leaving one out is reported as ';' expected, pointing at the loop line itself.

Want a blank editor instead? Open the Java playground.