Wiki
operator
1.Operator
**Purpose:** Operations used to execute code
In this chapter, we will mainly explain the following categories of operators:
| Operator Type | Purpose |
|---|---|
| Arithmetic operators | Used to perform the four basic arithmetic operations |
| Assignment operators | Used to assign the value of an expression to a variable |
| Comparison operators | Used to compare expressions and return a true or false value |
| Logical operators | Used to return a true or false value based on the value of an expression |
1.1.Arithmetic operators
Function:Used to handle the four arithmetic operations
Arithmetic operators include the following symbols:
| Operator | Term | Example | Result |
|---|---|---|---|
| + | Positive sign | +3 | 3 |
| - | Negative sign | -3 | -3 |
| + | Addition | 10 + 5 | 15 |
| - | Subtraction | 10 - 5 | 5 |
| * | Multiplication | 10 * 5 | 50 |
| / | Division | 10 / 5 | 2 |
| % | Modulo (remainder) | 10 % 3 | 1 |
| ++ | Pre-increment | a=2; b=++a; | a=3; b=3; |
| ++ | Post-increment | a=2; b=a++; | a=3; b=2; |
| -- | Pre-decrement | a=2; b=--a; | a=1; b=1; |
| -- | Post-decrement | a=2; b=a--; | a=1; b=2; |
Example 1:
//加减乘除
int main() {
int a1 = 10;
int b1 = 3;
cout << a1 + b1 << endl;
cout << a1 - b1 << endl;
cout << a1 * b1 << endl;
cout << a1 / b1 << endl; //两个整数相除结果依然是整数
int a2 = 10;
int b2 = 20;
cout << a2 / b2 << endl;
int a3 = 10;
int b3 = 0;
//cout << a3 / b3 << endl; //报错,除数不可以为0
//两个小数可以相除
double d1 = 0.5;
double d2 = 0.25;
cout << d1 / d2 << endl;
return 0;
}
Operation result:
13
7
30
3
0
2
Summary: In division, the divisor cannot be 0
Example 2:
//取模
int main() {
int a1 = 10;
int b1 = 3;
cout << 10 % 3 << endl;
int a2 = 10;
int b2 = 20;
cout << a2 % b2 << endl;
int a3 = 10;
int b3 = 0;
//cout << a3 % b3 << endl; //取模运算时,除数也不能为0
//两个小数不可以取模
double d1 = 3.14;
double d2 = 1.1;
//cout << d1 % d2 << endl;
return 0;
}
Operation result:
1
10
Summary: Only integer variables can be used in modulo operations.
Example 3:
//递增
int main() {
//后置递增
int a = 10;
a++; //等价于a = a + 1
cout << a << endl; // 11
//前置递增
int b = 10;
++b;
cout << b << endl; // 11
//区别
//前置递增先对变量进行++,再计算表达式
int a2 = 10;
int b2 = ++a2 * 10;
cout << b2 << endl;
//后置递增先计算表达式,后对变量进行++
int a3 = 10;
int b3 = a3++ * 10;
cout << b3 << endl;
return 0;
}
Operation result:
11
11
110
100
Summary: Prefix increment first applies ++ to the variable, then evaluates the expression; postfix increment is the opposite.
1.2.Assignment operator
**Function:** Used to assign the value of an expression to a variable
The assignment operators include the following symbols:
| Operator | Term | Example | Result |
|---|---|---|---|
| = | Assignment | a=2; b=3; | a=2; b=3; |
| += | Add and assign | a=0; a+=2; | a=2; |
| -= | Subtract and assign | a=5; a-=3; | a=2; |
| *= | Multiply and assign | a=2; a*=2; | a=4; |
| /= | Divide and assign | a=4; a/=2; | a=2; |
| %= | Modulo and assign | a=3; a%2; | a=1; |
Example:
int main() {
//赋值运算符
// =
int a = 10;
a = 100;
cout << "a = " << a << endl;
// +=
a = 10;
a += 2; // a = a + 2;
cout << "a = " << a << endl;
// -=
a = 10;
a -= 2; // a = a - 2
cout << "a = " << a << endl;
// *=
a = 10;
a *= 2; // a = a * 2
cout << "a = " << a << endl;
// /=
a = 10;
a /= 2; // a = a / 2;
cout << "a = " << a << endl;
// %=
a = 10;
a %= 2; // a = a % 2;
cout << "a = " << a << endl;
return 0;
}
Operation result:
a = 100
a = 12
a = 8
a = 20
a = 5
a = 0
1.3.Comparison operator
**Function:** Used to compare expressions and return a true or false value
Comparison operators include the following symbols:
| Operator | Term | Example | Result |
|---|---|---|---|
| == | Equal to | 4 == 3 | 0 |
| != | Not equal to | 4 != 3 | 1 |
| < | Less than | 4 < 3 | 0 |
| \> | Greater than | 4 > 3 | 1 |
| <= | Less than or equal to | 4 <= 3 | 0 |
| \>= | Greater than or equal to | 4 >= 1 | 1 |
Example:
int main() {
int a = 10;
int b = 20;
cout << (a == b) << endl; // 0
cout << (a != b) << endl; // 1
cout << (a > b) << endl; // 0
cout << (a < b) << endl; // 1
cout << (a >= b) << endl; // 0
cout << (a <= b) << endl; // 1
return 0;
}
Operation result:
0
1
0
1
0
1
Note: In comparison operations in C and C++ language, == "true" is represented by the number "1", and "false" is represented by the number "0".==
1.4.Logical operators
**Purpose:** Used to return a true value or false value based on the value of an expression.
Logical operators include the following symbols:
| Operator | Term | Example | Result |
|---|---|---|---|
| ! | NOT | !a | If a is false, then !a is true; if a is true, then !a is false. |
| && | AND | a && b | If both a and b are true, the result is true; otherwise, it is false. |
| \|\| | OR | a \|\| b | If either a or b is true, the result is true; if both are false, the result is false. |
**Example 1:** Logical NOT
//逻辑运算符 --- 非
int main() {
int a = 10;
cout << !a << endl; // 0
cout << !!a << endl; // 1
return 0;
}
Operation result:
0
1
Summary: True becomes false, false becomes true
**Example 2:** Logic and
//逻辑运算符 --- 与
int main() {
int a = 10;
int b = 10;
cout << (a && b) << endl;// 1
a = 10;
b = 0;
cout << (a && b) << endl;// 0
a = 0;
b = 0;
cout << (a && b) << endl;// 0
return 0;
}
Operation result:
1
0
0
Summary: Logical ==AND== operator summary: ==true only when both are true, otherwise false==
**Example 3:** Logical OR
//逻辑运算符 --- 或
int main() {
int a = 10;
int b = 10;
cout << (a || b) << endl;// 1
a = 10;
b = 0;
cout << (a || b) << endl;// 1
a = 0;
b = 0;
cout << (a || b) << endl;// 0
return 0;
}
Operation result:
1
1
0
Logical ==OR== operator summary: ==false only when both are false; true otherwise==