C语言 虽然有多个条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2751406/
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
While with multiple conditions
提问by lego69
Can somebody please explain why an expression (I study C) like
有人可以解释一下为什么一个表达式(我学习 C)像
while(a!=1 || b!=1 || c!=1)
causes problems.
导致问题。
In particular I have this specific code:
特别是我有这个特定的代码:
while (ch != '\n' || ch != '\t' || ch != ' ') { ... }
回答by DVK
UPDATE: As per your other comment, your expression is wrong - it has nothing to do with "while" having multiple conditions.
更新:根据您的其他评论,您的表达是错误的 - 它与具有多个条件的“while”无关。
ch != '\n' || ch != ' 'is ALWAYS true, no matter what the characters is.
ch != '\n' || ch != ' '总是正确的,无论角色是什么。
If the character is NOT a space, the second condition is true so the OR is true.
如果字符不是空格,则第二个条件为真,因此 OR 为真。
If the character is a space, the first condition is true (since space is not newline) and the OR is true.
如果字符是空格,则第一个条件为真(因为空格不是换行符)并且 OR 为真。
The correct way is ch != '\n' && ch != ' ' ...
正确的方法是 ch != '\n' && ch != ' ' ...
OLD answer:
旧答案:
Under normal circumstances there's no problem whatsoever with the expression above (assuming you wanted to do just that).
在正常情况下,上面的表达式没有任何问题(假设您只想这样做)。
The only issue with yours is that it can sometimes be less than optimal (e.g. if b and c never change throughout the loop, in which case you need to cache the value of b!=1in a variable).
你的唯一问题是它有时可能不是最佳的(例如,如果 b 和 c 在整个循环中从未改变,在这种情况下你需要缓存b!=1变量中的值)。
whilewith multiple conditions may have a problemin one case - if those multiple conditions actually have intended side effects.
while在一种情况下,具有多个条件可能会出现问题-如果这些多个条件实际上具有预期的副作用。
That is due to lazy evaluation of || and && in C, so that if the first expression is true, the rest will NOT be evaluated and thus their side effects will not happen.
那是由于对 || 的懒惰评估 和 && 在 C 中,所以如果第一个表达式为真,则其余的将不会被评估,因此它们的副作用不会发生。
回答by Mahmoud Al-Qudsi
That's perfectly correct. But it depends on what you want, perhaps you mean &&instead of ||
这是完全正确的。但这取决于你想要什么,也许你的意思是&&而不是||
回答by Mark Byers
You should be careful when using not in boolean expressions that you don't get the AND and OR mixed up. Read about De Morgan's Laws. Often it is easier to read with only one negative. Applying De Morgan's Law to your expression gives this:
在布尔表达式中使用 not 时应该小心,以免将 AND 和 OR 混淆。阅读德摩根定律。通常只有一个否定更容易阅读。将德摩根定律应用于您的表达式,结果如下:
while (!(ch=='\r' && ch=='\n' && ch==' '))
If you had written it in this form you will hopefully immediate notice that (ch == '\r' && ch == '\n') can never be true.
如果您以这种形式编写它,您希望立即注意到 (ch == '\r' && ch == '\n') 永远不会为真。
The solution is to change this:
解决方案是改变这一点:
while (ch != '\n' || ch != '\t' || ch != ' ')
into this:
进入这个:
while (!(ch == '\n' || ch == '\t' || ch == ' '))
You can read it as "While we don't have \n or \t or space, do this...". Note that "while not" is similar to "until" in English (and some programming languages), so you can also read it as "Until we have \n or \t or space, do this".
您可以将其读作“虽然我们没有 \n 或 \t 或空格,但请执行此操作...”。请注意,“while not”类似于英语(和一些编程语言)中的“until”,因此您也可以将其读作“直到我们有 \n 或 \t 或空格,执行此操作”。
回答by fastcodejava
What problem are you referring to? It is usually make sense as :
你指的是什么问题?它通常是有意义的:
while(a == 1 || b == 1 || c == 1)
Or
或者
while(a != 1 && b != 1 && c != 1)
But there is no problem with the expression you have if that is what you want.
但是,如果这是您想要的,那么您的表达没有问题。

