Originally Posted By: tfabris
My question is what happens with nested if-else-if statements in situations like that. [...] Will the second example function exactly like the first example?

My answer to that is "it's no longer a *trivial* single line of code, and should use braces." That nested if-else-if may have single *statements*, but it's not a single line of code, IMHO. Because C# uses "else if", as opposed to "elsif" or something like that, this is clearly in the territory of ambiguous code. My expectation is that unless the the parser considers whitespace a-la python, it will chunk the tokens as (if) (else if) (else), as opposed to (if) (else) { (if) (else) }.

That said, they're logically equivalent. Cribbing a bit of symbolic logic symbols (it's been too long since I've done any formal logic for the following to really be proper, but whatever):
Code:
if (X) { } else if (Y) { } else { }
  X     v    ¬X ^ Y     v  ¬X ^ ¬Y

vs.
Code:
if (X) { } else { if (Y) { } else { } }
  X     v   ¬X  ^  ( Y    v   ¬Y ) 

Using the distributive property, the latter, of course, simplifies to:
Code:
  X     v    ¬X ^ Y     v  ¬X ^ ¬Y

which is identical to the other statement.

edit: fixed symbols.


Edited by canuckInOR (18/05/2016 20:01)