使用AND或&&有什么好处?

时间:2023-01-13 16:12:41

Currently, I'm using && and || instead of AND and OR because that's how I was taught. In most languages, however, both are valid syntax. Are there any advantages to one or the other in any language?

目前,我正在使用&&和||而不是AND和OR,因为这就是我的教导方式。但是,在大多数语言中,两者都是有效的语法。用任何语言对其中一个有什么好处吗?

I did try to search for this question, but it's a bit hard. It doesn't interpret my input correctly.

我确实试图搜索这个问题,但这有点难。它没有正确解释我的输入。

8 个解决方案

#1


You ask “Are there any advantages to one or the other in any language?” This is, of course, dependent on the programming language.

你问“用任何语言对一个或那个有什么好处吗?”这当然取决于编程语言。

Languages that implement both and and && (and correspondingly or and ||) will do it one of two ways:

实现两者和&&(以及相应的或和||)的语言将采用以下两种方式之一:

  • Both behave exactly the same way. In which case, there is no advantage provided by the language in using one over the other.

    两者的行为方式完全相同。在这种情况下,语言在使用一种语言时没有优势。

  • Each behaves differently. In which case, the advantage is that you can get different behaviour by using one or the other.

    每种行为都不同。在这种情况下,优点是您可以通过使用其中一种来获得不同的行为。

That all sounds a bit facetious, but it's really as specific as one can get without talking about a specific language. Your question explicitly wants to know about all languages, but it's a question that needs to be answered per language.

这一切听起来都有点滑稽,但它真的具体,就像没有谈论特定语言一样。您的问题明确地想知道所有语言,但这是一个需要根据语言回答的问题。

#2


Perl has all four of {&& || and or} but they differ in their precedence. "and" and "or" have really low precedence so you can do things like "complex-function-call-here or die $!" and you won't accidentally have "or" slurp up something on its left side that you didn't want it to.

Perl有{&& ||的全部四个或者}或者它们的优先级不同。 “和”和“或”的优先级非常低,因此您可以执行“复杂功能 - 调用此处或死亡!”等操作。并且你不会意外地“或”在他的左侧啜饮你不想要它的东西。

#3


it depends on the language, but on PHP, I'd be careful about using && versus "and". The ones i often use are "&&" and "||"

这取决于语言,但在PHP上,我会小心使用&&与“和”。我经常使用的是“&&”和“||”

http://us3.php.net/manual/en/language.operators.logical.php

$g = true && false; // $g will be assigned to (true && false) which is false
$h = true and false; // $h will be assigned to true

#4


In some languages && will have a higher operator precedence than AND.

在某些语言中,&&的运算符优先级高于AND。

#5


If both works fine, then I would say it's really personal preference, in most cases, they are compiled into same binary code like this : 11100010001000101001001010 [not real code, just an example].

如果两者都工作正常,那么我会说它真的是个人偏好,在大多数情况下,它们被编译成相同的二进制代码,如下所示:11100010001000101001001010 [不是真正的代码,只是一个例子]。

#6


&& = two keystrokes of the same key. AND = three keystrokes of different keys.

&& =同一个键的两次击键。 AND =不同键的三次击键。

#7


I'm not sure what language you are using, but some languages differentiate between a normal boolean operator and a short-circuit operator. For example, the following are normal boolean operators in MATLAB:

我不确定您使用的是哪种语言,但有些语言区分普通布尔运算符和短路运算符。例如,以下是MATLAB中的普通布尔运算符:

C = or(A,B);
C = A | B;  % Exactly the same as above

However, this is a short-circuit operator:

但是,这是一个短路运营商:

C = A || B;

The short-circuit syntax will evaluate the first argument and then, depending on the value, will potentially skip over evaluating the second argument. For example, if A is already true, B doesn't have to be evaluated for an OR operation, since the result is guaranteed to be true. This is helpful when B is replaced with a logical operation that involves some kind of expensive computation.

短路语法将评估第一个参数,然后根据值,可能会跳过评估第二个参数。例如,如果A已经为真,则不必对OR进行OR运算的评估,因为结果保证为真。当B被替换为涉及某种昂贵计算的逻辑操作时,这很有用。

Here's a wikipedia link discussing short-circuit operators and their syntax for a few languages.

这是一个*链接,讨论短路运算符及其几种语言的语法。

#8


