operator
Role: Operations used to execute code
In this chapter, we will focus on the following types of operators:
Operator type | Effect |
---|---|
Arithmetic Operators | For four operations |
Assignment Operators | Used to assign the value of an expression to a variable |
Comparison operator | Used for comparison of expressions and returns a true or false value |
Logical operators | Used to return true or false values based on the value of an expression |
1. Arithmetic Operators
Role: For four operations
Arithmetic operators include the following symbols:
operator | term | Example | Result |
---|---|---|---|
+ | Positive sign | +3 | 3 |
- | Minus sign | -3 | -3 |
+ | plus | 10 + 5 | 15 |
- | reduce | 10 - 5 | 5 |
* | ride | 10 * 5 | 50 |
/ | except | 10 / 5 | 2 |
% | Modular (Remaining) | 10 % 3 | 1 |
++ | Pre-increment | a=2; b=++a; | a=3; b=3; |
++ | Postincrement | a=2; b=a++; | a=3; b=2; |
– | Pre-Decreasing | a=2; b=–a; | a=1; b=1; |
– | Posterior Decrease | a=2; b=a–; | a=1; b=2; |
Example 1:
//Add, subtract, multiply and divide int main() { int a1 = 10; int b1 = 3; cout << a1 + b1 << endl; cout << a1 - b1 << endl; cout << a1 * b1 << endl; cout << a1 / b1 << endl; //Divide two integers and the result is still an integer int a2 = 10; int b2 = 20; cout << a2 / b2 << endl; int a3 = 10; int b3 = 0; //Cout << A3 / B3 << endl; // Error, divisor cannot be 0 //Two decimals can be divided double d1 = 0.5; double d2 = 0.25; cout << d1 / d2 << endl; system("pause"); return 0; }
Summary: In a division operation, the divisor cannot be zero
Example 2:
//Modeling 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; // Divider cannot be 0 in modulo operation //Two decimal numbers are not modular double d1 = 3.14; double d2 = 1.1; //cout << d1 % d2 << endl; system("pause"); return 0; }
Summary: Only integer variables can be modulated
Example 3:
//Incremental int main() { //Postincrement int a = 10; a++; //Equivalent to a = a + 1 cout << a << endl; // 11 //Pre-increment int b = 10; ++b; cout << b << endl; // 11 //Difference //Pre-increment adds ++ to the variable before calculating the expression int a2 = 10; int b2 = ++a2 * 10; cout << b2 << endl; //Post-increment first evaluates the expression, then ++ the variable int a3 = 10; int b3 = a3++ * 10; cout << b3 << endl; system("pause"); return 0; }
Summary: Pre-increment adds ++ to the variable before calculating the expression, and then adds up the opposite
2. Assignment Operators
Role: Used to assign the value of an expression to a variable
Assignment operators include the following symbols:
operator | term | Example | Result |
---|---|---|---|
= | assignment | a=2; b=3; | a=2; b=3; |
+= | Add equals | a=0; a+=2; | a=2; |
-= | Subtract equals | a=5; a-=3; | a=2; |
*= | Multiply equals | a=2; a*=2; | a=4; |
/= | Divide equals | a=4; a/=2; | a=2; |
%= | Modulo equals | a=3; a%2; | a=1; |
Example:
int main() { //Assignment Operators // = 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; system("pause"); return 0; }
3. Comparison Operators
Role: Used to compare expressions and return a true or false value
The comparison operator has 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 system("pause"); return 0; }
Note: In the comparative operation between C and C++, True is represented by the number 1 and False is represented by the number 0.
4. Logical Operators
Role: Used to return true or false values based on the value of an expression
Logical operators have the following symbols:
operator | term | Example | Result |
---|---|---|---|
! | wrong | !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 one of a and b is true, the result is true, and if both are false, the result is false. |
Example 1: Logical NOT
//Logical Operator - Not int main() { int a = 10; cout << !a << endl; // 0 cout << !!a << endl; // 1 system("pause"); return 0; }
Summary: True to False, False to True
Example 2: Logical and
//Logical Operator--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 system("pause"); return 0; }
Summary: Summary of Logic and Operators: True as True, False as Others
Example 3: Logical or
//Logical Operator--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 system("pause"); return 0; }
Summary of logic or operator: duplicate false, remaining true
Reference resources: https://www.bilibili.com/video/BV1et411b73Z?p=15&t=271