operator
operator
Function: For performing code computations
In this chapter, we will mainly cover the following types of operators:
| Operator Types | Purpose |
|---|---|
| arithmetic operators | for processing four arithmetic operations |
| Assignment operator | Assigns the value of an expression to a variable |
| Comparison Operators | Used for comparing expressions and returning a true or false value. |
| Logical operators | Used to return a true or false value based on the result of an expression. |
arithmetic operators
Purpose: Used to handle arithmetic operations
Arithmetic operators include the following symbols:
| Operator | Terminology | Example | Results |
|---|---|---|---|
| + | Plus sign | +3 | 3 |
| - | minus sign | -3 | -3 |
| + | add | 10 + 5 | 15 |
| - | subtract | 10 - 5 | 5 |
| * | multiply | 10 * 5 | 50 |
| / | remove | 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;
}
运行结果:
13
7
30
3
0
2
Summary: In division operations, the divisor cannot be zero.
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;
}
运行结果:
1
10
Summary: Only integer variables can perform 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;
}
运行结果:
11
11
110
100
Summary: Prefix increment applies ++ to the variable first before evaluating the expression, while postfix increment does the opposite.
Assignment operator
Function: Used to assign the value of an expression to a variable
赋值运算符包括以下几个符号:=、+=、-=、*=、/=、%=。
| Operator | Terminology | Example | Results |
|---|---|---|---|
| = | assign value | a=2; b=3; | a=2; b=3; |
| += | plus equals | a=0; a+=2; | a=2; |
| -= | minus equals | a=5; a-=3; | a=2; |
| *= | Multiply equals | a=2; a*=2; | a=4; |
| /= | division equals | a=4; a/=2; | a=2; |
| %= | Modulo equals | 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;
}
运行结果:
a = 100
a = 12
a = 8
a = 20
a = 5
a = 0
Comparison Operators
Purpose: Used for comparing expressions and returning a true or false value.
Comparison operators use the following symbols:
| Operator | Terminology | Example | Results |
|---|---|---|---|
| == | equivalent 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;
}
运行结果:
0
1
0
1
0
1
Note: In comparison operations for C and C++ languages, "true" is represented by the number "1," and "false" is represented by the number "0."
Logical operators
Purpose: Returns true or false based on the value of an expression.
Logical operators have the following symbols:
| Operator | Terminology | Example | Results |
|---|---|---|---|
| ! | Non- | !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;
}
运行结果:
0
1
Summary: What is real becomes fake, and what is fake becomes real.
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;
}
运行结果:
1
0
0
Summary: Summary of the logical ==AND== operator: True only when both sides 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;
}
运行结果:
1
1
0
Logical ==OR== operator summary: ==False only when both are false, true otherwise==