FizzBuzz is one of the first exercises many developers learn. The rules are simple:
- Print numbers from 1 to 100
- If the number is divisible by 3, print “Fizz”
- If the number is divisible by 5, print “Buzz”
- If the number is divisible by both 3 and 5, print “FizzBuzz”
It looks straightforward, but the way you structure the conditions makes a difference. Let’s look at two approaches: using if...else if and using continue.
What does divisible really mean?
When we say “divisible,” we mean the number divides evenly without leaving a remainder. In C#, we use the modulo operator % to check for remainders.
For example:
6 % 3 == 0 // true, because 6 divided by 3 has no remainder
10 % 5 == 0 // true, because 10 divided by 5 has no remainder
7 % 3 == 1 // false, remainder is 1
So when you see if (i % 3 == 0), it means “if the number can be divided by 3 with no remainder.”
The if…else if approach
The most common way is to chain conditions together.
for (int i = 1; i <= 100; i++)
{
if (i % 3 == 0 && i % 5 == 0)
Console.WriteLine("FizzBuzz");
else if (i % 3 == 0)
Console.WriteLine("Fizz");
else if (i % 5 == 0)
Console.WriteLine("Buzz");
else
Console.WriteLine(i);
}
Here, the else if structure ensures that only one condition runs for each number.
Why check for both first?
If we wrote the code like this instead:
if (i % 3 == 0)
Console.WriteLine("Fizz");
else if (i % 5 == 0)
Console.WriteLine("Buzz");
else if (i % 3 == 0 && i % 5 == 0)
Console.WriteLine("FizzBuzz");
Then a number like 15 would match the first condition (i % 3 == 0) and stop there. It would print “Fizz” and never reach “FizzBuzz.” That is why we check for the most specific condition first.
Think of it like a decision tree:
Is the number divisible by 3 and 5 (no remainder)?
Yes -> FizzBuzz
No ->
Is it divisible by 3 (no remainder)?
Yes -> Fizz
No ->
Is it divisible by 5 (no remainder)?
Yes -> Buzz
No -> Just print the number
The continue approach
Another way is to write each condition separately and use continue to move on to the next number once a match is found.
for (int i = 1; i <= 100; i++)
{
if (i % 3 == 0 && i % 5 == 0)
{
Console.WriteLine("FizzBuzz");
continue;
}
if (i % 3 == 0)
{
Console.WriteLine("Fizz");
continue;
}
if (i % 5 == 0)
{
Console.WriteLine("Buzz");
continue;
}
Console.WriteLine(i);
}
In this version, each condition is independent. Once one is true, the continue keyword skips the rest and goes straight to the next loop iteration.
Which style is better?
Both approaches work.
- if...else if is a bit cleaner when conditions are mutually exclusive.
- continue keeps conditions separate and avoids nesting, but it does require a little more repetition.
For something as small as FizzBuzz, it comes down to personal preference. What matters most is making sure the conditions are in the right order.
Final thought
When I first tried FizzBuzz
, I made the mistake of checking i % 3 == 0
before checking for both 3 and 5. As a result, “FizzBuzz”
never printed. That small mistake taught me the importance of writing conditions in the right order.
And always remember: divisible means there should be no remainder.
No matter which style you use, always check the most specific case first.
Top comments (0)