Question

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?

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

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

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

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].

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

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:

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.

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

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) {
    // ..
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top