Possible solutions are:
- Having an "end if" statement. ...
- Disallowing the statement following a "then" to be an "if" itself (it may however be a pair of statement brackets containing only an if-then-clause). ...
- Requiring braces (parenthesize) when an "else" follows an "if".
- Requiring every "if" to be paired with an "else".
What is dangling IF () problem?
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 Java?
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.