赋值也就是将后面的值赋值给变量,这里最常用的就是= ,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