Incremental Java
Dangling else

Dangling else

Consider the following statement.
if ( num > 0 )   
  if ( num < 10 ) 
    System.out.println( "aaa" ) ;
  else
    System.out.println( "bbb" ) ;
In the example above, the else is indented with the inner if statement. It prints "bbb" only if num > 0 and num >= 10, which means it prints only if num is 10 or greater.

In the example below, the else is indented with the outer if statement.

if ( num > 0 )   
   if ( num < 10 ) 
      System.out.println( "aaa" ) ;
else
   System.out.println( "bbb" ) ;
This prints if num <= 0. Depending on which if the else is paired with, you can get different results.

By now, you should know that Java doesn't pay attention to indentation. So both examples above are the same to Java, which means that it must pick one version over the other.

Which one does it pick? It picks the first one. This phenomenon of having to pick between one of two possible if is called the dangling else.

Rule

An else matches with the nearest, previous, unmatched if that's not in a block.

By not in a block, I mean that the else is outside the block that the if is inside.

Thus,

if ( num > 0 )   
  if ( num < 10 ) // Previous unmatched if
    System.out.println( "aaa" ) ;
  else  // Matches with previous unmatched if
    System.out.println( "bbb" ) ;
What if we wanted the else to match with the first if?

Then, we need braces.

if ( num > 0 )  // Previous unmatched if
{
   if ( num < 10 ) // Unmatched, but in block
     System.out.println( "aaa" ) ;
}
else  // Matches with previous unmatched if not in block
   System.out.println( "bbb" ) ;
The else matches with the previous, unmatched if not in a block. This happens to be the outer if. There is an unmatched if that's the inner if, but that's in a block, so you can't match to it. Notice the else sits outside of the block. An else can not match to a previous if if the else is outside a block, while the previous if is inside.

Another Example

Let's add a second else.
if ( num > 0 )    // Outer if
  if ( num < 10 ) // Inner if
    System.out.println( "aaa" ) ;
  else  // Matches inner if
    System.out.println( "bbb" ) ;
else  // Matches outer if
   System.out.println( "ccc" ) ;
Using our rule, the first else matches the inner if. Since the inner if is already matched, you can't do any match it with the next else.

The second else must match with the outer if because the inner if is already matched.

Each else must match with a unique if

Every else must match with a unique if. You can't have two or more else matching to the same if. If there is no match, then your program won't compile.

Each else be preceded immediately by a valid if

Furthermore, every else has to have a valid if statement just before it. Fortunately, your program fails to compile if you don't do this.

Using Braces

Again, if you always use a block for if body and else body, then you can avoid confusion. However, it's useful to know the correct rule, which is why we wrote it out.