14.5. Statements
There are many kinds of statements in the Java programming language. Most correspond to statements in the C and C++ languages, but some are unique.
As in C and C++, the if
statement of the Java programming language suffers from the so-called “dangling else
problem,” illustrated by this misleadingly formatted example:
if (door.isOpen()) if (resident.isVisible()) resident.greet("Hello!");else door.bell.ring(); // A "dangling else"
The problem is that both the outer if
statement and the inner if
statement might conceivably own the else
clause. In this example, one might surmise that the programmer intended the else
clause to belong to the outer if
statement.
The Java programming language, like C and C++ and many programming ...
Get The Java® Language Specification, Java SE 7 Edition, Fourth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.