Unless there aren't any precedence issues, I'd say there are differences in readability. Consider the following:

除非没有任何优先级问题,否则我会说可读性存在差异。考虑以下:

if (&x == &y && &y == &z) {
    // ..
}

#define AND &&
if (&x == &y AND &y == &z) {
    // ..
}

#1


You ask “Are there any advantages to one or the other in any language?” This is, of course, dependent on the programming language.

你问“用任何语言对一个或那个有什么好处吗?”这当然取决于编程语言。

Languages that implement both and and && (and correspondingly or and ||) will do it one of two ways:

实现两者和&&(以及相应的或和||)的语言将采用以下两种方式之一:

  • Both behave exactly the same way. In which case, there is no advantage provided by the language in using one over the other.

    两者的行为方式完全相同。在这种情况下,语言在使用一种语言时没有优势。

  • Each behaves differently. In which case, the advantage is that you can get different behaviour by using one or the other.

    每种行为都不同。在这种情况下,优点是您可以通过使用其中一种来获得不同的行为。

That all sounds a bit facetious, but it's really as specific as one can get without talking about a specific language. Your question explicitly wants to know about all languages, but it's a question that needs to be answered per language.

这一切听起来都有点滑稽,但它真的具体,就像没有谈论特定语言一样。您的问题明确地想知道所有语言,但这是一个需要根据语言回答的问题。

#2


Perl has all four of {&& || and or} but they differ in their precedence. "and" and "or" have really low precedence so you can do things like "complex-function-call-here or die $!" and you won't accidentally have "or" slurp up something on its left side that you didn't want it to.

Perl有{&& ||的全部四个或者}或者它们的优先级不同。 “和”和“或”的优先级非常低,因此您可以执行“复杂功能 - 调用此处或死亡!”等操作。并且你不会意外地“或”在他的左侧啜饮你不想要它的东西。

#3


it depends on the language, but on PHP, I'd be careful about using && versus "and". The ones i often use are "&&" and "||"

这取决于语言,但在PHP上,我会小心使用&&与“和”。我经常使用的是“&&”和“||”

http://us3.php.net/manual/en/language.operators.logical.php

$g = true && false; // $g will be assigned to (true && false) which is false
$h = true and false; // $h will be assigned to true

#4


In some languages && will have a higher operator precedence than AND.

在某些语言中,&&的运算符优先级高于AND。

#5


If both works fine, then I would say it's really personal preference, in most cases, they are compiled into same binary code like this : 11100010001000101001001010 [not real code, just an example].

如果两者都工作正常,那么我会说它真的是个人偏好,在大多数情况下,它们被编译成相同的二进制代码,如下所示:11100010001000101001001010 [不是真正的代码,只是一个例子]。

#6


&& = two keystrokes of the same key. AND = three keystrokes of different keys.

&& =同一个键的两次击键。 AND =不同键的三次击键。

#7


I'm not sure what language you are using, but some languages differentiate between a normal boolean operator and a short-circuit operator. For example, the following are normal boolean operators in MATLAB:

我不确定您使用的是哪种语言,但有些语言区分普通布尔运算符和短路运算符。例如,以下是MATLAB中的普通布尔运算符:

C = or(A,B);
C = A | B;  % Exactly the same as above

However, this is a short-circuit operator:

但是,这是一个短路运营商:

C = A || B;

The short-circuit syntax will evaluate the first argument and then, depending on the value, will potentially skip over evaluating the second argument. For example, if A is already true, B doesn't have to be evaluated for an OR operation, since the result is guaranteed to be true. This is helpful when B is replaced with a logical operation that involves some kind of expensive computation.

短路语法将评估第一个参数,然后根据值,可能会跳过评估第二个参数。例如,如果A已经为真,则不必对OR进行OR运算的评估,因为结果保证为真。当B被替换为涉及某种昂贵计算的逻辑操作时,这很有用。

Here's a wikipedia link discussing short-circuit operators and their syntax for a few languages.

这是一个*链接,讨论短路运算符及其几种语言的语法。

#8


Unless there aren't any precedence issues, I'd say there are differences in readability. Consider the following:

除非没有任何优先级问题,否则我会说可读性存在差异。考虑以下:

if (&x == &y && &y == &z) {
    // ..
}

#define AND &&
if (&x == &y AND &y == &z) {
    // ..
}