1. 程式人生 > >資訊學奧賽系列教程:C++邏輯運算子

資訊學奧賽系列教程:C++邏輯運算子

C++中一共有三個邏輯運算子:

1、邏輯與 && 運算子前後兩個條件都為true才為true 

2、邏輯或 ||  運算子前後只要有一個條件為true就為true

3、邏輯非 ! 運算子後的表示式取反,非true為false,非false為true

邏輯運算子運用在條件語句和條件表示式中,以下是測試程式碼:

#include <iostream>
using namespace std;
int main()
{
	int a=15,b=-10;
	bool c,d,e;
	c= ((a>0) && (b>0) && (c==true));
	cout<<"c="<<c<<endl;
	d = ((a>0) || (b>0));
	cout<<"d="<<d<<endl;
	e = !(a>0);
	cout<<"e="<<e<<endl;
 	return 0;
}