当前位置:   article > 正文

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

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


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

加等于

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. // = 等于
  6. int a = 1;
  7. a = 10;
  8. cout << "a = " << a << endl;
  9. // += 加等于
  10. a += 10; // a += 10; == a = a + 10 ;
  11. cout << "a = " << a << endl;
  12. system("pause");
  13. return 0;
  14. }

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


减等于

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. // = 等于
  6. int a = 1;
  7. a = 10;
  8. cout << "a = " << a << endl;
  9. // += 加等于
  10. a += 10; // a += 10; == a = a + 10 ;
  11. cout << "a = " << a << endl;
  12. // -= 减等于
  13. a = 10;
  14. a -= 5; // a -= 5; == a = a - 5 ;
  15. cout << "a = " << a << endl;
  16. system("pause");
  17. return 0;
  18. }

 a -=5       等于        a = a - 5


乘等于

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. // = 等于
  6. int a = 1;
  7. a = 10;
  8. cout << "a = " << a << endl;
  9. // += 加等于
  10. a += 10; // a += 10; == a = a + 10 ;
  11. cout << "a = " << a << endl;
  12. // -= 减等于
  13. a = 10;
  14. a -= 5; // a -= 5; == a = a - 5 ;
  15. cout << "a = " << a << endl;
  16. // *= 乘等于
  17. a = 10;
  18. a *= 10;
  19. cout << "a = " << a << endl;
  20. system("pause");
  21. return 0;
  22. }

a *= 10        等于        a = a * 10


除等于

 

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. // = 等于
  6. int a = 1;
  7. a = 10;
  8. cout << "a = " << a << endl;
  9. // += 加等于
  10. a += 10; // a += 10; == a = a + 10 ;
  11. cout << "a = " << a << endl;
  12. // -= 减等于
  13. a = 10;
  14. a -= 5; // a -= 5; == a = a - 5 ;
  15. cout << "a = " << a << endl;
  16. // *= 乘等于
  17. a = 10;
  18. a *= 10;
  19. cout << "a = " << a << endl;
  20. // /= 除等于
  21. a = 10;
  22. a /= 2;
  23. cout << "a = " << a << endl;
  24. system("pause");
  25. return 0;
  26. }

a /=2   等于 a=a/2 


模等于

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. // = 等于
  6. int a = 1;
  7. a = 10;
  8. cout << "a = " << a << endl;
  9. // += 加等于
  10. a += 10; // a += 10; == a = a + 10 ;
  11. cout << "a = " << a << endl;
  12. // -= 减等于
  13. a = 10;
  14. a -= 5; // a -= 5; == a = a - 5 ;
  15. cout << "a = " << a << endl;
  16. // *= 乘等于
  17. a = 10;
  18. a *= 10;
  19. cout << "a = " << a << endl;
  20. // /= 除等于
  21. a = 10;
  22. a /= 2;
  23. cout << "a = " << a << endl;
  24. // %= 模等于
  25. a = 9;
  26. a %= 2;
  27. cout << "a = " << a << endl;
  28. system("pause");
  29. return 0;
  30. }

 a%=2   等于 a=a%2 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/383323
推荐阅读
相关标签
  

闽ICP备14008679号