Reputation: 1326
I was wondering why you would use an else if
statement, and not multiple if
statements? For example, what's the difference between doing this:
if(i == 0) ...
else if(i == 1) ...
else if(i == 2) ...
And this:
if(i == 0) ...
if(i == 1) ...
if(i == 2) ...
They seem to do the exact same thing.
Upvotes: 48
Views: 105978
Reputation: 53
A possible answer would be this:
if(i == 0)
{
i++;
System.out.println("%d", i);
}
else if(i == 1)
{
System.out.println("HI!");
}
the output would happen to be only
1
if(i == 0)
{
i++;
System.out.println("%d", i);
}
if(i == 1)
{
System.out.println("HI!");
}
the output would happen to be
1HI!
Upvotes: 2
Reputation: 11047
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
Upvotes: 80
Reputation: 7119
The difference is that if the first if
is true, all of the other else if
s won't be executed, even if they do evaluate to true. If they were individual if
s, nevertheless, all of the if
s will be executed if they evaluate to true.
Upvotes: 19
Reputation: 782
if : executed only if "condition" is true
elif : executed only if "condition" was false and "other condition" is true
Upvotes: 0
Reputation: 2895
If you have used multiple if
statements then if the condition is true
all will be executed. If you have used if
and else if
combination only one will be executed where first comes the true value
// if condition true then all will be executed
if(condition) {
System.out.println("First if executed");
}
if(condition) {
System.out.println("Second if executed");
}
if(condition) {
System.out.println("Third if executed");
}
// only one will be executed
if(condition) {
System.out.println("First if else executed");
}
else if(condition) {
System.out.println("Second if else executed");
}
else if(condition) {
System.out.println("Third if else executed");
}
Upvotes: 10
Reputation: 5655
See, if you want check all the condition like one, two, three... you second choice is fine, but in many cases you have check only one condition, so you have prevent other conditions not to execute, at that particular case you have to choose your first choice
Upvotes: 1
Reputation: 1183
For the first case: once an else if (or the first if) succeeds, none of the remaining else ifs or elses will be tested. However in the second case every if will be tested even if all of them (or one of them) succeeds.
Upvotes: 4
Reputation: 4202
No they are different. execution will check in every if. i.e.
if(true)
executes
if(true)
executes // no matter how many ifs you have
while with if and else if
if(true)
executes
else if(true)
// system doesn't checks for this once if gets true
in short only one of any else if ladder will get executed.
Upvotes: 1
Reputation: 5554
In first case, as soon as an if
or else if
condition becomes true, all the "else if" are skipped / not checked.
In the second case, even if value of i is 0, all the following conditions are tested.
So you can infer that, if you are testing for the same variable - which can't have multiple values at a given time, the better option is to use the first approach as it will be optimum.
Upvotes: 1
Reputation: 3392
The first example won't necessarily run 3 tests where the 2nd example will given no returns or gotos.
Upvotes: 1
Reputation: 2102
if
statements check for all multiple available if
.
while else if
check when if
statements fails , if
statement return true it will not check for else if
.
so it is depend on scenario how your requirement is.
Upvotes: 3