逻辑运算符
逻辑与 &&
逻辑或 ||
逻辑非 !
以上三种逻辑运算符,均有代名,等价于
逻辑与 and
逻辑或 or
逻辑非 not
这两种版本的用法是可以互相替代的,使用上没有任何差别,你习惯用什么,就可以用什么。
之所以要使用and,or,not来代替原本的符号,从原始意义上来说,并非是为了简化使用,或者让程序更清晰,仅仅是为了替代某些字符集中可能不包含的符号,在这里也就是|。其中!和&并非属于不常用的符号,但是为了完整性,最终也是采用了and和not的替代方式。
另外,在优先级上,not的优先级是最高的,其次是and,最后才是or,这一点一定要注意。
#include <iostream>
using namespace std;
int main()
{
int a = 1;
int b = 1;
int c = 0;
// a or b and c
// 先执行b and c,结果为false
// 再执行a or false,结果为true
bool d = a or b and c;
// (a or b) and c
// 先执行a or b,结果为true
// 再执行true and c,结果为false
bool e = (a or b) and c;
cout <<