3 ms·
You assumption that a separate case that prints fizzbuzz will break the code is not correct. With correct code, you get three if checks either way, but the read
by rertrree 12y ago
You assumption that a separate case that prints fizzbuzz will break the code is not correct. With correct code, you get three if checks either way, but the readability is higher, code complexity is lower and cases are nicely separated.
bool is3 = i%3 == 0 ;
bool is5 = i%5 == 0 ;
if( is3 && is5 )
{
printf("fizzbuzz\n") ;
}
else if( is3 )
{
printf("fizz\n") ;
}
else if( is5 )
{
printf("buzz\n") ;
}
else
{
printf("%d\n" , i ) ;
}
- Terretta 12y agoAnd what if there's a is7 woof criteria as well? You going to add another set of special cases for fizzwoof, buzzwoof, and fizzbuzzwoof? You see the problem, versus just doing fizz, buzz, and woof distinctly?
- rertrree 12y agoIt is not logical to subvert your code to adhere to some imaginary future requirements.