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.
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.
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.