&& vs 'and' as well as || vs 'or'

Earlier today in a post I saw a user correct another to use && instead of 'and'
Is there any advantage to this? I didn't want to hijack that thread so I am asking here.

I've had no issue using 'and' in place of && as well as 'or' in place of ||, seems the compiler takes care of that.

Just trying to figure out if I should dismiss this as personal preference or if there is actually a reason for it.

1 Like

I guess it avoids the possibility of only typing & instead of && and | instead of ||.
Although you may think they are equivalent, && permits evaluation short-circuit (which is important to avoid, for example, null pointer dereferencing) but I'm not sure & necessarily would.

1 Like

I guess that was me.

They are equivalent now, but when I learned to program they were not. It's just a force of habit.

It was, and this post is in no way a criticism, just personal understanding :slight_smile:

We need to understand the difference between "bitwise AND (&)" and "Logical AND (&&)" using simple example.

We need to understand the difference between "bitwise OR (|)" and "Logical OR (||)" using simple example.

Make google search.

No offense taken, old (ANSI C) habits die hard.

1 Like

No, I understand those differences. the question is specifically comparing 'and' vs && as well as 'or' vs ||.
Logical unions, not bitwise operations.

Please, explain in this Forum with simple example for the benefit of others.

I would, but that muddies the original question.
This is an apples-to-apples questions. Putting oranges in the bag does not make sense.

2 Likes

I believe the answer is quite obvious - 'and' is much easier to read than '&&'. Actually, some fonts render the pipe '|' very closely to 'I' or 'l' or '1' and any help with distinguishing it in some expression is welcome with me.

I see some doubts posted concerning whether they are equivalent. The alternate keywords like 'and' and 'or' and 'not' are completely equivalent to '&&', '||', and '!'. There is absolutely no syntactic or semantic difference. Except, as they are composed of alphanumeric characters, they must use white space to remain separate from alphanumeric identifiers and keywords.

1 Like

I figured, but thank you for confirming it.
Are these C++ or were they added to the Arduino mini-language?

Indeed, that's why I like them.

They're standard in C++

That clears it all up.
Thanks for your time.

Just for completeness, I was pointing out an error in the same line (using = instead of == in an if statement), then the force of habit kicked in.

1 Like

They're standard in C++

Although it is of minor concern to most Arduino programmers, on some compilers (e.g. MSVC), they are not available out of the box:

Well, Microsoft can sit on it.

3 Likes

Still, it is needed to have clarification with suitable examples that and is indeed equivalent to && and not to &.

BTW, still waiting for a post on headers :slightly_smiling_face:

Consult the language reference.

If you desperately need example, write two functions - one returns true, the other false.
Both print that they have been called.
Put calls to the functions in an if statement with an "and".
If the left-most call returns false, the right-most will not even be called

https://en.cppreference.com/w/cpp/language/operator_alternative

What is the point of examples if you have a language standard that says “and is an alternative token for &&”?

2 Likes