Java If和Else If的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20259351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:38:20  来源:igfitidea点击:

Difference Between If and Else If?

javaif-statement

提问by sparklyllama

I was wondering why you would use an else ifstatement, and not multiple ifstatements? For example, what's the difference between doing this:

我想知道为什么你会使用一个else if语句,而不是多个if语句?例如,这样做有什么区别:

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.

他们似乎在做同样的事情。

采纳答案by Damodaran

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

回答by MultiplyByZer0

The difference is that if the first ifis 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.

不同之处在于,如果第一个if为真,则所有其他 elseif都不会被执行,即使它们的计算结果为真。if但是,如果它们是单独的s,if那么如果它们的评估结果为真,则所有的s 都将被执行。

回答by shreyansh jogi

ifstatements check for all multiple available if. while else ifcheck when ifstatements fails , ifstatement return true it will not check for else if.

if语句检查所有多个可用的if. else ifif语句失败时检查,if语句返回 true 它不会检查else if

so it is depend on scenario how your requirement is.

所以这取决于你的要求如何。

回答by bvj

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

第一个示例不一定会运行 3 个测试,其中第二个示例不会给出任何返回或 goto。

回答by midhunhk

In first case, as soon as an ifor else ifcondition becomes true, all the "else if" are skipped / not checked.

在第一种情况下,只要iforelse if条件变为真,就会跳过/不检查所有“else if”。

In the second case, even if value of i is 0, all the following conditions are tested.

在第二种情况下,即使 i 的值为 0,也会测试以下所有条件。

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.

因此,您可以推断出,如果您正在测试同一个变量——在给定时间不能有多个值,更好的选择是使用第一种方法,因为它是最佳的。

回答by Foolish

No they are different. execution will check in every if. i.e.

不,它们是不同的。执行将检查每个 if。IE

if(true)
 executes
if(true)
  executes // no matter how many ifs you have

while with if and else if

while 与 if 和 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.

简而言之,如果梯子将被执行,则只有其他任何一个。

回答by Deepak

If you have used multiple ifstatements then if the condition is trueall will be executed. If you have used ifand else ifcombination only one will be executed where first comes the true value

如果您使用了多个if语句,那么如果条件为trueall 则将执行。如果您使用了ifelse if组合,则只会在第一个出现真值的地方执行

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

回答by sergeyan

For the first case: once an else if(or the first if) succeeds, none of the remaining else ifsor elseswill be tested. However in the second case every ifwill be tested even if all of them (or one of them) succeeds.

对于第一种情况:一旦else if(或第一个if)成功,其余的else ifselses都不会被测试。然而,在第二种情况下,即使所有(或其中一个)都成功,每个if也会被测试。

回答by Suganthan Madhavan Pillai

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

看,如果你想检查所有条件,比如一、二、三......你的第二个选择是好的,但在很多情况下你只检查一个条件,所以你有防止其他条件不执行,在那种特殊情况下你必须选择你的第一选择

回答by chia yongkang

if : executed only if "condition" is true

if :仅当“条件”为真时执行

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

elif :仅当“条件”为假且“其他条件”为真时执行