用于处理四则运算
运算符 | 说明 | 示例 |
---|---|---|
+ | 正号 加法 | +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; |
- #include
- using namespace std;
-
- int main()
- {
- //算术运算符
- int a = 5;
- int b = 3;
-
- cout << "a + b = " << a + b << endl;
- cout << "a - b = " << a - b << endl;
- cout << "a * b = " << a * b << endl;
- cout << "整数相除 a / b = " << a / b << endl;
- cout << "a % b = " << a % b << endl;
- cout << "b % a = " << b % a << endl;
- //前置递增:先让变量+1,再进行表达式运算
- b = ++a;
- cout << "b = ++a;结果a = " << a << ", b = " << b << endl;
- //后置递增,先进行表达式运算, 再让变量+1
- b = a++;
- cout << "b = a++;结果a = " << a << ", b = " << b << endl;
- b = --a;
- cout << "b = --a;结果a = " << a << ", b = " << b << endl;
- b = a--;
- cout << "b = a--;结果a = " << a << ", b = " << b << endl;
-
- double c = 5;
- double d = 3;
- cout << "浮点数相除 c / d = " << c / d << endl;
-
- system("pause");
-
- return 0;
- }
输出结果
a + b = 8
a - b = 2
a * b = 15
整数相除 a / b = 1
a % b = 2b % 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
- package main
-
- import "fmt"
-
- func main() {
- a := 5
- b := 3
- fmt.Println("a + b = ", a + b)
- fmt.Println("a - b = ", a - b)
- fmt.Println("a * b = ", a * b)
- fmt.Println("整数相除 a / b = ", a / b)
- fmt.Println("a % b = ", a % b)
- fmt.Println("b % a = ", b % a)
- //b = a++ //错误写法,报错:',', ';', new line or '}' expected, got '++'
- a++
- fmt.Println("a++ = ", a)
- b--
- fmt.Println("b-- = ", b)
-
- var c float32 = 5
- var d float32 = 3
- fmt.Println("浮点数相除 c / d = ", c / d)
- }
输出结果
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
将表达式的值赋值给变量
- #include
- using namespace std;
-
- int main()
- {
- //赋值运算符
- int a = 5;
- cout << "(a = 5) = " << a << endl;
-
- a += 2;
- cout << "(a += 2) = " << a << endl;
-
- a = 5;
- a -= 2;
- cout << "(a -= 2) = " << a << endl;
-
- a = 5;
- a *= 2;
- cout << "(a *= 2) = " << a << endl;
-
- a = 5;
- a /= 2;
- cout << "(a /= 2) = " << a << endl;
-
- a = 5;
- a %= 2;
- cout << "(a %= 2) = " << a << endl;
-
- system("pause");
-
- return 0;
- }
输出结果
(a = 5) = 5
(a += 2) = 7
(a -= 2) = 3
(a *= 2) = 10
(a /= 2) = 2
(a %= 2) = 1
上面与C++赋值运算符,且多出几个赋值运算符(加粗标注)
- package main
-
- import "fmt"
-
- func main() {
- var a int = 5
- fmt.Println("(a = 5) = ", a)
-
- a += 2
- fmt.Println("(a += 2) = ", a)
-
- a = 5
- a -= 2
- fmt.Println("(a -= 2) = ", a)
-
- a = 5
- a *= 2
- fmt.Println("(a *= 2) = ", a)
-
- a = 5
- a /= 2
- fmt.Println("(a /= 2) = ", a)
-
- a = 5
- a %= 2
- fmt.Println("(a %= 2) = ", a)
-
- a = 5
- a >>= 2
- fmt.Println("(a >>= 2) = ", a)
-
- a = 5
- a <<= 2
- fmt.Println("(a <<= 2) = ", a)
-
- a = 5
- a &= 2
- fmt.Println("(a &= 2) = ", a)
-
- a = 5
- a ^= 2
- fmt.Println("(a ^= 2) = ", a)
-
- a = 5
- a |= 2
- fmt.Println("(a |= 2) = ", a)
- }
输出结果
(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
表达式比较,返回一个真值或假值
- #include
- using namespace std;
-
- int main()
- {
- //比较运算符
- int a = 5;
- int b = 3;
- cout << "(a == b) = " << (a == b) << endl;
- cout << "(a != b) = " << (a != b) << endl;
- cout << "(a > b) = " << (a > b) << endl;
- cout << "(a < b) = " << (a < b) << endl;
- cout << "(a >= b) = " << (a >= b) << endl;
- cout << "(a <= b) = " << (a <= b) << endl;
-
- system("pause");
-
- return 0;
- }
输出结果
(a == b) = 0
(a != b) = 1
(a > b) = 1
(a < b) = 0
(a >= b) = 1
(a <= b) = 0
与C++比较运算符相同
- package main
-
- import "fmt"
-
- func main() {
- a := 5
- b := 3
- fmt.Println("(a == b)", a == b)
- fmt.Println("(a != b)", a != b)
- fmt.Println("(a > b)", a > b)
- fmt.Println("(a < b)", a < b)
- fmt.Println("(a >= b)", a >= b)
- fmt.Println("(a <= b)", a <= b)
- }
输出结果
(a == b) false
(a != b) true
(a > b) true
(a < b) false
(a >= b) true
(a <= b) false
根据表达式的值返回真值或假值
- #include
- using namespace std;
-
- int main()
- {
- //逻辑运算符
- int a = 5;
- int b = 0;
- bool c = true;
- bool d = false;
- cout << "一真一假(a && b) = " << (a && b) << endl;
- cout << "两个都真(a && c) = " << (a && c) << endl;
- cout << "两个都假(b && d) = " << (b && d) << endl;
- cout << "一真一假(a || b) = " << (a || b) << endl;
- cout << "两个都真(a || c) = " << (a || c) << endl;
- cout << "两个都假(b || d) = " << (b || d) << endl;
- cout << "非真(!a) = " << (!a) << endl;
- cout << "非假(!b) = " << (!b) << endl;
-
- system("pause");
-
- return 0;
- }
输出结果
一真一假(a && b) = 0
两个都真(a && c) = 1
两个都假(b && d) = 0
一真一假(a || b) = 1
两个都真(a || c) = 1
两个都假(b || d) = 0
非真(!a) = 0
非假(!b) = 1
与C++逻辑运算符相同
- package main
-
- import "fmt"
-
- func main() {
- a := true
- b := false
- c := true
- d := false
- fmt.Println("一真一假(a && b = )", a && b)
- fmt.Println("两个都真(a && c = )", a && c)
- fmt.Println("两个都假(b && d = )", b && d)
- fmt.Println("一真一假(a || b = )", a || b)
- fmt.Println("两个都真(a || c = )", a || c)
- fmt.Println("两个都假(b || d = )", b || d)
- fmt.Println("非真(!a = )", !a)
- fmt.Println("非假(!b = )", !b)
- }
输出结果
一真一假(a && b = ) false
两个都真(a && c = ) true
两个都假(b && d = ) false
一真一假(a || b = ) true
两个都真(a || c = ) true
两个都假(b || d = ) false
非真(!a = ) false
非假(!b = ) true