Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "or" and "||"

Tags:

c++

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 ?

like image 373
ElementalStorm Avatar asked Feb 22 '13 11:02

ElementalStorm


People also ask

Does || MEAN and or or?

The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise.

What is || used for?

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.

What is the difference between && || operators and and or?

&& 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.

Is || and && the same?

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


4 Answers

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.

like image 114
Mats Petersson Avatar answered Oct 04 '22 18:10

Mats Petersson


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;
}
like image 42
Peter Wood Avatar answered Oct 04 '22 16:10

Peter Wood


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.

like image 21
PlasmaHH Avatar answered Oct 04 '22 18:10

PlasmaHH


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.

like image 38
Arne Mertz Avatar answered Oct 04 '22 17:10

Arne Mertz