Are multiple 'if' statements and 'if-else-if' statements the same for mutually exclusive conditions?
When you write multiple if statements, it's possible that more than one of them will be evaluated to true, since the statements are independent of each other.
When you write a single if else-if else-if ... else statement, only one condition can be evaluated to true (once the first condition that evaluates to true is found, the next else-if conditions are skipped).
You can make multiple if statements behave like a single if else-if .. else statement if each of the condition blocks breaks out of the block that contains the if statements (for example, by returning from the method or breaking from a loop).
For example :
public void foo (int x)
{
if (x>5) {
...
return;
}
if (x>7) {
...
return;
}
}
Will have the same behavior as :
public void foo (int x)
{
if (x>5) {
...
}
else if (x>7) {
...
}
}
But without the return statements it will have different behavior when x>5 and x>7 are both true.