C++的運算符你真的瞭解嗎

前言

運算符的作用:用於執行代碼的運算

主要有:

在這裡插入圖片描述

1 算術運算符

用於處理四則運算

在這裡插入圖片描述

對於前置遞增:將遞增運算前置,使變量先加一,再進行表達式運算。

對於後置遞增:將遞增運算後置,使變量先進行表達式運算,再加一。

#include<iostream>
using namespace std;
int main()
{
	//1.前置遞增:先加一,再進行表達式運算
	int a = 10;
	int b = ++a * 10;
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	//2.後置遞增:先進行表達式運算,再加一
	int c = 10;
	int d = c++ * 10;
	cout << "c = " << c << endl;
	cout << "d = " << d << endl;
	system("pause");
	return 0;
}

2 賦值運算符

在這裡插入圖片描述

#include<iostream>
using namespace std;
int main1()
{
	//賦值運算符
	int a = 10;
	int b = 2;
	cout << "a = " << a << endl;
	//+=
	a = 10;
	a += b;
	cout << "a = " << a << endl;
	//-=
	a = 10;
	a -= b;
	cout << "a = " << a << endl;
	//*=
	a = 10;
	a *= b;
	cout << "a = " << a << endl;
	// /=
	a = 10;
	a /= b;
	cout << "a = " << a << endl;
	// %=
	a = 10;
	a %= b;
	cout << "a = " << a << endl;
	system("pause");
	return 0;
}

3 比較運算符

在這裡插入圖片描述

#include<iostream>
using namespace std;
int main()
{
	cout << (4 == 3) << endl;
	cout << (4 != 3) << endl;
	cout << (4 < 3) << endl;
	cout << (4 > 3) << endl;
	cout << (4 >= 3) << endl;
	cout << (4 <= 3) << endl;
	system("pause");
	return 0;
}

4 邏輯運算符

在這裡插入圖片描述

#include<iostream>using namespace std;int main(){int a = 5;// 邏輯運算符 非cout << !a << endl;cout << !!a << endl;// 邏輯運算符 與int b = 0;int c = 3;cout << (a && b) << endl;cout << (a && c) << endl;//邏輯運算符  或cout << (!a || b) << endl;cout << (a || c) << endl;system("pause");return 0;}#include<iostream>
using namespace std;
int main()
{
	int a = 5;
	// 邏輯運算符 非
	cout << !a << endl;
	cout << !!a << endl;
	// 邏輯運算符 與
	int b = 0;
	int c = 3;
	cout << (a && b) << endl;
	cout << (a && c) << endl;
	//邏輯運算符  或
	cout << (!a || b) << endl;
	cout << (a || c) << endl;
	system("pause");
	return 0;
}

總結

本篇文章就到這裡瞭,希望能夠給你帶來幫助,也希望您能夠多多關註WalkonNet的更多內容!     

推薦閱讀: