java if-else-if 阶梯的两个部分的条件语句都为真

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26550990/
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-11-02 10:08:02  来源:igfitidea点击:

Conditional statement true in both parts of if-else-if ladder

javaif-statementconditional-statements

提问by traggatmot

If you have code like this:

如果你有这样的代码:

if (A > X && B > Y)
{
   Action1();
}
else if(A > X || B > Y)
{
   Action2();
}

With A > Xand B > Y, will both parts of the if-else-ifladder be executed?

使用A > XB > Yif-else-if梯形图的两个部分都会被执行吗?

I'm dealing with Java code where this is present. I normally work in C++, but am an extremely new (and sporadic) programmer in both languages.

我正在处理存在此问题的 Java 代码。我通常使用 C++ 工作,但我是两种语言的非常新的(和零星的)程序员。

回答by AdamMc331

No, they won't both execute. It goes in order of how you've written them, and logically this makes sense; Even though the second one reads 'else if', you can still think of it as 'else'.

不,他们不会同时执行。它按照你如何编写它们的顺序进行,从逻辑上讲这是有道理的;即使第二个读作“else if”,您仍然可以将其视为“else”。

Consider a typical if/else block:

考虑一个典型的 if/else 块:

if(true){
   // Blah
} else{
   // Blah blah
}

If your first statement is true, you don't even bother looking at what needs to be done in the else case, because it is irrelevant. Similarly, if you have 'if/elseif', you won't waste your time looking at succeeding blocks because the first one is true.

如果您的第一个陈述是正确的,您甚至不会费心查看在 else 情况下需要做什么,因为它无关紧要。同样,如果您有“if/elseif”,您就不会浪费时间查看后续块,因为第一个块为真。

A real world example could be assigning grades. You might try something like this:

一个现实世界的例子可能是分配成绩。你可以尝试这样的事情:

if(grade > 90){
   // Student gets A
} else if(grade > 80){
   // Student gets B
} else if(grade > 70){
   // Student gets c
}

If the student got a 99%, all of these conditions are true. However, you're not going to assign the student A, B and C.

如果学生得了 99%,则所有这些条件都为真。但是,您不会分配学生 A、B 和 C。

That's why order is important. If I executed this code, and put the B block before the A block, you would assign that same student with a B instead of an A, because the A block wouldn't be executed.

这就是为什么顺序很重要。如果我执行此代码,并将 B 块放在 A 块之前,您将为同一个学生分配 B 而不是 A,因为 A 块不会被执行。

回答by Grice

If both A > Xand B > Yare true then your code will only execute Action1. If one of the conditions is true it will execute Action2. If none are true, it will do nothing.

如果A > XB > Y都为真,那么您的代码只会执行Action1。如果其中一个条件为真,它将执行Action2。如果没有一个是真的,它什么也不做。

Using this:

使用这个:

if (A > X || B > Y) {
   Action2
   if (A > X && B > Y) {
      Action1                                    
   } 
}

will result in the possibility of both actions occurring when A > Xand B > Yare both true.

将导致两个动作发生的可能性,当A > XB > Y都为真时。

回答by Barmar

When the condition after ifis true, only the first block is executed. The elseblock is onlyexecuted when the condition is false. It doesn't matter what's in the elseblock, it's not executed. The fact that the else block is another ifstatement is irrelevant; it won't be executed, so it will never perform the (A > X || B > X)test, and its body will not be executed even if that condition is true.

当之后的条件if为真时,只执行第一个块。该else在条件为假时执行。else块中的内容无关紧要,它不会被执行。else 块是另一个if语句这一事实无关紧要;它不会被执行,所以它永远不会执行(A > X || B > X)测试,即使条件为真,它的主体也不会被执行。

回答by Freddie Chopin

If you're talking about C, then only the first block that satisfies the condition is executed - after the control "enters" the conditional block it then "leaves" after all other conditions.

如果你在谈论 C,那么只有满足条件的第一个块才会被执行——在控制“进入”条件块之后,它会在所有其他条件之后“离开”。

If you want such behavior, then just use two separate conditions - remove "else" and you have it.

如果你想要这样的行为,那么只需使用两个单独的条件 - 删除“else”,你就拥有了。