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>7) {
...
return;
}
if (x>5) {
...
return;
}
}
Will have the same behavior as :
public void foo (int x)
{
if (x>7) {
...
}
else if (x>5) {
...
}
}
But without the return statements it will have different behavior when x>5 and x>7 are both true.
Answer from Eran on Stack OverflowVideos
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>7) {
...
return;
}
if (x>5) {
...
return;
}
}
Will have the same behavior as :
public void foo (int x)
{
if (x>7) {
...
}
else if (x>5) {
...
}
}
But without the return statements it will have different behavior when x>5 and x>7 are both true.
Yes, it makes a difference: see The if-then and if-then-else Statements.
Furthermore, you can easily test it.
Code #1:
int someValue = 10;
if(someValue > 0){
System.out.println("someValue > 0");
}
if(someValue > 5){
System.out.println("someValue > 5");
}
Will output:
someValue > 0
someValue > 5
While code #2:
int someValue = 10;
if(someValue > 0){
System.out.println("someValue > 0");
}else if(someValue > 5){
System.out.println("someValue > 5");
}
Will only output:
someValue > 0
As you can see, code #2 never goes to the second block, as the first statement (someValue > 0) evaluates to true.
if(i == 0) ... //if i = 0 this will work and skip the following else-if statements
else if(i == 1) ...//if i not equal to 0 and if i = 1 this will work and skip the following else-if statement
else if(i == 2) ...// if i not equal to 0 or 1 and if i = 2 the statement will execute
if(i == 0) ...//if i = 0 this will work and check the following conditions also
if(i == 1) ...//regardless of the i == 0 check, this if condition is checked
if(i == 2) ...//regardless of the i == 0 and i == 1 check, this if condition is checked
The difference is that if the first if is true, all of the other else ifs won't be executed, even if they do evaluate to true. If they were individual ifs, nevertheless, all of the ifs will be executed if they evaluate to true.