英文:
else condition after an if (multiple conditions) (C)
问题
以下是翻译好的部分:
我正在编写一些 C 代码。
我想知道的是:如果我有以下条件:
if (condition1 && condition2) {
做某事
} else if (condition1) {
做其他事情
}
这是否正确?还是 else 只接受排除在 if 内指定条件之外的条件?
我猜想,如果使用逻辑或(||)而不是 && 的话,这将毫无意义,因为这意味着如果我们进入 else,condition1 和 condition2 都将为假。但使用 && 时,我认为这是正确的,因为 if 内的第一个条件由 BOTH 条件的匹配给出,所以 else 只是其中一个条件(因此意味着 condition2 为假)。
这是否正确?
尚未在实际代码中尝试过这个,只是在转移到代码之前理论上试图解决这个问题。
英文:
I'm writing some lines of code in C.
what I would like to know is: if I have the following condition:
if (condition1 && condition2) {
do something
} else if (condition1) {
do something else
}
Would this be correct? or does the else only admits conditions that exclude the ones specified inside the if?
I guess this would not make sense in case of logical OR (||) instead of &&, cause in that case it would mean that if we reach the else, BOTH condition1 and condition2 would be false. But with &&, I assume it would be correct since the first condition inside the if is given by the matching of BOTH conditions, so the else might be just one of them (so meaning condition2 is false).
is this correct?
`
Haven't tried this on actual code yet, was just trying to solve this theoretically before moving to the code.
答案1
得分: 1
你的代码没问题。语言没有对if
和else if
中的条件关系施加内建限制。一些关系可能不太有用,你关于||
的观点是正确的。
然而,如果条件计算代价高的话,使用嵌套的if
可能更清晰,也更高效。
if (condition1) {
if (condition2) {
做一些事情
} else {
做其他事情
}
}
英文:
Your code is fine. The language has no built-in restrictions on how the conditions related in if
and else if
. Some relationships may not be useful -- your point about ||
is correct.
However, it may be clearer to refactor using nested if
so you don't repeat conditions. This is also more efficient if calculating the conditions is expensive.
if (condition1) {
if (condition2) {
do something
} else {
do something else
}
}
答案2
得分: 1
The else
block is executed only when the associated if
statement is reached and the if
condition evaluates to a falsey value. Your if
condition, condition1 && condition2
, can result in a falsey value even if condition1
is truthy.
In the case of using logical OR (||
) instead of &&
, if the if
condition were condition1 || condition2
, the if
branch would be taken whenever condition1
is true, and sometimes when it's not. In this case, the else if (condition1)
might not serve its intended purpose but isn't strictly incorrect in terms of the language.
英文:
> Would this be correct? or does the else only admits conditions that exclude the ones specified inside the if?
An else
block is executed if and only if control reaches the associated if
statement, and the condition of the if
evaluates to a falsey value.
But that doesn't make your example inherently wrong. Your if
condition is
condition1 && condition2
, which can evaluate to a falsey value despite condition1
being truthy.
> I guess this would not make sense in case of logical OR (||) instead of &&, cause in that case it would mean that if we reach the else, BOTH condition1 and condition2 would be false.
Yes, if the if
condition were condition1 || condition2
then the if
branch would be taken whenever condition1
was true (and sometimes when it wasn't), so the else if (condition1)
would be useless, and presumably not what was intended (but not strictly speaking wrong as far as the language goes).
答案3
得分: 0
如果进入该分支,或者不进入。如果第一个条件匹配,就执行第一个分支。
您可以通过尝试不同的值排列来解决这个问题:
条件1 | 条件2 | 分支 |
---|---|---|
真 | 真 | 第1 |
真 | 假 | 第2 |
假 | 真 | 无 |
假 | 假 | 无 |
英文:
if
either goes in that branch, or it doesn't. If the first conditions matched, the first branch ran.
You can work through this by exploring various permutations of values:
condition1 | condition2 | branch |
---|---|---|
true | true | 1st |
true | false | 2nd |
false | true | none |
false | false | none |
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论