The dangling else is a problem in computer programming in which an optional else clause in an if–then(–else) statement results in nested conditionals being ambiguous. Formally, the reference context-free grammar of the language is ambiguous, meaning there is more than one correct parse tree.
How is the dangling else ambiguity resolved in C ++?
Java, C and C++ have chosen to resolve the Dangling-Else ambiguity uses the following rule: An else keyword always associates with the nearest preceeding if keyword that does NOT cause a syntax error.
How is the dangling else ambiguity resolved in Java?
The ambiguity that arises if a language allows constructs of the form if b1 then if b2 then S1 else S2 in which it is not clear which if is associated with the else. Algol 60 resolved the ambiguity by forbidding the use of if immediately after then; Pascal, C, C++, and Java associate the else with the innermost if.