I have come across the following code in C#.
if(condition0) statement0;
else if(condition1) statement1;
else if(condition2) statement2;
else if(condition3) statement3;
...
else if(conditionN) statementN;
else lastStatement;
Some of my colleagues tell me that this is an else if
statement. However, I am convinced that it is actually a multi-layered nested if-else
statement. I know that without delimiters {}
, one statement is allowed in an if
or else
. So in this case I think it would be equivalent to the following code.
if(condition0)
statement0;
else
if(condition1)
statement1;
else
if(condition2)
statement2;
else
if(condition3)
statement3;
else
...
Note that all I changed was the whitespace.
This indentation works because each else
goes back to the most recent if
statement when there are no delimiters.
Can anyone clarify if the else if
format in the first example is treated differently by the compiler than the nested if-else
format in the second example?