三、C++运算符(2)算数运算符

一、递增递减

#define _CRT_SECURE_NO_WARNINGS
#include
#include
using namespace std;
int main()
{
	//1、前置递增
	int a = 10;
	++a;	//让变量+1
	cout << "a=" << a << endl;

	//2、后置递增
	int b = 10;
	b++;
	cout << "b=" << b << endl;

	//3、前置和后置的区别
	//前置递增,先让变量+1,然后进行表达式运算
	int a2 = 10;
	int b2 = ++a2 * 10;
	cout << "a2=" << a2 << endl;	//11
	cout << "b2=" << b2 << endl;	//110

	//后置递增,先进行表达式运算,后让变量+1
	int a3 = 10;
	int b3 = a3++ * 10;
	cout << "a3=" << a3 << endl;	//然后10++变为11
	cout << "b3=" << b3 << endl;	//10先运算100

	//4、后置递减同理

	system("pause");

	return 0;
}

运行结果:

a=11

b=11

a2=11

b2=110

a3=11

b3=100

你可能感兴趣的:(C++,后端,学习,考研,算法,c++,开发语言,后端)