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, 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 lines. An if picks between two outcomes. 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 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 passengers are left alone, but the glance happens at every seat without exception.
for (int count = 1; count <= 20; count++) {
if (count == 3) {
std::cout << "clap" << std::endl;
} else {
std::cout << count << std::endl;
}
}Read the nesting by the braces. The outer pair belongs to for, so everything inside — including the whole
if/else — runs once per pass, twenty times in all. The inner pairs belong to if and else, so only one
of those two lines runs on any given pass, chosen fresh each time.
Every third number, not the third one
count == 3 is true on exactly one pass, because == compares against one fixed value. To catch 3, 6, 9, 12
and the rest you need something all of them share: divide any of them by 3 and nothing is left over.
% from the pizza order measures exactly that. 7 % 3 is 1, because two threes fit into 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 count % 3 == 0 is the test you want — read it as "the remainder after dividing by three is
zero."
A worked example
A row of seats, not a clapping game, so the answer to this exercise stays yours to write:
#include <iostream>
int main() {
for (int seat = 1; seat <= 4; seat++) {
if (seat % 2 == 0) {
std::cout << seat << " aisle" << std::endl;
} else {
std::cout << seat << " window" << std::endl;
}
}
std::cout << "all seated" << std::endl;
}1 window
2 aisle
3 window
4 aisle
all seatedFour passes, 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 the loop's braces entirely, so it prints 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:
if (count == 3) {Replace it with a test that is true for every third count: use % and compare the remainder with 0. The
loop, the two std::cout lines, the braces and the banners are all 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 land on 1, 4, 7 and 10 instead, the test is comparing the remainder with the wrong number. Remainders after dividing by three go 1, 2, 0, 1, 2, 0, and it is the 0 that marks every third count.
If nothing prints at all and the compiler stops first, check every if and for line ends in { with a
matching } somewhere below it — a missing brace on a nested block is the usual cause, and the error message
will point at roughly where the mismatch was noticed rather than exactly where the brace went missing.
Nothing here can break. The program either compiles with an odd rhythm or refuses to build, and either way the fix is one line away.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
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
- 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 multiple of three prints as an ordinary number instead. The rhythm still looks regular, which is what makes this one easy to miss just by glancing at the output.
- Keeping count == 3 and expecting it to repeat
- 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.
- Dropping the braces around the if or else block
- C++ only attaches the single line right after if (...) to the branch when the braces are missing. With two std::cout lines meant to sit inside the loop as a whole and one branch inside that, losing a pair of braces here changes which lines belong to which decision without the compiler complaining at all.
- Writing count = 3 instead of count == 3 in the original test, by habit
- count = 3 inside the if would compile, silently set count to 3 on every pass, and restart the loop from a number that never grows — a loop that runs forever rather than twenty times. The page would stop it for you, but the real bug is the single equals sign, not the loop.
Want a blank editor instead? Open the C++ playground.