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 a block a fixed number of times
Repeating work is most of what a computer is for: every row of a table, every message queued for sending, 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 you will meet in almost every language 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
printf calls 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.
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. Read them as three questions.
- Where does it start?
int n = 1declares a counter calledn, local to the loop, and puts 1 in it once, before the first pass. - How long does it keep going?
n <= 4is checked before every pass. True and the block runs again; false and the loop ends, and the program carries on with whatever comes after the closing brace. - What changes each time?
n++runs at the end of every pass. Two plus signs mean add one; two minus signs,n--, mean take one away.
A worked example
Seats rather than a rocket, so the countdown stays yours to write:
#include <stdio.h>
int main(void) {
for (int seat = 1; seat <= 3; seat++) {
printf("Seat %d ready\n", seat);
}
printf("All seats checked.\n");
return 0;
}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 by hand. 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:
for (int i = 0; i <= 2; i++) {The banners, the rocket and the printf between 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 subtract one 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
condition is checked before each pass, so the last number printed is the last one for which it was still
true. Let it survive zero and a T minus 0 line sneaks in before the banner. If the count starts on the
wrong number instead, it is the first part rather than the condition.
The next likeliest mistake is a loop that never finishes, which happens when the counter moves away from the finish rather than towards it. This site notices and stops a run that takes too long, and reports it as a timeout rather than letting your browser tab hang.
And if nothing appears between the two banners, the loop took no passes at all: the condition was already false on the very first check. Read the three parts left to right as start, keep going, change, then run it again.
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
- Separating the three parts with commas instead of semicolons
- The compiler reports a syntax error right there, because a for header needs exactly two semicolons marking the three parts — commas mean something different inside an expression and do not divide the header at all.
- Counting upwards while the condition 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 i >= 1 stays true forever. This site stops a run that goes on too long, but on your own machine a loop like this would never end on its own.
- Letting the condition stay true one number too long
- A condition that is still true at zero prints a T minus 0 line before liftoff. The condition is checked once before every pass, so the last number you see is the last one for which it was still true.
- Declaring int i outside the for line and then reusing i later expecting it to still be 5
- A counter declared inside the parentheses, int i = 5, exists only for the loop and stops existing the moment the loop ends — trying to print i afterwards is an undeclared identifier. Keeping the counter local like this, valid since C99, is the usual style precisely because it cannot collide with anything outside the loop.
Longer explanation: read the full lesson. Want a blank editor instead? Open the C playground.