![]() |
![]() |
![]() |
![]() |
![]() |
|
In the following nested if statement, it is not immediately clear
whether the question mark would be replaced by "long" or
"short":
if (length > 60)
if (length < 90)
System.out.println("It is normal!");
else
System.out.println("It is ?");
As the nested if statement is laid out on the page, it seems that
"long" is the right answer. If the nested if statement were
laid out another way, however, you might have the impression that
"short" is the right answer:
if (length > 60)
if (length < 90)
System.out.println("It is normal!");
else System.out.println("It is ?");
Because Java pays no attention to layout, you need to know that Java
assumes that each else belongs to the nearest if that is not
already matched with an else. Thus, the question mark should be
replaced by "long".