手把手帶你學習C++的運算符

運算符

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

運算符類型

作用

算術運算符 用於處理四則運算
賦值運算符 用於將表達式的值賦給變量
比較運算符 用於表達式的比較,並返回一個真值或假值
邏輯運算符 用於根據表達式的值返回真值或假值

01 算術運算符

運算符 術語 示例 結果
+ 正號 +3 3
負號 -3 -3
+ 10+5 15
10-5 5
* 10*5 50
/ 10/5 2
% 取模/取餘 10%3 1
++ 前置遞增 a=2;b=++a; a=3;b=3;
++ 後置遞增 a=2;b=a++; a=3;b=2;
前置遞減 a=2;b=–a; a=1;b=1;
後置遞增 a=2;b=a–; a=1;b=2;
#include<iostream>
using namespace std;
int main1()
{
	//加減乘除
	int a1 = 10;
	int b1 = 3;
	cout << a1 + b1 << endl;
	cout << a1 - b1 << endl;
	cout << a1 * b1 << endl;
	cout << a1 / b1 << endl; //兩個整數相除,結果依然是整數,將小數部分去除
	int a2 = 10;
	int b2 = 20;
	cout << a2 / b2 << endl;
	int a3 = 10;
	int b3 = 0;
	//cout << a3 / b3 << endl; //錯誤,除數不可以為0
	double x1 = 0.5;
	double x2 = 0.22;
	cout << x1 / x2 << endl;
	//取模
	int c1 = 10;
	int d1 = 3;
	cout << c1 % d1 << endl;
	int c2 = 10;
	int d2 = 20;
	cout << c2 % d2 << endl;
	int c3 = 10;
	int d3 = 0;
	//cout << c3 % d3 << endl; //錯誤,取模除數不可以為0
	double x3 = 3.14;
	double x4 = 1.1;
	//cout << x3 % x4 << endl; //錯誤,不支持小數取模運算
	//1.前置遞增
	int m = 10;
	++m;
	cout << "m = " << m << endl;
	//2.後置遞增
	int n = 10;
	n++;
	cout << "n = " << n << endl;
	//3.前置和後置的區別
	//前置遞增:先讓變量+1,後進行表達式運算
	int m2 = 10;
	int n2 = ++m2 * 10;
	cout << "m2 = " << m2 << endl;
	cout << "n2 = " << n2 << endl;
	//後置遞增:先進行表達式運算,後讓變量+1
	int m3 = 10;
	int n3 = m3++ * 10;
	cout << "m3 = " << m3 << endl;
	cout << "n3 = " << n3 << endl;
	system("pause");
	return 0;
}

02 賦值運算符

運算符 術語 示例 結果
= 賦值 a=2;b=3; a=2;b=3;
+= 加等於 a=0;a+=2; a=2;
-= 減等於 a=5;a-=3;

a=2;

*= 乘等於 a=2;a*=2;

a=4;

/= 除等於 a=4;a/=2; a=2;
%= 模等於 a=3;a%=2; a=1;
#include<iostream>
using namespace std;
int main2()
{
	//賦值運算符
	int a = 10;
	cout << "a = " << a << endl; //10
	// +=
	a += 2;
	cout << "a = " << a << endl; //12
	// -=
	a = 10;
	a -= 2;
	cout << "a = " << a << endl; //8
	// *=
	a = 10;
	a *= 2;
	cout << "a = " << a << endl; //20
	// /=
	a = 10;
	a /= 2;
	cout << "a = " << a << endl; //5
	// %=
	a = 10;
	a %= 2;
	cout << "a = " << a << endl; //0
	system("pause");
	return 0;
}

03 比較運算符

運算符術語示例結果==相等於4==30!=不等於4!=31<小於4<30>大於4>31<=小於等於4<=30>=大於等於4>=31

#include<iostream>
using namespace std;
int main3()
{
	//比較運算符
	// ==
	int a = 10;
	int b = 20;
	cout << (a == b) << endl;
	// !=
	cout << (a != b) << endl;
	// >
	cout << (a > b) << endl;
	// <
	cout << (a < b) << endl;
	// >=
	cout << (a >= b) << endl;
	// <=
	cout << (a <= b) << endl;
	system("pause");
	return 0;
}

04 邏輯運算符

運算符術語示例結果!非!a如果a為假,則!a為真;如果a為真,則!a為假。&&與a&&b如果a和b都為真,則結果為真,否則為假。||或a||b如果a或b有一個為真,則結果為真;二者都為假時,結果為假。

#include<iostream>
using namespace std;
int main4()
{
	//邏輯運算符 非 !
	int a = 10;
	cout << !a << endl; //0
	cout << !!a << endl; //1
	//邏輯運算符 與 &&
	int c = 10;
	int d = 10;
	cout << (c && d) << endl; // 1
	c = 0;
	d = 10;
	cout << (c && d) << endl; // 0
	c = 10;
	d = 0;
	cout << (c && d) << endl; // 0
	c = 0;
	d = 0;
	cout << (c && d) << endl; // 0
	//邏輯運算符 或 ||
	int e = 10;
	int f = 10;
	cout << (e || f) << endl; // 1
	e = 0;
	f = 10;
	cout << (e || f) << endl; // 1
	e = 10;
	f = 0;
	cout << (e || f) << endl; // 1
	e = 0;
	f = 0;
	cout << (e || f) << endl; // 0
	system("pause");
	return 0;
}

總結

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

推薦閱讀: