简体   繁体   English

条件语句在if-else-if梯子的两个部分都为true

[英]Conditional statement true in both parts of if-else-if ladder

If you have code like this: 如果您有这样的代码:

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

With A > X and B > Y , will both parts of the if-else-if ladder be executed? A > XB > Yif-else-if梯子的两个部分都会被执行吗?

I'm dealing with Java code where this is present. 我正在处理存在此问题的Java代码。 I normally work in C++, but am an extremely new (and sporadic) programmer in both languages. 我通常在C ++中工作,但在两种语言中都是一个非常新的(和零星的)程序员。

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. 同样地,如果你有'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. 如果学生得到99%,所有这些条件都是正确的。 However, you're not going to assign the student A, B and C. 但是,你不会指派学生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块不会被执行。

If both A > X and B > Y are true then your code will only execute Action1 . 如果A > XB > Y都为真,那么您的代码将只执行Action1 If one of the conditions is true it will execute Action2 . 如果其中一个条件为真,它将执行Action2 If none are true, it will do nothing. 如果没有,它将什么都不做。

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 > X and B > Y are both true. A > XB > Y都为真时,将导致两种动作发生的可能性。

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”就可以了。

When the condition after if is true, only the first block is executed. if之后的条件为真时,仅执行第一个块。 The else block is only executed when the condition is false. else在条件为false时执行。 It doesn't matter what's in the else block, it's not executed. else块中没有什么关系,它没有被执行。 The fact that the else block is another if statement is irrelevant; else块是另一个if语句的事实是无关紧要的; 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. 它不会被执行,因此它永远不会执行(A > X || B > X)测试,即使该条件为真,也不会执行它的正文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM