2

Normally in JAVA if an IF statement doesn't have curly brackets can have only one line that is executed when IF condition is met, however if another IF block (inner IF) follows the initial IF, then no error is triggered and there are more lines. How is this possible?

Example

if (true)
if (true)
   System.out.println("true");
else
   System.out.println("false");
Alin
  • 217
  • 1
  • 3
  • 7

4 Answers4

5

No errors because it equals to

if (true) {
    if (true) {
       System.out.println("true");
    }
    else
    {
       System.out.println("false");
    }
}

And a valid syntax.

But please always use {} otherwise, it's very hard to understand where the if block ends exactly.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • 1
    +1 I suggest you always use standard indentation or use you IDE to do it for you. That way you can clearly see the nesting even if you don't have `{ }` – Peter Lawrey Aug 10 '13 at 09:41
  • if (true) int x=0; int y=0; is identical with if (true) {int x=0; int y=0;} and still returns an error. – Alin Aug 10 '13 at 09:51
  • @Alin Which error you got ?? – Suresh Atta Aug 10 '13 at 09:53
  • I get an error if I put an else after the 2 statements, while using curly braces I don't. If I don't have an if, int y=0; it will not be executed as part of the if block. – Alin Aug 10 '13 at 09:57
  • I'm not sure,what I understood is, now you are facing some scoping of local variables issue,And also one of the main reason is to always use `{}` – Suresh Atta Aug 10 '13 at 10:00
  • No. You are saying that if(true) if(true) System.out.println("true"); else System.out.println("false"); doesn't return an error because can be translated in statements with braces. But having if(true) if(true) System.out.println("true1"); System.out.println("true2"); else System.out.println("false"); can also be translated in statement with braces, but compiler is complaining, so having the translation possible is not the reason why is working. Hope this clarify. I used variables because prints are longer and code isn't very readable in comments. Thanks – Alin Aug 10 '13 at 10:10
2

That's because your outer if block really contains a single statement.

  • If inner if condition is true, the outer if is equivalent to:

    if (true)
        System.out.println("true");
    
  • And if, inner if condition if false, it is equivalent to:

    if (true)
        System.out.println("false");
    

Still, it is really a bad idea to omit the curly braces around if-else or loops for that matter, and specially doing this with nested block, can turn evil. Just because it can be done, doesn't mean you should do it.

To see how the nested if blocks without braces can grow ugly, and often lead to mistakes, consider this code, what do you think the output should be?

boolean b = true;
boolean b2 = false;

if (b)
    if (true)
       System.out.println("true");
       if (b2)
           System.out.println("Hello");
else
    System.out.println("false");
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
2

Normally in JAVA if an IF statement doesn't have curly brackets can have only one line that is executed when IF condition is met,

Correction. An if statement without braces can have only one statement that is executed when the condition is met. And the syntax of if goes something like

if (<condition>) <statement>; [else <statement>;]

That is, if there's an else, it's part of the if. It's all one statement.

The reason there's no error is because there's no ambiguity here. (Well, not to the compiler, anyway.) Since the else is part of the if, it goes with the closest if. So with proper indenting, you have

if (true)
    if (true)
       System.out.println("true");
    else
       System.out.println("false");
cHao
  • 84,970
  • 20
  • 145
  • 172
0

consider this IF statement as:

    if(true) {
       if(true) {
           System.out.println("true"); 
       }
       else {
           System.out.println("false");
       }
    }

if first IF is true it goes to next statement, here next statement is also a IF and also true therefore it executes statements in it. If suppose it is false then it search for else end executes statements in else. So all statement are got covered and no error scenario occurs.

Ashwani
  • 3,463
  • 1
  • 24
  • 31