[ACCEPTED]-The written versions of the logical operators-logical-operators

Accepted answer
Score: 127

They originated in C in the header <iso646.h>. At 21 the time there were keyboards that couldn't 20 type the required symbols for && (for example), so 19 the header contained #define's that would assist 18 them in doing so, by (in our example) defining 17 and to be &&. Of course, as time went by this 16 became less used.

In C++, they became what 15 are known as alternate tokens. You do not need to include anything 14 to use these tokens in a compliant compiler 13 (as such, the C++-ified version of the C 12 header, <ciso646>, is blank). Alternate tokens are 11 just like regular tokens, except for spelling. So 10 during parsing and is exactly the same as &&, it's just 9 a different way of spelling the same thing.

As 8 for their use: because they are rarely used, using 7 them is often more surprising and confusing 6 than it is helpful. I'm sure if it were 5 normal, they would be much easier to read, but 4 people are so used to && and || anything else 3 just gets distracting.

EDIT: I have seen 2 a very slight increase in their usage since 1 I posted this, however. I still avoid them.

Score: 23

They do exist for usability (character support 22 in keyboard/display flavors) and general 21 readability, but there's another reason 20 that's nowadays more pronounced. Almost 19 none of the answers here, here, or even the main 18 answer here spell out the core reason many of 17 us prefer the word versions over the symbol 16 versions (and a main reason other languages 15 use them): bugs. The differences between 14 the word versions are very visible. The 13 differences between the symbol versions 12 are markedly less so, to the point of tempting 11 bugs to a comparatively much greater extent: "x|y" is 10 very much not "x||y", yet when 9 embedded in a larger expression many of 8 us miss the difference. It's similar to 7 the common accidental mixing of the assignment 6 vs equality operator. For this reason I've 5 weaned myself off of the symbol versions 4 (it wasn't easy) in favor of the word versions. I'd 3 rather have someone do a double-take on 2 them due to our love of old things than 1 tempt bugs.

Score: 11

In C++, they are real keywords. In C, they're 1 macros defined in <iso646.h>. See http://web.archive.org/web/20120123073126/http://www.dinkumware.com/manuals/?manual=compleat&page=iso646.html.

Score: 4

and and && are functionally the same in C++. The 14 and and or operators are truly valid C++ and 13 part of the language standard.

To elaborate 12 on other answers with a concrete example, there 11 is another reason besides just "readability" to 10 prefer and over &&. Spelling out "and" explicitly 9 when a logical AND is what you mean eliminates 8 the possibility of subtle bugs.

Consider this:

int x = 3;
int y = 4;

// Explicitly use "and"
if (x and y) {
    cout << "and: x and y are both non-zero values" << endl;

// Using "&&"
if (x && y) {
    cout << "&&: x and y are both non-zero values" << endl;

// Oops! I meant to type "&&"!
if (x & y) {
    cout << "&: x and y are both non-zero values" << endl;
else {
    cout << "How did I get here?" << endl;

All three 7 if-statements will compile, but the last 6 one means something entirely different and 5 unintended!

If you always use and when you mean 4 logical AND, you can never accidentally 3 type a single "&" and have your code 2 compile successfully and run with mysterious 1 results.

Another good exercise: Try leaving off a character of "and" by accident and see what happens. ;)

Score: 2

Try searching this page for usage of the 13 logical operators. && or || is very easy to 12 find. On the other hand, searching for 11 and or or will give a lot of false positives.

The 10 symbol versions also play more nicely with 9 plain text files. If && appears in a comment 8 or documentation, I immediately grok that 7 the author meant the logical operator. If 6 an and appears in documentation, the text styling 5 probably tells me which is meant. If an 4 and appears in a comment... sorry but word 3 recognition doesn't tell me its function 2 in the comment.

Did you even see that last 1 use of and, without the text styling to help?

More Related questions