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 is replaced by a clap, so the beat is visible straight down the screen.

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

Looking at each thing as it goes past and treating some of them differently is most of what software does — every tenth customer, the overdue rows, the weekends in a calendar. It is a loop with a question inside it.

Go straight to the code ↓

A question asked on every pass

You already have both halves of this. A for loop repeats a block. An if picks between two blocks. Put the if inside the loop's braces and the question is no longer asked once — it is asked again for every number the loop counts up to, and the answer can come out differently each time.

Think of an inspector walking down a train carriage. Passing every seat is the loop; glancing at the ticket is the question. Most people are left alone, but the glance happens at every seat without exception.

Braces are what put it inside

Until now the loop's body was a single printf call. Here it is a whole if/else, and that block in turn has two bodies of its own.

C
for (int count = 1; count <= 20; count++) {
    if (count == 3) {
        printf("clap\n");
    } else {
        printf("%d\n", count);
    }
}
printf("finished\n");

Read it by which braces a line sits inside:

  • Inside the for's { } means runs every pass — twenty times here.
  • Inside the if's or else's { } means runs only when that branch is chosen, and only on the passes where it is chosen.
  • Outside every brace means runs once, after the loop has finished entirely. printf("finished\n"); is not part of the loop at all.

Indentation is there for you to read, not for the compiler to obey — but keeping it honest, as the examples here do, is what makes the braces easy to check by eye.

Every third, not the third one

count == 3 is true on exactly one pass. To catch 3, 6, 9, 12 and the rest you need something they all share: divide any of them by 3 and nothing is left over.

% from the party planner measures that leftover. 7 % 3 is 1, because two threes fit into seven and one is left behind. 9 % 3 is 0, because three threes fit exactly. A leftover of zero is the mark of every third number, so asking whether the leftover equals zero is the test you want.

A worked example

A row of seats, not a clapping game, so the answer to this exercise stays yours to write:

C
#include <stdio.h>

int main(void) {
    for (int seat = 1; seat <= 4; seat++) {
        if (seat % 2 == 0) {
            printf("%d aisle\n", seat);
        } else {
            printf("%d window\n", seat);
        }
    }
    printf("all seated\n");
    return 0;
}

Running that prints five lines:

Output
1 window
2 aisle
3 window
4 aisle
all seated

Four numbers, four decisions. Seats 2 and 4 leave nothing over when divided by two, so they take the first branch; 1 and 3 take the second. The last line sits outside every brace, so it appears once at the bottom rather than after every seat.

Your turn

Press Run before changing anything. The count from 1 to 20 prints fine, but there is a single clap at 3 and then nothing but numbers, because the test currently reads:

C
if (count == 3) {

Replace it with a test that is true for every third count: use % and compare the leftover with 0. The loop, the two printf calls and the banners are finished — that one test is the only thing to touch.

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

If something goes wrong

If the claps come out on 1, 4, 7 and 10 instead of 3, 6, 9 and 12, the test is comparing the leftover with the wrong number. Leftovers after threes go 1, 2, 0, 1, 2, 0, and it is the 0 that marks every third count.

If the compiler stops on the if line complaining that the left-hand side of an assignment is not assignable, count the equals signs: one stores a value, two ask a question, and only the question belongs inside an if's parentheses.

If the claps vanish entirely and only numbers print, check that the if is still inside the for loop's own braces — one misplaced } is enough to move it outside, where count no longer changes from pass to pass the way you expect.

None of these break anything. The program either runs with an odd rhythm or refuses to compile, and either way the fix is a single line away.

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 if outside the for loop's braces
An if written after the loop's closing brace instead of inside it only ever sees count holding whatever it was left with after the loop ended, so the question is asked once instead of twenty times. Braces, not indentation, decide what is inside a C loop — indentation here is only for readers.
Writing the test as count % 3 == 1
The leftover is 1 for 1, 4, 7 and 10, so the claps land one beat early and every real multiple of three prints as an ordinary number instead. The rhythm still looks regular, which is what makes this one easy to miss.
Keeping count == 3 and expecting it to fire again
The question is asked on all twenty passes, but count only ever equals exactly 3 on one of them. You get a single clap near the top and plain numbers for the rest of the round.
Writing if (count % 3 = 0) with one equals sign
That line assigns 0 to the result of count % 3, which is not a place a value can be stored — the compiler rejects it outright with an error about the left-hand side not being assignable, rather than letting a silent bug through.

Longer explanation: read the full lesson. Want a blank editor instead? Open the C playground.