0

I don't understand this simple C# code

int a = 4; int b = ++a; if ( b > 5) { Console.Write(b); }else{ Console.Write(a) ; //output 5 b = 5 which is not higher than 5, so why is the program outputing the value of b?

11th Feb 2018, 8:26 AM
PinkBunny
PinkBunny - avatar
2 Answers
+ 7
a is also 5 b = ++a <- this increments the variable a by 1 then assigns a's value to b as a result both variables are 5. Note: the else statement is executed, (Console.Write(a))
11th Feb 2018, 8:28 AM
jay
jay - avatar
+ 1
Thank you Jay. I get it now.
11th Feb 2018, 8:42 AM
PinkBunny
PinkBunny - avatar