4. 运算符与表达式——编程世界的“数学魔法”
4.1 什么是运算符和表达式
在 C++ 里,运算符就像是数学里的运算符号,能对数据进行各种操作;而表达式则是由运算符和操作数(也就是变量或者常量)组合而成的式子。就好比在数学中,“3 + 5” 是一个表达式,“+” 就是运算符。在编程里,我们也可以用类似的方式进行计算和操作。
4.2 算术运算符
算术运算符用于进行基本的数学运算,常见的算术运算符有:
运算符 | 描述 | 示例 |
---|---|---|
+ | 加法 | int sum = 3 + 5; // sum 的值为 8 |
– | 减法 | int diff = 7 - 2; // diff 的值为 5 |
* | 乘法 | int product = 4 * 6; // product 的值为 24 |
/ | 除法 | int quotient = 10 / 2; // quotient 的值为 5 注意:如果是两个整数相除,结果会取整,比如 7 / 2 的结果是 3 |
% | 取模(求余数) | int remainder = 7 % 2; // remainder 的值为 1 |
以下是一个简单的程序示例,展示了算术运算符的使用:
#include <iostream> using namespace std; int main() { int a = 10, b = 3; int sum = a + b; int diff = a - b; int product = a * b; int quotient = a / b; int remainder = a % b; cout << "和: " << sum << endl; cout << "差: " << diff << endl; cout << "积: " << product << endl; cout << "商: " << quotient << endl; cout << "余数: " << remainder << endl; return 0; }
4.3 比较运算符
比较运算符用于比较两个值的大小关系,结果是一个布尔值(true
或 false
)。常见的比较运算符有:
运算符 | 描述 | 示例 |
---|---|---|
== | 等于 | bool isEqual = (5 == 5); // isEqual 的值为 true |
!= | 不等于 | bool isNotEqual = (3 != 7); // isNotEqual 的值为 true |
> | 大于 | bool isGreater = (8 > 3); // isGreater 的值为 true |
< | 小于 | bool isLess = (2 < 6); // isLess 的值为 true |
>= | 大于等于 | bool isGreaterOrEqual = (5 >= 5); // isGreaterOrEqual 的值为 true |
<= | 小于等于 | bool isLessOrEqual = (4 <= 7); // isLessOrEqual 的值为 true |
下面的程序展示了比较运算符的使用:
#include <iostream> using namespace std; int main() { int x = 10, y = 5; bool result1 = (x == y); bool result2 = (x != y); bool result3 = (x > y); bool result4 = (x < y); bool result5 = (x >= y); bool result6 = (x <= y); cout << "x 等于 y: " << (result1 ? "true" : "false") << endl; cout << "x 不等于 y: " << (result2 ? "true" : "false") << endl; cout << "x 大于 y: " << (result3 ? "true" : "false") << endl; cout << "x 小于 y: " << (result4 ? "true" : "false") << endl; cout << "x 大于等于 y: " << (result5 ? "true" : "false") << endl; cout << "x 小于等于 y: " << (result6 ? "true" : "false") << endl; return 0; }
4.4 逻辑运算符
逻辑运算符用于对布尔值进行操作,常见的逻辑运算符有:
运算符 | 描述 | 示例 |
---|---|---|
&& | 逻辑与,只有当两个操作数都为 true 时,结果才为 true | bool result = (true && false); // result 的值为 false |
|| | 逻辑或,只要有一个操作数为 true ,结果就为 true | bool result = (true || false); // result 的值为 true |
! | 逻辑非,对操作数取反 | bool result = !true; // result 的值为 false |
以下程序展示了逻辑运算符的使用:
#include <iostream> using namespace std; int main() { bool a = true, b = false; bool andResult = a && b; bool orResult = a || b; bool notResult = !a; cout << "a 与 b: " << (andResult ? "true" : "false") << endl; cout << "a 或 b: " << (orResult ? "true" : "false") << endl; cout << "非 a: " << (notResult ? "true" : "false") << endl; return 0; }
4.5 运算符优先级
就像在数学中,乘法和除法的优先级高于加法和减法一样,在 C++ 里,不同的运算符也有不同的优先级。当一个表达式中有多个运算符时,会按照优先级的顺序进行计算。一般来说,算术运算符的优先级较高,然后是比较运算符,最后是逻辑运算符。可以使用括号来改变运算的顺序,就像在数学中用括号来明确先计算哪一部分一样。
例如:
#include <iostream> using namespace std; int main() { int result = 3 + 5 * 2; // 先计算乘法,再计算加法,结果为 13 int result2 = (3 + 5) * 2; // 使用括号改变顺序,先计算加法,再计算乘法,结果为 16 cout << "第一个结果: " << result << endl; cout << "第二个结果: " << result2 << endl; return 0; }
4.6 课后小练习
- 编写一个程序,输入两个整数,计算它们的和、差、积、商和余数,并输出结果。
- 编写一个程序,输入一个整数,判断它是否大于 10 且小于 20,输出判断结果。
通过这部分的学习,你已经了解了 C++ 中的运算符和表达式,它们能让你在编程中进行各种计算和逻辑判断,为后续编写更复杂的程序打下了基础。