views:

682

answers:

8

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?

+3  A: 

It's a multi-layered if-else.

The reason it is has to do with c# syntax rules. An else is followed by a statement, and any if chain qualifies as a statement.

Robert Harvey
A: 

There is no "else if" statement in C#.

For that matter, I don't know that there are any multi-word statement keywords in C#.

John Saunders
yield return. yield break.
Eric Lippert
Well, It's say `foreach..in` is a multi-word keyword, albeit broken up a bit.
James Curran
`yield return` would be a multi-word statement keyword. As would `foreach (... in ...)`, depending on how loosely you want to interpret the phrase.
cHao
`yield return` is not a single keyword. It is a contextual keyword followed by the required context keyword `return` or `break`;
Matthew Whited
try-catch-finally. do-while. switch-case.
Eric Lippert
@Eric: thanks. You're right, of course. No matter what it actually is in terms of the compiler grammar, "yield break" is conceptually the same kind of thing as "else if" would be, if that were a statement.
John Saunders
+1  A: 

You are correct. It's just an else followed by an if.

James Curran
+37  A: 

You are correct; there is no such thing as an "else if" statement in C#. It's just an else where the statement of the alternative clause is itself an if statement.

Of course, the IDE treats "else if" as special so that you get the nice formatting you'd expect.

Note that there is an #elif construct in the "preprocessor" syntax.

Note also that C, C++ and ECMAScript - and I am sure many more C-like languages - also have the property that there is no formal "else if" statement. Rather, in each the behaviour falls out of the definition of "else" as coming before a single statement.

Eric Lippert
Well I guess that settles it.
BlueRaja - Danny Pflughoeft
+1 because I always learn something new when you post an answer.
Robert Harvey
+3  A: 

The construct else if is never mentioned in the C# specification, except in some examples where it is used without explanation. So I do not think it is a special construct, it is just nested if statements.

Mark Byers
It is also not listed in the table of keywords (section 2.4.3), which is the formal definition of what constitutes a keyword token. That's enough for me, but to take it a bit further: A keyword is described as "an identifier-like sequence of characters"; the specification for identifiers does not allow whitespace characters. Since "else if" contains at least one whitespace character, I would say that it is not "an identifier-like sequence of characters".
Dr. Wily's Apprentice
A: 

To expand on @hunter's answer the reason, as you hit on it that without brackets it will only execute the next line, if it were a bunch of nested the else would need brackets:

if(condition0) 
  statement0;
else
{
  if(condition1)
    statement1;
  else
  {
    if(condition2)
      statement2;
    else
    {
      if(condition3)
        statement3;
      else
      ...
    }
  }
}
Dustin Laine
Down vote for explaining the brackets, which is what he thought was happening?
Dustin Laine
The issue is that else-if *is* a nested if-else structure and you don't actually need the brackets (not that they wouldn't be encouraged if actually written in a new line + indented style). Brackets are not about *lines*, they are about *statements*. The statement, in this case, is another *if*.
Anthony Pegram
Agree completely, I was more stating where his original thought was incorrect, and if it was as he thought it would need to be as I wrote it.
Dustin Laine
+1  A: 

The Selection Statement of the C# Language Specification only shows if and switch statements. If you select the if statement, it says:

The if statement selects a statement for execution based on the value of a Boolean expression.

if-statement:

if ( boolean-expression ) embedded-statement

if ( boolean-expression ) embedded-statement else embedded-statement boolean-expression: expression

An else part is associated with the lexically nearest preceding if that is allowed by the syntax

SwDevMan81
A: 

The two examples you give are equivalent in every language. In C or C#, it's exactly equivalent to an else, then if. In some other languages, elseif is syntactic sugar for else, then if. So no matter which language you use, they will compile to the same code (or be interpreted to the same behavior). See http://en.wikipedia.org/wiki/Conditional_%28programming%29#Else_If

Tegan Mulholland
In general, "else if" is treated as two separate statements by a compiler/interpreter. Languages with an "else if" construct usually use a single word, such as elseif or elif.
Tegan Mulholland