Is there any difference between
if ( a or b or c ) {
...and...
if ( a || b || c ) {
...and more in general between the two operators, even in terms of precedence ?
The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise.
The logical OR ( || ) operator (logical disjunction) for a set of operands is true if and only if one or more of its operands is true. It is typically used with boolean (logical) values. When it is, it returns a Boolean value.
&& is used to perform and operation means if anyone of the expression/condition evaluates to false whole thing is false. || is used to perform or operation if anyone of the expression/condition evaluates to true whole thing becomes true.
The && and || Operators in JavaScript. If applied to boolean values, the && operator only returns true when both of its operands are true (and false in all other cases), while the || operator only returns false when both of its operands are false (and true in all other cases).
Aside from the stylistic sense that makes a lot of seasoned programmers think "Huh, has someone suddenly started writing Pascal?", there is no functional difference.
The purpose of these alternative names is to allow people living in, say, Sweden or Germany, to use a standard Local variant of ASCII, where |
is ö
.
Since the introduction of Unicode and extended ASCII, this need has pretty much disappeared, since nationalized character sets don't have to "steal" less commonly used characters to produce their national "special" characters.
edited You can overload operator or
.
They are the same.
See Operators in C and C++.
If you overload operator or
for a type, you can't then overload operator||
for the same type as the compiler will consider them to be the same function.
This error comes from the following code:
> prog.cpp: In function ‘bool operator||(Type, Type)’:
> prog.cpp:8:6: error: redefinition of ‘bool operator||(Type, Type)’
> prog.cpp:4:6: error: ‘bool operator||(Type, Type)’ previously defined here
On ideaone:
class Type {}
};
bool operator or(Type lhs, Type rhs) {
return true;
}
bool operator ||(Type lhs, Type rhs) {
return false;
}
int main() {
Type a;
Type b;
a or b;
a || b;
}
As per ISO14882:2011(e) 2.6-2 (Table 2) or
is an alternative token for ||
, and as such has exactly the same meaning:
In all respects of the language, each alternative token behaves the same, respectively, as its primary token,except for its spelling.
Often compilers implement them as #define or ||
or the internal equivalent.
Same for and
, bitor
, xor
, compl
, bitand
, and_eq
, or_eq
, xor_eq
, not
and not_eq
.
As was said in the other answers, the two are meant to be the same and thus exchangeable. But there is one big difference: MSVC, C++ Builder and maybe other compilers as well do not support or
without including a special header. That makes using or
a portability obstacle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With