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
原文地址:https://blog.csdn.net/w14768855/article/details/137392242
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!