当前位置:   article > 正文

C++ 学习(三)运算符 - 算术运算符、赋值运算符、比较运算符、逻辑运算符_c++ 赋值与比较

c++ 赋值与比较

1、算术运算符

用于处理四则运算

C++ 算术运算符

运算符说明示例
+

正号

加法 

+5 (正号)

5 + 3 

-

负号

减法

-5 (负号)

5 - 3

*乘法5 * 3
/除法5 / 3(整数类型相除得整数,浮点数类型相除得小数)
%取模(求余数)5 % 3 (整数类型运算,浮点数不能取模运算)
++

前置递增

后置递增

a = 1; b = ++a; // a = 2; b = 2;

a = 1; b = a++; // a = 2; b = 1;

--

前置递减

后置递减

a = 1; b = --a; // a = 0; b = 0;

a = 1; b = a--; // a = 0; b = 1;

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. //算术运算符
  6. int a = 5;
  7. int b = 3;
  8. cout << "a + b = " << a + b << endl;
  9. cout << "a - b = " << a - b << endl;
  10. cout << "a * b = " << a * b << endl;
  11. cout << "整数相除 a / b = " << a / b << endl;
  12. cout << "a % b = " << a % b << endl;
  13. cout << "b % a = " << b % a << endl;
  14. //前置递增:先让变量+1,再进行表达式运算
  15. b = ++a;
  16. cout << "b = ++a;结果a = " << a << ", b = " << b << endl;
  17. //后置递增,先进行表达式运算, 再让变量+1
  18. b = a++;
  19. cout << "b = a++;结果a = " << a << ", b = " << b << endl;
  20. b = --a;
  21. cout << "b = --a;结果a = " << a << ", b = " << b << endl;
  22. b = a--;
  23. cout << "b = a--;结果a = " << a << ", b = " << b << endl;
  24. double c = 5;
  25. double d = 3;
  26. cout << "浮点数相除 c / d = " << c / d << endl;
  27. system("pause");
  28. return 0;
  29. }

输出结果

a + b = 8
a - b = 2
a * b = 15
整数相除 a / b = 1
a % b = 2

b % a = 3
b = ++a;结果a = 6, b = 6
b = a++;结果a = 7, b = 6
b = --a;结果a = 6, b = 6
b = a--;结果a = 5, b = 6
浮点数相除 c / d = 1.66667

Go语言算术运算符

  1. package main
  2. import "fmt"
  3. func main() {
  4. a := 5
  5. b := 3
  6. fmt.Println("a + b = ", a + b)
  7. fmt.Println("a - b = ", a - b)
  8. fmt.Println("a * b = ", a * b)
  9. fmt.Println("整数相除 a / b = ", a / b)
  10. fmt.Println("a % b = ", a % b)
  11. fmt.Println("b % a = ", b % a)
  12. //b = a++ //错误写法,报错:',', ';', new line or '}' expected, got '++'
  13. a++
  14. fmt.Println("a++ = ", a)
  15. b--
  16. fmt.Println("b-- = ", b)
  17. var c float32 = 5
  18. var d float32 = 3
  19. fmt.Println("浮点数相除 c / d = ", c / d)
  20. }

输出结果

a + b =  8
a - b =  2
a * b =  15
整数相除 a / b =  1
a % b =  2
b % a =  3
a++ =  6
b-- =  2
浮点数相除 c / d =  1.6666666

2、赋值运算符

将表达式的值赋值给变量

C++赋值运算符

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. //赋值运算符
  6. int a = 5;
  7. cout << "(a = 5) = " << a << endl;
  8. a += 2;
  9. cout << "(a += 2) = " << a << endl;
  10. a = 5;
  11. a -= 2;
  12. cout << "(a -= 2) = " << a << endl;
  13. a = 5;
  14. a *= 2;
  15. cout << "(a *= 2) = " << a << endl;
  16. a = 5;
  17. a /= 2;
  18. cout << "(a /= 2) = " << a << endl;
  19. a = 5;
  20. a %= 2;
  21. cout << "(a %= 2) = " << a << endl;
  22. system("pause");
  23. return 0;
  24. }

输出结果

(a = 5) = 5
(a += 2) = 7
(a -= 2) = 3
(a *= 2) = 10
(a /= 2) = 2
(a %= 2) = 1

Go语言赋值运算符

