Do else if statements exist in C #?

I met 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 . However, I am convinced that this is actually a multi-level nested if-else . I know that without delimiters {} one statement is allowed in if or else . Therefore, in this case, I think it will be equivalent to the following code.

 if(condition0) statement0; else if(condition1) statement1; else if(condition2) statement2; else if(condition3) statement3; else ... 

Please note that everything I changed was a space. This indentation works because every else returns to the most recent if when there are no delimiters.

Can someone clarify if the else if format in the first example matches the compiler differently than the nested if-else format in the second example?

+45
c # if-statement
Jul 30. '10 at 19:57
source share
8 answers

You're right; there is no such thing as "else if" in C #. This is just another, where the statement of an alternative sentence is itself an if statement.

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

Note that there is a #elif constructor in the #elif .

Note also that C, C ++, and ECMAScript - and I am sure that many more C-like languages โ€‹โ€‹- also have the property that there is no formal else else statement. Rather, in each behavior, it falls out of the definition of "else" as coming before one statement.

+75
Jul 30 '10 at 20:02
source share

This is a multi-level if-else.

The reason this is due to C # syntax rules. An else follows an instruction, and any if chain qualifies as an instruction.

+3
Jul 30 '10 at 20:00
source share

The else if never mentioned in the C # specification , with the exception of some examples where it is used without explanation. Therefore, I do not think that this is a special construction; it is simply embedded in operators.

+3
Jul 30 '10 at 20:02
source share

In C # there is no "else if" statement.

In this regard, I do not know that in C # there are keywords with several words.

+2
Jul 30 '10 at 20:01
source share

You're right. This is just else followed by if .

+2
Jul 30 2018-10-10T00:
source share

To expand on @hunter the answer to the reason when you click on it that without parentheses it will only execute the following line, if it is a bunch of nested else, parentheses are needed:

 if(condition0) statement0; else { if(condition1) statement1; else { if(condition2) statement2; else { if(condition3) statement3; else ... } } } 
+1
Jul 30 '10 at 20:02
source share

The statement on choosing a C # language specification shows only if and switch expressions. If you select the if , it says:

The if statement selects the statement to execute based on the value of the boolean expression.

if-expression:

if (boolean-expression) embedded-statement

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

The other part is related to the lexically nearest previous, if syntax allowed

+1
Jul 30 '10 at 20:05
source share

The two examples you give are equivalent in each language. In C or C #, this is exactly equivalent to else, then if. In some other languages, elseif is syntactic sugar for another, then if. Therefore, no matter what language you use, they will be compiled into the same code (or interpreted according to the same behavior). See http://en.wikipedia.org/wiki/Conditional_%28programming%29#Else_If

+1
Jul 30 '10 at 20:05
source share



All Articles