C# version Somchok Sakjiraphong 17th July, 2008 18:23 (UTC)

//just rounding things up to a single line…

for (int i = 1; i <= 100; i++) { Console.WriteLine((i % 3 == 0 && i % 5 == 0) ? “FizzBuzz” : ((i % 3 == 0) ? “Fizz” : (i % 5 == 0) ? “Buzz” : i.ToString())); }

//anyway the class definition and the method has to declared too in order to compile.. //Cheers… :)

C# version Kirit Sælensminde 18th July, 2008 11:19 (UTC)
Somchok Sakjiraphong said

//just rounding things up to a single line…

If you're going for one line can't you remove the braces and have a single expression?

for (int i = 1; i <= 100; i++)
    Console.WriteLine(
        (i % 3 == 0 && i % 5 == 0) ? “FizzBuzz” :
            ((i % 3 == 0) ? “Fizz” :
                (i % 5 == 0) ? “Buzz” :
                    i.ToString())
    );

I can never work out how expressions like this should really be laid out to make them readable though. Maybe the fact that it's so hard to lay out is a reason for not wanting to put them in the code?


To join in the discussion you should register or log in