术语问题:&&操作员

发布于 2024-11-01 03:48:14 字数 227 浏览 1 评论 0原文

嗯,我读了很多资料,但它们的定义不同:

  • &&是逻辑的逻辑运算符 连词 //我猜这是正确的

  • &&是逻辑 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 技术交流群。

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

发布评论

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

评论(3

与他有关 2024-11-08 03:48:14

在 C#、Java、C++、C 以及其他几种语言中,&& 是布尔 AND 运算符的编程语言实现。它旨在考虑以下事实(不适用于纯命题逻辑):

  • 计算操作数的计算成本可能很高
  • 计算操作数可能会因异常而失败
  • 计算操作数可能会进入无限循环

因此编程中的布尔表达式语言实际上有四种可能的结果:truefalse、“异常”和“无限循环”。在某些情况下,有两个布尔表达式,其中第二个表达式可能成功可以通过查看第一个表达式来确定。例如,使用表达式 foo != nullfoo.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):

  • Evaluating an operand may be computationally expensive
  • Evaluating an operand may fail with an exception
  • Evaluating an operand may enter an infinite loop

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 expressions foo != null and foo.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, and a && b should perhaps rather be phrased as "an expression that returns false if a is false, and the value of b if a is true".

森林散布 2024-11-08 03:48:14

简单的答案。

它对 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 似乎最相关。

我发现自己没有资格批评其中的定义。

12.3.3.23 &&表达式

对于表达式 expr 的形式 expr-first && expr-秒

expr-first 之前 v 的明确赋值状态与 expr 之前 v 的明确赋值状态相同。

• 如果 exprfirst 之后 v 的状态是明确赋值或者“在 true 表达式之后明确赋值”,则 expr-second 之前 v 的明确赋值状态是明确赋值的。否则,它不是明确分配的。

• expr 之后 v 的明确赋值状态由以下公式确定:

<块引用>

o 如果expr-first之后v的状态是肯定赋值的,那么expr之后v的状态肯定是赋值的。

o 否则,如果expr-second之后v的状态是确定赋值的,并且expr-first之后v的状态是“在false表达式之后确定赋值” ,那么expr之后v的状态肯定被赋值。

o 否则,如果expr-second之后v的状态是明确赋值的或者“true表达式之后明确赋值”,则expr之后v的状态是“true表达式之后明确赋值”。

o 否则,如果 v 在 expr-first 之后的状态是“在 false 表达式之后确定赋值”,并且 v 在 expr-second 之后的状态是“在 false 表达式之后确定赋值”,那么 v 在 expr 之后的状态是“错误表达后肯定赋值”。

o 否则,expr之后v的状态不会被明确赋值。

[示例:在下面的代码中

class A
{
    static void F(int x, int y) {
    int i;
    if (x >= 0 && (i = y) >= 0) {
       // i definitely assigned
    }
    else {
        // i not definitely assigned
    }
        // i not definitely assigned
    }
}

变量i被认为是在if语句的一个嵌入语句中明确赋值的,但在另一个中则不然。在方法 F 的 if 语句中,变量 i 在第一个嵌入语句中被明确赋值,因为表达式 (i = y) 的执行始终先于该嵌入语句的执行。相反,变量 i 在第二个嵌入语句中并未明确分配,因为 x >= 0 可能测试为 false,导致变量 i 未被分配。示例结束]

我希望这能澄清问题。

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.

12.3.3.23 && expressions

For an expression expr of the form expr-first && expr-second:

• The definite assignment state of v before expr-first is the same as the definite assignment state of v before expr.

• The definite assignment state of v before expr-second is definitely assigned if the state of v after exprfirst is either definitely assigned or “definitely assigned after true expression”. Otherwise, it is notdefinitely assigned.

• The definite assignment state of v after expr is determined by:

o If the state of v after expr-first is definitely assigned, then the state of v after expr is definitely assigned.

o Otherwise, if the state of v after expr-second is definitely assigned, and the state of v after expr-first is “definitely assigned after false expression”, then the state of v after expr is definitely assigned.

o Otherwise, if the state of v after expr-second is definitely assigned or “definitely assigned after true expression”, then the state of v after expr is “definitely assigned after true expression”.

o Otherwise, if the state of v after expr-first is “definitely assigned after false expression”, and the state of v after expr-second is “definitely assigned after false expression”, then the state of v after expr is “definitely assigned after false expression”.

o Otherwise, the state of v after expr is not definitely assigned.

[Example: In the following code

class A
{
    static void F(int x, int y) {
    int i;
    if (x >= 0 && (i = y) >= 0) {
       // i definitely assigned
    }
    else {
        // i not definitely assigned
    }
        // i not definitely assigned
    }
}

the variable i is considered definitely assigned in one of the embedded statements of an if statement but not in the other. In the if statement in method F, the variable i is definitely assigned in the first embedded statement because execution of the expression (i = y) always precedes execution of this embedded statement. In contrast, the variable i is not definitely assigned in the second embedded statement, since x >= 0 might have tested false, resulting in the variable i’s being unassigned. end example]

I hope that clears things up.

夜夜流光相皎洁 2024-11-08 03:48:14

连词是“和”的另一个词。所以他们的输出都是正确的。

就它们如何实际给出输出而言,如果第一个参数为 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.

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