103
votes

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?

5
\me Rewrites all his codelmat - Reinstate Monica
in the spirit of "clean code" I'd personally recommend to do away with the habit of writing || and &&, maybe even ! at times. Words are always better then "line noise", not to mention the possible confusion with the bit manipulation operators.Ichthyo
@Ichthyo That isn't always correct. It's way faster to read a lot of symbols and know the meaning of them, than reading a lot of words.vallentin
The irony of saying that and is more readable and then writing "and && or" though :)Ivan Vergiliev
@Ichthyo Have you ever programmed Fortran? In Fortran you'd get your heart's desire: Virtually everything that's a symbol in C/C++/Java is a keyword in Fortran. You get begin, end, contains, pointer, allocatable, extends, etc. I'd run fast in the opposite direction if someone offered me to work with Fortran again, but, hey, maybe that's a task for you? ;-) Honestly, the use of symbols for vital stuff in C/C++/Java is a feature, not a bug. It allows your eyes to quickly recognize a single symbol instead of parsing and interpreting a word instead.cmaster - reinstate monica

5 Answers

121
votes

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.

22
votes

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.

3
votes

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. ;)

2
votes

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?