什么是 C++对于“AND OR”?

发布于 2024-12-14 15:50:41 字数 674 浏览 2 评论 0原文

我正在写一些条件 if 并且我想要类似

if ( /*condition one */) and or ( /* condition two */   )

C++ 中是否有这样的运算符以及如何编写它?

所以我想要类似的东西

if((( /* condition A */)&&(/* condition B */ ))|| /* AND OR? */ ((  /* condition C */ )&&(  /* condition D */)))

(用于确定一个段的至少一侧是否属于另一个段)

所以...在c++中有“AND OR”并且我们应该总是像这样编写丑陋的代码重复:

if(((( /* condition A */)&&(/* condition B */ ))|| ((   /* condition C */ )&&(  /* condition D */))) || ((( /* condition A */)&&(/* condition B */ )) && (( /* condition C */ )&&(  /* condition D */))) 

I am writting some conditionall if and I want to have something like

if ( /*condition one */) and or ( /* condition two */   )

Is there such operator in C++ and how to write it?

So I want something like

if((( /* condition A */)&&(/* condition B */ ))|| /* AND OR? */ ((  /* condition C */ )&&(  /* condition D */)))

(for figuring out if at least one side of a segment belongs to another segment)

So... there is on "AND OR" IN c++ AND WE SHALL ALWAYS WRITE UGLY CODE DUPLICATION LIKE THIS:

if(((( /* condition A */)&&(/* condition B */ ))|| ((   /* condition C */ )&&(  /* condition D */))) || ((( /* condition A */)&&(/* condition B */ )) && (( /* condition C */ )&&(  /* condition D */))) 

?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

断舍离 2024-12-21 15:50:41

如果你的意思是我认为你的意思,那么在英语中它更常见地写作和/或,它的意思是这样的:

  • 如果A是真的,B是假的,或者
  • 如果A是假的,B是真的,或者
  • 如果A是真的并且 B 为真

,则表达式整体为真。

如果这就是您的意思,那么答案就是 or||

前两种情况在编程中通常称为“异或”或“异或”:即,如果一个为真,而另一个则不是。

C++ or 运算符是包含 OR,也就是说,如果一个或两个操作数为 true,则为 true

If you mean what I think you mean, then in English it is more commonly written and/or, and what it means is this:

  • if A is true and B is false OR
  • if A is false and B is true OR
  • if A is true and B is true

then the expression is as a whole is true.

If that is what you mean, then the answer is simply or or ||.

The first two cases are typically referred to in programming as an exclusive or, or XOR: that is, if one is true, and the other is not.

The C++ or operator is an inclusive OR, that is, it is true if one or both of the operands are true

季末如歌 2024-12-21 15:50:41

如果有两个条件,AB

A | B | A and B | A or B
------------------------
T   T      T        T
T   F      F        T
F   T      F        T
F   F      F        F

如果 A 或 B 为真,则 A 和 B 也为真。听起来你只是想要 OR。

If you have two conditions, A and B

A | B | A and B | A or B
------------------------
T   T      T        T
T   F      F        T
F   T      F        T
F   F      F        F

If A or B is true, A and B is true as well. Sounds like you just want OR.

怎樣才叫好 2024-12-21 15:50:41

and or 只是布尔术语中的 or:您可以使用双管道 (||) 运算符。

if ( /*condition one */) || ( /* condition two */   )

and or would simply be or in boolean terms: you can use the double pipe (||) operator for that.

if ( /*condition one */) || ( /* condition two */   )
笛声青案梦长安 2024-12-21 15:50:41

只需使用。即使两个条件都为真,它的计算结果也会为true

换句话说,“或”运算是包含或,而不是排除或。后者要求两个条件之一为真。

我们通常使用 || 运算符,但 or 也可以:

if (/* condition one */ || /* condition two */)

Just use or. It will evaluate to true if even if both conditions are true.

In other words, the "or" operation is inclusive or, not exclusive or. The latter requires that exactly one of the two conditions be true.

We usually use the || operator, but or also works:

if (/* condition one */ || /* condition two */)
醉殇 2024-12-21 15:50:41

并接受带有以下输出的输入

true, true -> true
true, false -> false
false, true -> false
false, false -> false

或 接受带有以下输出的输入

true, true -> true
true, false -> true
false, true -> true
false, false -> false

因此,如果“和或”意味着它可以是“和”,也可以是“或”,则使用单个“或”可以处理这两种情况,如果您只是验证“true”输出,如 true, true -> true (也是唯一的 true 输出)已包含在标准 or 输出中。

如果“and or”意味着它必须是“and”并且它也必须是“or”,如果您仅验证“true”输出,则使用单个“and”可以处理这两种情况,如 true, true -> true (也是唯一的 true 输出)已包含在标准 or 输出中。

如果您仅验证“假”输出,则选择哪个运算符的优先级将根据组合的解释而反转。

And takes input with the following output

true, true -> true
true, false -> false
false, true -> false
false, false -> false

Or takes the input with the following output

true, true -> true
true, false -> true
false, true -> true
false, false -> false

So if "and or" means that it could be "and" and it could be "or", using a single "or" handles both scenarios if your are only verifying "true" output, as true, true -> true (and's only true output) is already contained in the standard or output.

If "and or" means that it must be "and" and it must also be "or" using a single "and" handles both scenarios if you are only verifying "true" output, as true, true -> true (and's only true output) is already contained in the standard or output.

If you are verifying only "false" output, the precedence of which operator to select is inverted depending on the interpretation of the combination.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文