for example:
if(cond1)
if(cond2) DoCond2();
else DoCond3();
Will the else
statement be considered as the else of the first if
or the second if
? Why? (is there an explanation from syntactic point of view?)
Is the answer also the same in the other C-based programming languages such as C and Java?
Note: this is not homework. I can easily test it, and use the curly brackets if I don't like the default behaviour, bu开发者_高级运维t I'm curious as to the reason.
Edit Guys, apparently there was a very serious mistake in the original example. Please check again. I know, it's embarrassing. Sorry.
As per MSDN
In nested if statements, the else clause belongs to the last if that does not have a corresponding else.
Also C and C++, conditions can be evaluated where a result of 0 is false and any other number is true. In C#, the condition must evaluate to a boolean value true/false
EDIT As per the Edit its a nested if, so the inner one is evaluated when true and still the else is with the inner if
Will the else statement be considered as the else of the first if or the second if?
Edit (removed what I original said here - it doesn't apply):
With such a construction, I couldn't tell you. The syntax for each language is ambiguous.
But I'd personally guess it would "stick" to the closest if
. Otherwise it would be nearly impossible for someone to read.
I'd say try it and see, but the best solution is to apply a coding standard that forbids people from ever doing this...
Why? (is there an explanation from syntactic point of view?)
You can check out the (often unofficial) grammar definition for each language to see why such statements hang together the way they do.
The (old) official C# grammar:
http://msdn.microsoft.com/en-us/library/aa645596(v=VS.71).aspx
Edit: But it won't help you much, because the grammar for C-like languages in this case requires context, or deliberate work-arounds.
See: http://en.wikipedia.org/wiki/Dangling_else#Context_Free_Solutions
Is the answer also the same in the other C-based programming languages such as C and Java?
Grammars are complicated things, and getting nested scopes right is a bit hard. But in general, the answer will often revolve around definitions of block
s vs statement
s.
You could define a statement
as a line ended by a ;
. Then define a block
as a series of statement
s surrounded by {
}
.
So, you could define your if
statement as if block-or-statement [else block-or-statement]
, the [
]
denoting that it is optional.
Edit: The problem with this definition is that such a grammar requires context, and only solves the problem with the original formulation of your question. When it is truly ambiguous (as your new, edited version), you must have higher level processing (context) to deal with the ambiguity.
C++, for example:
- (grabbed link from) Is there a yacc-able C++ grammar?
- http://www.computing.surrey.ac.uk/research/dsrg/fog/CxxGrammar.y
A few times in that last document you will see "dangling else". This specifically describes what you're looking for.
This problem exists in many languages, not just C-likes. It is one reason you see explicit statements like end if
in BASIC variants, or Python's rules for if
s.
Let's clarify how the if
statement works.
After an if
clause, you can have two things:
- Single line statement
- Multi line statement
The single line statement is like the two examples you provided. In that scenario, the else
belongs to the if
only if it is directly after it (like your second example).
The multi line statement will have curly brackets { }
following the if statement. An else
following the closing bracket makes it belong to that statement.
You can "chain" if
statements using the else if
syntax. The following if will behave like a normal if, but it will be checked only if the previous if
returns false. In your example, both if
statements will be checked regardeless of the result.
Maybe to understand this it would be useful to add braces:
if(cond1) {
DoCond1();
}
if(cond2) {
DoCond2();
} else {
DoCond3();
}
If there are no braces after if
or else
, only the first statement (in this case DoCondX
) belongs to the if/else block.
EDIT Looking at the code after the edit:
if(cond1) {
if(cond2) {
DoCond2();
} else {
DoCond3();
}
}
精彩评论