上面与C++赋值运算符,且多出几个赋值运算符(加粗标注)

  1. package main
  2. import "fmt"
  3. func main() {
  4. var a int = 5
  5. fmt.Println("(a = 5) = ", a)
  6. a += 2
  7. fmt.Println("(a += 2) = ", a)
  8. a = 5
  9. a -= 2
  10. fmt.Println("(a -= 2) = ", a)
  11. a = 5
  12. a *= 2
  13. fmt.Println("(a *= 2) = ", a)
  14. a = 5
  15. a /= 2
  16. fmt.Println("(a /= 2) = ", a)
  17. a = 5
  18. a %= 2
  19. fmt.Println("(a %= 2) = ", a)
  20. a = 5
  21. a >>= 2
  22. fmt.Println("(a >>= 2) = ", a)
  23. a = 5
  24. a <<= 2
  25. fmt.Println("(a <<= 2) = ", a)
  26. a = 5
  27. a &= 2
  28. fmt.Println("(a &= 2) = ", a)
  29. a = 5
  30. a ^= 2
  31. fmt.Println("(a ^= 2) = ", a)
  32. a = 5
  33. a |= 2
  34. fmt.Println("(a |= 2) = ", a)
  35. }

输出结果

(a = 5) =  5
(a += 2) =  7
(a -= 2) =  3
(a *= 2) =  10
(a /= 2) =  2
(a %= 2) =  1
(a >>= 2) =  1
(a <<= 2) =  20
(a &= 2) =  0
(a ^= 2) =  7
(a |= 2) =  7

3、比较运算符(关系运算符)

表达式比较,返回一个真值或假值

C++比较运算符

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. //比较运算符
  6. int a = 5;
  7. int b = 3;
  8. cout << "(a == b) = " << (a == b) << endl;
  9. cout << "(a != b) = " << (a != b) << endl;
  10. cout << "(a > b) = " << (a > b) << endl;
  11. cout << "(a < b) = " << (a < b) << endl;
  12. cout << "(a >= b) = " << (a >= b) << endl;
  13. cout << "(a <= b) = " << (a <= b) << endl;
  14. system("pause");
  15. return 0;
  16. }

输出结果

(a == b) = 0
(a != b) = 1
(a > b) = 1
(a < b) = 0
(a >= b) = 1
(a <= b) = 0

Go语言比较运算符

与C++比较运算符相同

  1. package main
  2. import "fmt"
  3. func main() {
  4. a := 5
  5. b := 3
  6. fmt.Println("(a == b)", a == b)
  7. fmt.Println("(a != b)", a != b)
  8. fmt.Println("(a > b)", a > b)
  9. fmt.Println("(a < b)", a < b)
  10. fmt.Println("(a >= b)", a >= b)
  11. fmt.Println("(a <= b)", a <= b)
  12. }

输出结果

(a == b) false
(a != b) true
(a > b) true
(a < b) false
(a >= b) true
(a <= b) false

4、逻辑运算符

根据表达式的值返回真值或假值

C++逻辑运算符

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. //逻辑运算符
  6. int a = 5;
  7. int b = 0;
  8. bool c = true;
  9. bool d = false;
  10. cout << "一真一假(a && b) = " << (a && b) << endl;
  11. cout << "两个都真(a && c) = " << (a && c) << endl;
  12. cout << "两个都假(b && d) = " << (b && d) << endl;
  13. cout << "一真一假(a || b) = " << (a || b) << endl;
  14. cout << "两个都真(a || c) = " << (a || c) << endl;
  15. cout << "两个都假(b || d) = " << (b || d) << endl;
  16. cout << "非真(!a) = " << (!a) << endl;
  17. cout << "非假(!b) = " << (!b) << endl;
  18. system("pause");
  19. return 0;
  20. }

 输出结果

一真一假(a && b) = 0
两个都真(a && c) = 1
两个都假(b && d) = 0
一真一假(a || b) = 1
两个都真(a || c) = 1
两个都假(b || d) = 0
非真(!a) = 0
非假(!b) = 1

Go语言逻辑运算符 

与C++逻辑运算符相同

  1. package main
  2. import "fmt"
  3. func main() {
  4. a := true
  5. b := false
  6. c := true
  7. d := false
  8. fmt.Println("一真一假(a && b = )", a && b)
  9. fmt.Println("两个都真(a && c = )", a && c)
  10. fmt.Println("两个都假(b && d = )", b && d)
  11. fmt.Println("一真一假(a || b = )", a || b)
  12. fmt.Println("两个都真(a || c = )", a || c)
  13. fmt.Println("两个都假(b || d = )", b || d)
  14. fmt.Println("非真(!a = )", !a)
  15. fmt.Println("非假(!b = )", !b)
  16. }

输出结果

一真一假(a && b = ) false
两个都真(a && c = ) true
两个都假(b && d = ) false
一真一假(a || b = ) true
两个都真(a || c = ) true
两个都假(b || d = ) false
非真(!a = ) false
非假(!b = ) true

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

闽ICP备14008679号