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, but 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 blocks vs statements.
You could define a statement as a line ended by a ;. Then define a block as a series of statements 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:
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 ifs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With