30

Any pros/cons to using the "and" operator vs the && operator? I personally think "and" is only going to cause confusion (ironically).

If there aren't any differences, why does it exist? It seems silly and unnecessary.

you786
  • 3,659
  • 5
  • 48
  • 74
  • 2
    Nope, no difference. Choose which one you want (but keep it consistent). – David G Feb 06 '13 at 23:29
  • This is a matter of some debate and not a good fit for StackOverflow. See also http://stackoverflow.com/q/1103313/78845 and http://stackoverflow.com/q/1703979/78845 (personally, I like the iso646 spellings of these operators). – johnsyweb Feb 06 '13 at 23:33
  • 3
    Have +1 for a geek ice breaker. – Tony Hopkinson Feb 06 '13 at 23:48
  • 1
    Well, it is not as bad as trigraphs ;) You'd better `#include ` if you want your code to be portable. – Hans Passant Feb 06 '13 at 23:49
  • It really does make sense when your code says `if (a öö b)` to instead use `if (a or b)`. But if your ´||´ actually looks like it should (which it does if you are using a PC or something else produced in the last 20 or so years) - the problem originates in the 128-character sets used in for example Sweden, Norway or Germany, which "stole" the "unusual" characters of []\ and {}| to make ÄÅÖ and äåö respectively, and I think ^ is ü, but I can't remember exactly. – Mats Petersson Feb 06 '13 at 23:55
  • 1
    Visual studio won't understand `and` ... – germannp Feb 28 '18 at 12:35

2 Answers2

31

It's the same operator. The difference is merely one of style. Consult your project documentation, or ask your boss, or your wife, or flip a coin.

user207421
  • 305,947
  • 44
  • 307
  • 483
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 11
    I asked my wife, who just happens to be an english major. She prefers "and". I suppose that's as good a reason as any. – HackySchmacky Oct 06 '20 at 21:58
  • 1
    I asked my boss, who just happens to be a lawyer. She prefers "&&", because "and" is used by plebs. –  Sep 19 '21 at 23:43
20

They're anachronisms - they were originally introduced to accomodate folks who didn't have "^" or "|" characters on their keyboards.

Furthermore, although "and" and "&&" are equivalent ... "and" and "&" are quite different. Using "and" instead of "&&" is simply confusing on a number of different levels, for several different reasons. Including giving the poor maintenance programmer a completely unnecessary "wtf?" experience.

I would not use them in any code. And I've certainly never seen them used in any "live" code.

IMHO...

Here's a bit more on the topic, if you're interested:

santa
  • 983
  • 9
  • 30
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • 5
    Why would you *not* use them in any code? – juanchopanza Feb 06 '13 at 23:38
  • 11
    Same reasonsing as me I should think. I've been doing C & C++ on and off for 15 years, and I didn't even know they existed. You don't need comprehension failures around something this basic, things are hard enough. – Tony Hopkinson Feb 06 '13 at 23:46
  • 2
    I don't understand why people always complain for the bitwise operator for this topic. "and" and "&&" as different as "bitand" and "&" What's up with that? Don't understand this part of your answer, please enlighten me – user3063349 Dec 07 '16 at 14:12
  • 7
    I very much disagree with the mentality of not using something just because it is new and different. Not only would I use the `and` operator, I would actually prefer it over `&&` for more reasons than just readability. See my answer to a similar question here: https://stackoverflow.com/a/54185388/3964397 – tjwrona1992 Jan 14 '19 at 18:46