Nested tenary operation in Fizz Buzz

Hi there,

When I try the fizz buzz exercise, I think up with an interesting solution that I didn’t know why it works.
The codes are compact but it really works.

#include <stdio.h>

void main(void){
for(int i = 1; i <= 100; i++){
(i%3==0)&&(i%5==0)?printf(“FizzBuzz\n”):
i%3==0?printf(“Fizz\n”):
i%5==0?printf(“Buzz\n”):

printf("%d\n", i);

}

}//end main

It seems that tenary operation can be nested. Is that right?

Yeah tenary operator can be nested as much as you want. But as you said it yourself, it makes the code a lot compact and complex. I suggest only using tenary operator without nesting to let your code more readable.

^ (even if it’s in c++, it’s the same thing in c)

Thanks for the link! :smiley: