Skip to content

Exercise 6 of 10 · Loops and conditions

The Clapping Game

What you will make

A count from one to twenty where every third number comes out as a clap instead, so the beat runs in a straight line down the screen.

The one new idea: An if nested inside a for loop is decided again on every pass through the loop

Looking at every item as it goes past and treating some of them differently is most of what software does: the overdue invoices, the weekends in a calendar, every hundredth order. All of it is a loop with a question inside, asked again on every single pass.

Go straight to the code ↓

A question asked on every turn

You already have both halves of this. A for loop repeats a block of lines; an if chooses between two outcomes. Nest the if inside the loop's braces and its question is no longer asked once — it is asked again for every number the loop counts through, and the answer may come out differently each time.

Twenty turns, twenty decisions, one test.

Java
for (int count = 1; count <= 20; count++) {
    if (count == 3) {
        System.out.println("clap");
    } else {
        System.out.println(count);
    }
}
System.out.println("finished");

Each pair of braces marks off the lines that belong to whatever sits in front of it. The loop's braces hold the whole if, so the question is asked once per number — twenty times over. The if braces and the else braces each hold one println, and only one of the two runs per turn. The last line sits after the loop's closing brace, outside everything, so it runs once, when the counting is over.

Catching every third one, not just the third one

count == 3 can only be true on one turn out of twenty. To catch 3, 6, 9, 12 and the rest, you need something all of them share: divide any one of them by three and nothing is left over.

% reports that leftover — the operator that told you how many pizza slices spilled past a whole pizza. 7 % 3 is 1, because two threes fit inside seven with one left behind. 9 % 3 is 0, because three threes fit exactly. A leftover of zero is the mark of every third number, so the test measures that leftover against 0 with ==.

A worked example

A row of lockers rather than a clapping game, so the rhythm in the editor stays yours to find:

Java
public class Main {
    public static void main(String[] args) {
        for (int box = 1; box <= 6; box++) {
            if (box % 4 == 0) {
                System.out.println("Box " + box + " has the spare key");
            } else {
                System.out.println("Box " + box);
            }
        }
        System.out.println("Row checked.");
    }
}
Output
Box 1
Box 2
Box 3
Box 4 has the spare key
Box 5
Box 6
Row checked.

Six numbers, six decisions. Only box 4 leaves nothing over when divided by four, so only box 4 takes the first branch. The last line sits outside the loop's braces, which is why it appears once at the bottom rather than after every box.

Your turn

Press Run before changing anything. The count from 1 to 20 arrives neatly, one clap lands at 3, and after that it is numbers all the way, because the test reads:

Java
if (count == 3) {

Replace it with a test that is true for every third count: reach for %, and measure the leftover against 0. The loop, the printing lines, the braces and the banners are finished — the test inside the round brackets is the only thing to touch.

When it works, the claps land on 3, 6, 9, 12, 15 and 18, and the beat is readable from across the room.

If something goes wrong

The likeliest slip is the rhythm itself. If the claps come out on 1, 4, 7 and 10, the leftover is being measured against the wrong number — leftovers after threes run 1, 2, 0, 1, 2, 0, and it is the 0 that marks every third count.

If the output panel is completely empty, not even the top banner, a brace has gone missing somewhere in the edit. javac reads the whole program before running any of it, so an unclosed block stops the lot and reports reached end of file while parsing.

And a message about an unexpected type where a boolean was needed means the test was written as an assignment — one equals sign where two belong. None of this breaks anything: the program either runs with an odd rhythm or refuses to start, and the fix is one line away either way.

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

Losing one of the closing braces while editing the line
javac reads the whole file before running any of it, so an unclosed block stops everything rather than part of it. The message is reached end of file while parsing, and the output panel stays completely empty — not even the top banner appears.
Writing the test with one equals sign instead of two
count % 3 = 0 tries to store 0 into the result of count % 3, which is not a variable Java can assign into at all. javac refuses this before anything runs, reporting unexpected type — a comparison needs ==, and an assignment needs something on the left it can actually store into.
Comparing the leftover with 1 rather than 0
The leftover after dividing by three is 1 for 1, 4, 7 and 10 — each one beat after a multiple of three — so every clap lands one beat late and every third number in the intended sense prints as an ordinary count instead. The rhythm stays perfectly regular, which is what makes this slip easy to read straight past.
Keeping the test that measures the count against 3
count == 3 is asked again on all twenty turns, but it can only be true on the single turn where count happens to hold exactly 3. That gives one clap near the top of the count and plain numbers all the way to the end.

Want a blank editor instead? Open the Java playground.