java - Are multiple 'if' statements and 'if-else-if' statements the same for mutually exclusive conditions? -


is there difference between writing multiple if statements , if-else-if statements ?

when tried write program multiple if statements, did not give expected results, worked if-else-if.

the conditions mutually exclusive.

when write multiple if statements, it's possible more 1 of them evaluated true, since statements independent of each other.

when write single if else-if else-if ... else statement, 1 condition can evaluated true (once first condition evaluates true found, next else-if conditions skipped).

you can make multiple if statements behave single if else-if .. else statement if each of condition blocks breaks out of block contains if statements (for example, returning method or breaking loop).

for example :

public void foo (int x) {     if (x>5) {         ...         return;     }     if (x>7) {         ...         return;     } } 

will have same behavior :

public void foo (int x) {     if (x>5) {         ...     }     else if (x>7) {         ...     } } 

but without return statements have different behavior when x>5 , x>7 both true.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -