C++:赋值运算符(17)

赋值也就是将后面的值赋值给变量,这里最常用的就是= ,a=1那么a就是1,此外还包含以下的赋值运算

|-----|-----|-------------------|------|
| = | 等于 | int a = 1; a=10 | a=10 |
| += | 加等于 | int a = 1; a+=1; | a=2 |
| -= | 减等于 | int a = 1; a-=1; | a=0 |
| *= | 乘等于 | int a = 2; a*=5; | a=10 |
| /= | 除等于 | int a = 10; a/=2; | a=5 |
| %= | 模等于 | int a = 10; a%=2; | a=0 |


接下来我用不一样的数去演示一遍

加等于

#include<iostream>
using namespace std;

int main()
{
	//	=	等于
	int a = 1;
	a = 10;
	cout << "a = " << a << endl;

	//	+=	加等于
	a += 10;	// a += 10;	== a = a + 10 ;
	cout << "a = " << a << endl;


	system("pause");
	return 0;
}

a += 10; 等于 a = a + 10;


减等于

#include<iostream>
using namespace std;

int main()
{
	//	=	等于
	int a = 1;
	a = 10;
	cout << "a = " << a << endl;

	//	+=	加等于
	a += 10;	// a += 10;	== a = a + 10 ;
	cout << "a = " << a << endl;

	//	-=	减等于
	a = 10;
	a -= 5;		// a -= 5;	== a = a - 5 ;
	cout << "a = " << a << endl;

	system("pause");
	return 0;
}

a -=5 等于 a = a - 5


乘等于

#include<iostream>
using namespace std;

int main()
{
	//	=	等于
	int a = 1;
	a = 10;
	cout << "a = " << a << endl;

	//	+=	加等于
	a += 10;	// a += 10;	== a = a + 10 ;
	cout << "a = " << a << endl;

	//	-=	减等于
	a = 10;
	a -= 5;		// a -= 5;	== a = a - 5 ;
	cout << "a = " << a << endl;

	//	*=	乘等于
	a = 10;
	a *= 10;
	cout << "a = " << a << endl;

	system("pause");
	return 0;
}

a *= 10 等于 a = a * 10


除等于

#include<iostream>
using namespace std;

int main()
{
	//	=	等于
	int a = 1;
	a = 10;
	cout << "a = " << a << endl;

	//	+=	加等于
	a += 10;	// a += 10;	== a = a + 10 ;
	cout << "a = " << a << endl;

	//	-=	减等于
	a = 10;
	a -= 5;		// a -= 5;	== a = a - 5 ;
	cout << "a = " << a << endl;

	//	*=	乘等于
	a = 10;
	a *= 10;
	cout << "a = " << a << endl;

	//	/=	除等于
	a = 10;
	a /= 2;
	cout << "a = " << a << endl;

	system("pause");
	return 0;
}

a /=2 等于 a=a/2


模等于

#include<iostream>
using namespace std;

int main()
{
	//	=	等于
	int a = 1;
	a = 10;
	cout << "a = " << a << endl;

	//	+=	加等于
	a += 10;	// a += 10;	== a = a + 10 ;
	cout << "a = " << a << endl;

	//	-=	减等于
	a = 10;
	a -= 5;		// a -= 5;	== a = a - 5 ;
	cout << "a = " << a << endl;

	//	*=	乘等于
	a = 10;
	a *= 10;
	cout << "a = " << a << endl;

	//	/=	除等于
	a = 10;
	a /= 2;
	cout << "a = " << a << endl;

	//	%=	模等于
	a = 9;
	a %= 2;
	cout << "a = " << a << endl;

	system("pause");
	return 0;
}

a%=2 等于 a=a%2

相关推荐
Ciderw几秒前
MySQL为什么使用B+树?B+树和B树的区别
c++·后端·b树·mysql·面试·golang·b+树
yerennuo6 分钟前
windows第七章 MFC类CWinApp介绍
c++·windows·mfc
齐雅彤8 分钟前
Bash语言的并发编程
开发语言·后端·golang
AitTech16 分钟前
C#性能优化技巧:利用Lazy<T>实现集合元素的延迟加载
开发语言·windows·c#
翻晒时光17 分钟前
深入解析Java集合框架:春招面试要点
java·开发语言·面试
峰子201223 分钟前
B站评论系统的多级存储架构
开发语言·数据库·分布式·后端·golang·tidb
ExRoc41 分钟前
蓝桥杯真题 - 填充 - 题解
c++·算法·蓝桥杯
Channing Lewis1 小时前
python如何使得pdf加水印后的大小尽可能小
开发语言·python·pdf
利刃大大1 小时前
【二叉树的深搜】二叉树剪枝
c++·算法·dfs·剪枝
_.Switch1 小时前
Python Web开发:使用FastAPI构建视频流媒体平台
开发语言·前端·python·微服务·架构·fastapi·媒体