术语问题:&&操作员
嗯,我读了很多资料,但它们的定义不同:
&&是逻辑的逻辑运算符 连词 //我猜这是正确的
&&是逻辑 AND 运算符 //I 认为这不准确 技术角度
- &&是 条件运算符执行 逻辑与//我认为这是正确的 虽然
所有的理解都是正确的,但我想说第一个是最精确的。还是我错了?
Well I have been reading quite a lot sources but they differ in definiton:
&& is logical operator of logical
conjunction //I guess this is correct&& is operator of logical AND //I
think this is not precise from
technical point of view- && is
conditional operator performing
logical AND //I think this is right
as well
While all are correct in terms of understanding, I would say the first one is most precise. Or am I mistaken?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 C#、Java、C++、C 以及其他几种语言中,
&&
是布尔 AND 运算符的编程语言实现。它旨在考虑以下事实(不适用于纯命题逻辑):因此编程中的布尔表达式语言实际上有四种可能的结果:
true
、false
、“异常”和“无限循环”。在某些情况下,有两个布尔表达式,其中第二个表达式可能成功可以通过查看第一个表达式来确定。例如,使用表达式foo != null
和foo.bar == 42
,我们可以确定如果第一个表达式为 false,那么第二个表达式将失败。因此,&&
运算符被设计为“短路”:如果左操作数的计算结果为 false,则不会计算右操作数。在两个操作数都成功评估为 true 或 false 的所有情况下,此规则产生的结果与实际评估了两个操作数的结果相同,但它可以提高性能(因为可能根本不需要评估正确的操作数)并且在不牺牲安全性的情况下增加紧凑性(如果仔细构建表达式,使左操作数“保护”右操作数)。同样,如果左操作数的计算结果为 true,则||
将不会计算右操作数。一个简短的答案是,尽管
&&
受到 AND 的强烈启发,但它的设计考虑了某些编程特性,而a && b
也许应该表述为“如果a
为 false,则返回 false 的表达式,如果a
,则返回b
的值”是真的”。In C#, Java, C++, C, and probably several other languages,
&&
is a programming language implementation of the boolean AND operator. It is designed to account for the following facts (that do not apply in pure propositional logic):So a boolean expression in a programming language really has four possible outcomes:
true
,false
, "exception", and "infinite loop". In some situations, one has two boolean expressions where the possible success of the second expression can be determined by looking at the first expression. For instance, with the expressionsfoo != null
andfoo.bar == 42
, we can be certain that if the first expression is false, then the second expression will fail. Hence, the&&
operator is designed to be "short-cirquited": If the left operand evaluates to false, the right operand is not evaluated. In all cases where both operands would evaluate successfully to true or false, this rule produces the same result as if one actually had evaluated both operands, but it allows for increased performance (because the right operand might not need to be evaluated at all) and increased compactness without sacrificing safety (if one takes care to structure the expression such that the left operand "guards" the right one). Similarly,||
will not evaluate the right operand if the left operand evaluates to true.A shorter answer is that although
&&
is strongly inspired by AND, it is designed to take certain programming peculiarities into account, anda && b
should perhaps rather be phrased as "an expression that returns false ifa
is false, and the value ofb
ifa
is true".简单的答案。
它对 2 个返回布尔值的逻辑表达式执行 AND 运算。如果第一个为 false,则不会评估第二个。
无论使用 & 来评估两者。
复杂的答案。
http://www.ecma-international.org/publications/standards/Ecma-334.htm" rel="nofollow"> ecma-international.org/publications/standards/Ecma-334.htm 标题 12.3.3.23 似乎最相关。
我发现自己没有资格批评其中的定义。
[示例:在下面的代码中
我希望这能澄清问题。
The easy answer.
It performs an AND operation on 2 logical expressions returnting a Boolean. It does not evaluate the second if the first is false.
To evaluate both regardless use &.
The complicated answer.
http://www.ecma-international.org/publications/standards/Ecma-334.htm heading 12.3.3.23 seems most relavent.
I find myself unqualified to criticise the definition within.
[Example: In the following code
I hope that clears things up.
连词是“和”的另一个词。所以他们的输出都是正确的。
就它们如何实际给出输出而言,如果第一个参数为 false,
&&
通常不会计算第二个参数。Conjunction is another word for "and". So they're all correct in terms of their output.
In terms of how they actually give the output,
&&
usually doesn't evaluate the second argument if the first one is false.