Exercise 3 of 10 · Loops
Ferris Wheel Car Numbering
What you will make
A program that prints a numbered list of ferris wheel cars along with how many seats each one has.
The one new idea: Repeating a block a fixed number of times with a for loop
Loops let a program repeat work without copy-pasting the same line over and over — essential once you have more than a couple of similar items to handle.
Go straight to the code ↓Once a program needs to repeat the same action several times, retyping the same line over and over gets old fast — and it doesn't scale if the number of repeats can change. A for loop handles this with three parts: a starting point (int car = 1), a continue condition checked before every iteration (car <= totalCars), and a step that runs after each iteration (car++). As long as the condition is true, the loop body runs again.
Worked example
Here's a warehouse aisle printing its shelf numbers:
int shelfCount = 4;
Console.WriteLine("Aisle 7 Shelves");
for (int shelf = 1; shelf <= shelfCount; shelf++)
{
Console.WriteLine($"Shelf {shelf}");
}Aisle 7 Shelves
Shelf 1
Shelf 2
Shelf 3
Shelf 4With shelfCount set to 4 and the condition shelf <= shelfCount, the loop runs for shelf values 1, 2, 3, and 4 — four times total, ending right after it prints the last one.
Your turn
The starter code numbers the cars on a ferris wheel from 1 up to totalCars, which is set to 5. But its loop condition is car < totalCars, which stops one car too early — the loop only ever reaches car 4, and car 5 never gets printed. Fix the condition so the loop includes the last car too.
If something goes wrong
If your output is missing the very last item a loop should produce, check whether you're using < where you meant <= (or the reverse) — this "off-by-one" mistake is one of the most common loop bugs there is. If instead you see one extra, unwanted line, you likely went the other direction. Either way, it helps to write out by hand what value the loop variable holds on its very first and very last pass through the body.
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
- Using < where <= was needed (or the reverse), leaving the loop one short or one too many.
- The loop's continue condition is checked before every pass, so off-by-one differences directly change how many times the body runs.
- Forgetting that car++ happens after the loop body, not before it.
- This affects what value car holds on the first and last passes, and it's easy to mentally place the increment at the wrong point.
- Reusing a loop variable name across separate loops and expecting it to carry over its old value.
- Each for loop's variable is freshly declared and scoped to that loop, so it always restarts from its own initializer.
Want a blank editor instead? Open the C# playground.