### && and || or what's the difference?

closed account (3CXz8vqX)
Hi,

I've noticed other people using "and" in place of &&, I'm wondering what the difference is. I can see the advantages of the former and I'd like to move to it, but I can't seem to find neither hide nor hare of it in the cplusplus tuts.
"&& and || or what's the difference?"

&& (logical AND) operator truth table
false&&false=false
false&&true=false
true&&false=false
true&&true=true

|| (logical OR) operator truth table
false||false=false
false||true=true
true||false=true
true||true=true
Just to elaborate on the above, in simpler terms:

The or operator (||) will evaluate if one or more of the variables are true. The and operator (&&) will evaluate if all of the variables are true.

For a more in depth explanation, see: http://www.learncpp.com/cpp-tutorial/36-logical-operators/
closed account (3CXz8vqX)
...nah, both of you misunderstood.

 ``1234567891011`` `````` int a = 5, b = 5; if( a == 5 && b == 5 ) return 1; if( a == 2 and b == 2 ) return 2; if( a == 5 or b ==3 ) return 3; //All are apparently valid though I'm not sure what the differences are. return 0; ``````

Those are the and/or's I mean. I get the logical operators already, but it's just the syntax I'm unfamiliar with. I'm assuming that there are also others, perhaps 'not' instead of '!' ... >.>;
Last edited on
http://www.cplusplus.com/reference/ciso646/

I don't think there is any functional difference, though I do think "or" is a lot more readable than "||"
When I first started learning c++, we were discouraged from using the 'and' or 'or' instead were told to use '&&' and '||'. It was only recently that I found out you can actually type 'and' without getting an error
xD ok

What's the difference?

NOTHING

edit:Im using g++ on MAC OS X
Last edited on
> I'm assuming that there are also others
 ``1234`` ``````and or not //logical bitor bitand xor compl //bit operations and_eq or_eq xor_eq //assignment (bit operations) not_eq //comparison != ``````

http://www.cplusplus.com/reference/ciso646/
> the inclusion of this header has no effect in C++,
Last edited on
The rationale for it being discouraged is because it's not widely accepted by developers as the way to write code. But you would need to consult the coding standard your organisation uses. I have yet to work in an organisation that would accept "and" over "&&"
The ciso646 defines macros, but the note says:
"In C++, reserved words exist and are treated as aliases of their respective operator."

C had logical operators and C++ did inherit them. Someone thought that it would be neat to be able to write (A and B) instead of (A && B). Macros and preprocessor make that possible. Compiler with extensions makes that possible. Standard that adds keywords makes that possible.

Being possible may make someone happy, but it does not mean that everybody will change their habits (and revise tutorials), particularly when && has been fine the whole time and still is.
Topic archived. No new replies allowed.