views:

211

answers:

8

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.

A: 

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

J.W.
A: 

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

geofftnz
Edmund
as does "AND" :)
geofftnz
+1  A: 

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
動靜能量
+1  A: 

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

yjerem
A: 

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.

gnovice
+2  A: 

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.

Michael Dunn
+2  A: 

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.

bignose
You're right, but I think the gist of his question was which languages (if any) fall into the latter category and how the behavior is different. I don't think he is interested in a specific programming language. It wouldn't help him much, for example, to ask "In languages X, Y, Z is the behavior different?" and have the answer come back "no", he wants to know whether such languages exist.
A: 

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) {
 // ..
}
Magnus Skog