文章目录

  • 逻辑非
  • 逻辑与
  • 逻辑或

//逻辑运算符
//与(&&)或(||)非(!)
//作用:根据表达式的值返回真值或者假值

逻辑非

#include <iostream>
using namespace std;
int main()
{
	//在 C++ 中,除了 0 都是真
	int a = 10;
	cout << !a << endl;
	cout << !!a << endl;

	system("pause");
	return 0;
}

逻辑与

//逻辑运算符
//与(&&)或(||)非(!)
//作用:根据表达式的值返回真值或者假值 1 为真 0 为假

#include <iostream>
using namespace std;
int main()
{
	//在 C++ 中,除了 0 都是真
	//逻辑与 && 运算
	int a = 10;
	int b = 10;
	cout << (a && b) << endl;

	int a1 = 10;
	int b1 = 0;
	cout << (a1 && b1) << endl;


	system("pause");
	return 0;
}

//两个都是真才为真,有一个假为假

逻辑或

//逻辑运算符
//与(&&)或(||)非(!)
//作用:根据表达式的值返回真值或者假值 1 为真 0 为假

#include <iostream>
using namespace std;
int main()
{
	//在 C++ 中,除了 0 都是真
	//逻辑与 || 运算
	//有一个为真则结果为真
	int a = 10;
	int b = 0;
	cout<<(a || b) << endl;


	system("pause");
	return 0;
}
//只要有一个为真,则表达式为真

更多推荐

C++ 逻辑与或非 逻辑与逻辑或 逻辑非