Jeff Snider
Jeff Snider

Reputation: 750

Java if-if-else behavior

I wrote a simple if/else in my code which worked fine. Later I added another level of if under the first, and was baffled by its behavior. Here's very simple code to recreate the situation:

public static void main(String[] args) {
    boolean a = true;
    boolean b = false;

    if (a)
        if (b)
            System.out.println("a=true, b=true");
    else
        System.out.println("a=false");
}

It returns "a=false", even though a is true!

It turns out the else binds with the nearest if, although I have not found it documented anywhere, and eclipse does not highlight the mismatched indentation levels as an error (although it does correct it when formatting the file).

A very, very strong argument for using braces!

Where is the binding order of else/if documented?

And as a challenge,

Is there a way to make the above code do what the indentation makes you expect without adding braces?

Upvotes: 4

Views: 1573

Answers (7)

Taslim Oseni
Taslim Oseni

Reputation: 6263

First and foremost, best advice is to always use braces!

Naturally, every else statement is attached to the nearest if-statement and this is evident from this link and this is why your code behaves this way. The compiler cares nothing about indentation.. You can write your entire code on one line, it'd still work as long as its syntactically correct.

Also, if you want the output you expected, nesting an if-statement inside another if-statement (with no else-statement) is a primitive practice; this can simply be done with the && operator like this:

if (a && b)
        System.out.println("a=true, b=true");
else
    System.out.println("a=false");

You'd have gotten your desired output. I hope this helps.. Merry coding!

Upvotes: 0

Lieuwe Rooijakkers
Lieuwe Rooijakkers

Reputation: 164

public static void main(String[] args) {
    boolean a = true;
    boolean b = false;

    if (a && b)
        System.out.println("a=true, b=true");
    else if (!a)
        System.out.println("a=false");
}

Upvotes: 1

gherkin
gherkin

Reputation: 506

Is there a way to make the above code do what the indentation makes you expect without adding braces?

I would go:

public static void main(String[] args) {
    boolean a = true;
    boolean b = false;

    if (!a)
        System.out.println("a=false");   
    else if(b)
        System.out.println("a=true, b=true");

}

Upvotes: 4

Cruncher
Cruncher

Reputation: 7812

Is there a way to make the above code do what the indentation makes you expect without adding braces?

if (a)
    if (b)
        System.out.println("a=true, b=true");
    else;
else
    System.out.println("a=false");

else; will definitively finish the innermost if.

I would like to note that, if I ever came across this code in production, I would hunt through the commit logs until I found out who wrote it. This is absolutely an unacceptable thing to do. but it answers the quoted question

Upvotes: 11

Rohit Jain
Rohit Jain

Reputation: 213261

Is there a way to make the above code do what the indentation makes you expect without adding braces?

No. Because Java is not Python, and compiler doesn't work based on what's on your mind. That is why you should always use braces.

Clearly the else is a part of the inner if, and hence the result is expected. This is evident from JLS §14.5 - Statements

Upvotes: 19

John Koerner
John Koerner

Reputation: 38077

It is documented in Section 14.5 of the Java Language Spec:

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 languages before them, arbitrarily decrees that an else clause belongs to the innermost if to which it might possibly belong. This rule is captured by the following grammar:

Upvotes: 4

kosa
kosa

Reputation: 66637

It is clearly stated in Language specification 14.5. Statements:

Java programming language suffers from the so-called "dangling else problem,

Statements are thus grammatically divided into two categories: those that might end in an if statement that has no else clause (a "short if statement") and those that definitely do not.

Only statements that definitely do not end in a short if statement may appear as an immediate substatement before the keyword else in an if statement that does have an else clause.

Upvotes: 10

Related Questions