5

I ran across this puzzle today. Obviously, this isn't correct style, but I'm still curious as to why no output is coming out.

int x = 9;
int y = 8;
int z = 7;

if (x > 9) if (y > 8) System.out.println("x > 9 and y > 8");

else if (z >= 7) System.out.println("SHOULD OUTPUT THIS x <= 9 and z >= 7");

else
  System.out.println("x <= 9 and z < 7");

The above has no output when run. But, when we add in brackets for the if-statement, suddenly the logic behaves as I expect.

int x = 9;
int y = 8;
int z = 7;

if (x > 9) {
  if (y > 8) System.out.println("x > 9 and y > 8");
}

else if (z >= 7) System.out.println("SHOULD OUTPUT THIS x <= 9 and z >= 7");

else
  System.out.println("x <= 9 and z < 7");

This outputs "SHOULD OUTPUT THIS x <= 9 and z >= 7". What is going on here?

Thanks!

1
  • 3
    haha... The else applies to the inner-most nesting level.
    – Mysticial
    Oct 5, 2012 at 4:17

4 Answers 4

7

If you rewrite the first way like this (which is how it is behaving), it is easier to understand

if (x > 9)
  if (y > 8) System.out.println("x > 9 and y > 8");
  else if (z >= 7) System.out.println("SHOULD OUTPUT THIS x <= 9 and z >= 7");
  else
    System.out.println("x <= 9 and z < 7");

Since x is not > 9, the block never executes.

4

This:

if (x > 9) ... if (y > 8) ... else if (z >= 7) ... else

is ambiguous, because during parsing the else could be bound to the first if or the second if. (This is called the dangling else problem). The way Java (and many other languages) deals with this is to make the first meaning illegal, so the else clauses always bind to the innermost if statements.

1
  • Best answer, because it actually explains what the problem is called and why it occurs like so. +1
    – FThompson
    Oct 5, 2012 at 4:25
0

Just fix the indenting on your code and the issue becomes clear:

int x = 9;
int y = 8;
int z = 7;

if (x > 9)
    if (y > 8)
        System.out.println("x > 9 and y > 8");
    else if (z >= 7)
        System.out.println("SHOULD OUTPUT THIS x <= 9 and z >= 7");
    else
        System.out.println("x <= 9 and z < 7");
0

Because you are using the else block in inner most level

Your code is being treated as the following code

if (x > 9) // This condition is false, hence the none of the following statement will be executed
{
    if (y > 8) 
    {
        System.out.println("x > 9 and y > 8");
    } else if(z >= 7)
    {
        System.out.println("SHOULD OUTPUT THIS x <= 9 and z >= 7");
    }
    else 
    {
        System.out.println("x <= 9 and z < 7");
    }
}

The first condition specified with the if statement is false and control in not entering into the code associated with that condition and simply reaching the end of the program and printing nothing.

That's why its normal practice is to enclose statements with brackets even if you are writing a single statement.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.