The written versions of the logical operators

This is the only place I've ever seen and, or and not listed as actual operators in C++. When I wrote up a test program in NetBeans, I got the red underlining as if there was a syntax error and figured the website was wrong, but it is NetBeans which is wrong because it compiled and ran as expected.

I can see ! being favored over not but the readability of and && or seems greater than their grammatical brothers. Why do these versions of the logical operators exist and why does seemingly no one use it? Is this truly valid C++ or some sort of compatibility with C that was included with the language?


Solution 1:

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

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

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

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

Solution 2:

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

Solution 3:

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

Solution 4:

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

To elaborate on other answers with a concrete example, there is another reason besides just "readability" to prefer and over &&. Spelling out "and" explicitly when a logical AND is what you mean eliminates 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 if-statements will compile, but the last one means something entirely different and unintended!

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

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

Solution 5:

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

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

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