逻辑运算符

This commit is contained in:
flykhan 2023-06-19 15:08:08 +08:00
parent b1523467f8
commit 1a98dde7d5
3 changed files with 39 additions and 0 deletions

14
day6/d3.cpp Normal file
View File

@ -0,0 +1,14 @@
// 逻辑与短路
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
int a = 3, b = 4;
int c = (a<b) && (--a); // a自减1, a = a-1 => a = 2, c = 1
int d = (a>b) && (a++); // a++不会执行 => a = 2, d = 0
cout << a << "," << b << endl; // 2, 4
cout << c << "," << d << endl; // 1, 0
}

13
day6/d4.cpp Normal file
View File

@ -0,0 +1,13 @@
// 逻辑或短路
#include <iostream>
using namespace std;
int main()
{
int a = 4, b = 2;
a += b; // a = 6
a = (a >= b) || b++; // 6 >= 2 =>1 || b++ 右侧的b++不会执行
cout << a << "," << b << endl; // 1,2
return 0;
}

12
day6/d5.cpp Normal file
View File

@ -0,0 +1,12 @@
// 逻辑非测试
#include <iostream>
using namespace std;
int main()
{
int a, b;
cin >> a >> b; // 3 4
cout << "!(a > b) = " << !(a > b) << endl; // !0 = 1
return 0;
}