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

Apr 13, 2013 at 3:33am
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.
Apr 13, 2013 at 3:52am
"&& 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
Apr 13, 2013 at 4:04am
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/
Apr 13, 2013 at 4:13am
closed account (3CXz8vqX)
...nah, both of you misunderstood.

1
2
3
4
5
6
7
8
9
10
11

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 Apr 13, 2013 at 4:14am
Apr 13, 2013 at 4:25am
I was reading about this the other day, maybe this:
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 "||"
Apr 13, 2013 at 4:29am
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
Apr 13, 2013 at 4:30am
xD ok

What's the difference?

NOTHING

edit:Im using g++ on MAC OS X
Last edited on Apr 13, 2013 at 4:34am
Apr 13, 2013 at 5:00am
> I'm assuming that there are also others
1
2
3
4
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 Apr 13, 2013 at 5:02am
Apr 13, 2013 at 6:32am
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 "&&"
Apr 13, 2013 at 10:12am
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.