Can some One explain && and || operators for the following code please? [duplicate]
I don't understand why the below code prints 1
.
1 && 0
is not the same as true && false
-> false
?
Why doesn't this print 0
?
#include <iostream>
using namespace std;
int main(){
cout << 1 && 0;
return 0;
}
It's all about Operator Precedence.
The Overloaded Bitwise Left Shift Operator operator<<(std::basic_ostream)
has a higher priority than the Logical AND Operator &&
.
#include <iostream>
int main() {
std::cout << (1 && 0);
return 0;
}
If you are not 146% sure about the priority of an operator, do not hesitate to use brackets. Most modern IDEs will tell you if you don't need to use them.