+ 2

Can someone explain to me how exactly this code works?

This was in C# challenge, it returns 1500, and I don't know what is going on here :c int a = 1500; int b = 2000 % 1; //int c = 1000; { if(b>a)//c+) Console.Write("2000"); } { Console.Write("1500"); } /*{ Console.Write("1000"); }*/

30th Apr 2017, 10:56 AM
Kuba Nawieśniak
Kuba Nawieśniak - avatar
6 Respostas
+ 18
Let's remove the parts which have been commented out. int a = 1500; int b = 2000 % 1; { if(b>a) Console.Write("2000"); } { Console.Write("1500"); } This should now be clear why the output is 1500.
30th Apr 2017, 10:59 AM
Hatsy Rei
Hatsy Rei - avatar
+ 15
a=1500; b=2000%1=0; Since, 0 is not bigger than 1500, thus only Console.WriteLine("1500"); runs
30th Apr 2017, 11:12 AM
Sachin Artani
Sachin Artani - avatar
+ 15
If you are writing a songle line statement after if or else, then adding curly braces are optional else mandatory. This program is doing useless things, so ignore as Hasty Rei said.
30th Apr 2017, 11:36 AM
Sachin Artani
Sachin Artani - avatar
+ 14
1) If you have no braces after if statement, only the first statement following the if statement will be executed if condition is fulfilled. 2) They do nothing. :D
30th Apr 2017, 11:40 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Thanks, I'm learning C# from yesterday, you've helped a lot! ^^
30th Apr 2017, 11:40 AM
Kuba Nawieśniak
Kuba Nawieśniak - avatar
+ 2
This part I understand, but 1. why there are no parentheses after if statement? 2. why if statement and write 1500 func are in parentheses which seems to do nothing?
30th Apr 2017, 11:34 AM
Kuba Nawieśniak
Kuba Nawieśniak - avatar