java - No curly braces if else statements? -


int x = 5, y = 5, z = 5;   if ( x != 5 )     if ( y <= 7 )     z = z + 4;   else     z = z + 2;  system.out.println( z );  

will "else" runs? "if" belong to? why or why not? thanks

it print out 5. doesn't approach second if-else, because doesn't pass first condition x!=5 , else statement missing , goes last line print variable out. it's same as:

int x=5, y=5, z=5; if (x!=5) {     if (y<=7) {         z=z+4;     } else {         z=z+2;     } } system.out.println(z);  

that's reason why using both of brackets () , {} correctly more recommended!


Comments