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");