Difference Between If and Else If?

元气小坏坏 提交于 2020-04-07 12:45:44

问题


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.


回答1:


if(i == 0) ... //if i = 0 this will work and skip following statement
else if(i == 1) ...//if i not equal to 0 and if i = 1 this will work and skip following 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



回答2:


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, however, all of the ifs will be executed if they evaluate to true.




回答3:


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.




回答4:


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");
}



回答5:


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.




回答6:


The first example won't necessarily run 3 tests where the 2nd example will given no returns or gotos.




回答7:


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.




回答8:


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.




回答9:


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




回答10:


if : executed only if "condition" is true

elif : executed only if "condition" was false and "other condition" is true



来源:https://stackoverflow.com/questions/20259351/difference-between-if-and-else-if

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!