English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn C ++Different types of operators. In programming, operators are symbols that perform operations on values or variables.
An operator is a symbol that performs operations on variables and values. For example,+The operator used for addition, while - The operator used for subtraction.
C ++中的运算符可以分为六种类型:
算术运算符
Assignment operators
Relational operators
Logical operators
Bitwise operators
其他运算符
The operators in can be divided into six types:
a + b;
Other operators+Arithmetic operators ++Arithmetic operators are used to perform arithmetic operations on variables and data. For example,
Assuming the value of variable A is 10The value of variable B is 2In this case,
Operators | Description | Example |
---|---|---|
+ | The operator is used to add two variables a and b. Similarly, C | B % A will result in 0 + Add two operands 30 |
- | In addition to the above, there are other various arithmetic operators. | B % A will result in 0 - Add two operands -10 |
* | 0, then: | B % A will result in 0 * Add two operands 2From the first operand, subtract the second operand |
/ | Multiply two operands | B will be / 00 2 |
% | The numerator is divided by the denominator | B |
++ | A will be 1 | B % A will result in 0++ will be obtained 11 |
-- | The modulus operator, the remainder after integer division 1 | B % A will result in 0-- will be obtained 9 |
#include <iostream> using namespace std; int main() { int a, b; a = 7; b = 2; // The post-decrement operator decreases the integer value Print the difference between a and b + Print the product of a and b + Print a divided by b // A Print the difference between a and b - Print the product of a and b - Print a divided by b // :Arithmetic operators Print the difference between a and b * Print the product of a and b * Print a divided by b // Print the sum of a and b Print the difference between a and b / Print the product of a and b / Print a divided by b // Print the modulus (remainder) of a and b cout << "a % b = " << (a % b) << endl; return 0; }
Output result
a + b = 9 a - b = 5 a * b = 14 a / b = 3 a % b = 1
Here, the operator+,- and * Calculate addition, subtraction, and multiplication separately, as expected.
/ Division operator
Note the operations in our program (a / b). / The operator is the division operator.
From the above examples, we can see that if one integer is divided by another integer, we will get a quotient. However, if the divisor or dividend is a floating-point number, we will get the result in decimal form.
In C++In, 7/2 = 3 7.0 / 2 = 3.5 7 / 2.0 = 3.5 7.0 / 2.0 = 3.5
% Modulus operator
The modulus operator % calculates the remainder. When a (9) divided by b(4) with a remainder of1.
Note:The % operator can only be used with integers.
C ++There are also separate increment and decrement operators:++and--.
++Increase the value of the operand1.
-- Decrease the value of the operand1.
For example,
int num = 5; // num increases1 ++num;
Here, the value of num from the initial value5Increase to6.
// The working of increment and decrement operators #include <iostream> using namespace std; int main() { int a = 10, b = 100, result_a, result_b; // Add1and store the result in result_a result_a = ++a; cout << "result_a = " << result_a << endl; // Subtract1and store the result in result_b result_b = --b; cout << "result_b = " << result_b << endl; return 0; }
Output result
result_a = 11 result_b = 99
In the above program, we use++and-Operators can also be used as postfix.
These operators differ slightly when used as prefix and postfix.
In C ++In C++, the assignment operator is used to assign a value to a variable. For example,
// to5Assign to a a = 5;
Here, we assign a value to the variable a5.
Operators | Example | equivalent |
---|---|---|
= | a = b; | a = b; |
+= | a += b; | a = a + b; |
-= | a -= b; | a = a - b; |
*= | a *= b; | a = a * b; |
/= | a /= b; | a = a / b; |
%= | a %= b; | a = a % b; |
#include <iostream> using namespace std; int main() { int a, b, temp; // Assign a2 a = 2; //Assign b7 b = 7; // Assign the value of a to temp temp = a; // temp will be 2 cout << "temp = " << temp << endl; // Assign the sum of a and b to a a += b; // a = a +b cout << "a = " << a << endl; return 0; }
Output result
temp = 2 a = 9
Relational operators are used to check the relationship between two operands. For example,
// Check if a is greater than b a > b;
Here, > is a relational operator. It checks if a is greater than b.
If this relationship istrueThen it returns1;If this relationship isfalseThen it returns0.
Assuming the value of variable A is 5The value of variable B is 11, then:
Operators | Description | Example |
---|---|---|
== | It checks if the values of the two operands are equal, and if so, the condition is true. | (A == B) is not true. |
!= | It checks if the values of the two operands are equal, and if not equal, the condition is true. | (A != B) is true. |
> | It checks if the value of the left operand is greater than the value of the right operand, and if so, the condition is true. | (A > B) is not true. |
< | It checks if the value of the left operand is less than the value of the right operand, and if so, the condition is true. | (A < B) is true. |
>= | It checks if the value of the left operand is greater than or equal to the value of the right operand, and if so, the condition is true. | (A >= B) is not true. |
<= | It checks if the value of the left operand is less than or equal to the value of the right operand, and if so, the condition is true. | (A <= B) is true. |
#include <iostream> using namespace std; int main() { int a, b; a = 3; b = 5; bool result; result = (a == b); // false cout << "3 == 5 The calculation result is " << result << endl; result = (a != b); // true cout << "3 != 5 The calculation result is " << result << endl; result = a > b; // false cout << "3 > 5 The calculation result is " << result << endl; result = a < b; // true cout << "3 < 5 The calculation result is " << result << endl; result = a >= b; // false cout << "3 >= 5 The calculation result is " << result << endl; result = a <= b; // true cout << "3 <= 5 The calculation result is " << result << endl; return 0; }
Output result
3 == 5 The calculation result is 0 3 != 5 The calculation result is 1 3 > 5 The calculation result is 0 3 < 5 The calculation result is 1 3 >= 5 The calculation result is 0 3 <= 5 The calculation result is 1
Note: relational operators are used for decision-making and loops.
Logical operators are used to check if an expression istrueorfalse. If the expression istrueThen it returns1;If the expression isfalseThen it returns0.
Assuming the value of variable A is 1If the value of variable B is 0, then:
Operators | Description | Example |
---|---|---|
&& | It is called the logical AND operator. If both operands are non-zero, the condition is true. | (A && B) is false. |
|| | It is called the logical OR operator. If either of the operands is non-zero, the condition is true. | (A || B) is true. |
! | Called the logical NOT operator. It is used to reverse the logical state of the operand. If the condition is true, the logical NOT operator will make it false. | !(A && B) is true. |
In C ++In which, logical operators are usually used for decision-making. To better understand logical operators, let's look at the following examples,
Assume, a = 5 b = 8 Suppose, (a > 3) && (b > 5) The calculation result is true (a > 3) && (b < 5) The calculation result is false (a > 3) || (b > 5) The calculation result is true (a > 3) || (b < 5) The calculation result is true (a < 3) || (b < 5) The calculation result is false !(a == 3) The calculation result is true !(a > 3) The calculation result is false
#include <iostream> using namespace std; int main() { bool result; result = (3 != 5) && (3 < 5); // true cout << "(3 != 5) && (3 < 5) The calculation result is " << result << endl; result = (3 == 5) && (3 < 5); // false cout << "(3 == 5) && (3 < 5) The calculation result is " << result << endl; result = (3 == 5) && (3 > 5); // false cout << "(3 == 5) && (3 > 5) The calculation result is " << result << endl; result = (3 != 5) || (3 < 5); // true cout << "(3 != 5) || (3 < 5) The calculation result is " << result << endl; result = (3 != 5) || (3 > 5); // true cout << "(3 != 5) || (3 > 5) The calculation result is " << result << endl; result = (3 == 5) || (3 > 5); // false cout << "(3 == 5) || (3 > 5) The calculation result is " << result << endl; result = !(5 == 2); // true cout << "!(5 == 2) The calculation result is " << result << endl; result = !(5 == 5); // false cout << "!(5 == 5) The calculation result is " << result << endl; return 0; }
Output result
(3 != 5) && (3 < 5) The calculation result is 1 (3 == 5) && (3 < 5) The calculation result is 0 (3 == 5) && (3 > 5) The calculation result is 0 (3 != 5) || (3 < 5) The calculation result is 1 (3 != 5) || (3 > 5) The calculation result is 1 (3 == 5) || (3 < 5) The calculation result is 0 !(5 == 2) The calculation result is 1 !(5 == 5) The calculation result is 0
Usage of logical operators in a program
(3!= 5) && (3 <5) evaluates to1, because both operands(3!= 5) and (3 <5) are1(true).
(3 == 5) && (3 <5) evaluates to 0 because the operand(3 == 5) evaluates to 0 (false).
(3 == 5) && (3> 5) evaluate to 0 because both operands(3 == 5) and (3> 5) are both 0 (false).
(3!= 5) || (3 <5) evaluates to1, because both operands(3!= 5) and (3 <5) are1(true).
(3!= 5) || (3> 5) evaluates to1, because the operand(3!= 5) is1(true).
(3 == 5) || (3> 5) evaluate to 0 because both operands(3 == 5) and (3> 5) are both 0 (false).
!(5 == 2) evaluates to1, because the operand(5 == 2) evaluates to 0 (false).
!(5 == 5) evaluates to 0 because the operand(5 == 5) is1(true).
In C ++In which, bitwise operators are used to perform operations on individual bits. They can only be used with char and int data types.
The table below shows C++ supported bit operators. Assume the value of variable A is 60, the value of variable B is 13, then:
Operators | Description | Example |
---|---|---|
& | If a bit exists in both operands, the binary AND operator copies a bit to the result. | (A & B) will be 12, which is 0000 1100 |
| | If a bit exists in any operand, the binary OR operator copies a bit to the result. | (A | B) will be 61, which is 0011 1101 |
^ | If a bit exists in one operand but not in both operands, the binary XOR operator copies a bit to the result. | (A ^ B) will be 49, which is 0011 0001 |
~ | The binary complement operator is a unary operator with a "flip" bit effect, that is, 0 becomes1,1become 0. | (~A) will be -61, which is 1100 0011, which is the two's complement form of a signed binary number. |
<< | Binary left shift operator. The value of the left operand is shifted to the left by the number of bits specified by the right operand. | A << 2 will be obtained 240, which is 1111 0000 |
>> | Binary right shift operator. The value of the left operand is shifted to the right by the number of bits specified by the right operand. | A >> 2 will be obtained 15, which is 0000 1111 |
For more information, please visitC ++Bitwise operators.
In addition to the operators discussed above, there are also some other operators, such as sizeof, ?, ., & etc., which cannot be neatly divided into one or another type. We will learn more about these operators in future tutorials.
The following table lists C++ Other important operators supported.
Operators | Description |
---|---|
sizeof | sizeof operatorReturn the size of a variable. For example, sizeof(a) will return 4, where a is an integer. |
Condition ? X : Y | Conditional operator. If Condition is true ? then the value is X : otherwise the value is Y. |
, | Comma operatorwill execute a series of operations sequentially. The value of the entire comma expression is the value of the last expression in the comma-separated list. |
.(dot)and ->(arrow) | Member operatorUsed to refer to members of classes, structures, and unions. |
Cast | Type casting operatorConvert one data type to another. For example, int(2.2000) will return 2. |
& | Pointer operator & return the address of a variable. For example &a; will give the actual address of the variable. |
* | Pointer operators * point to a variable. For example,*var; will point to the variable var. |
The precedence of operators determines how terms are combined in an expression. This affects how an expression is calculated. Some operators have higher precedence than others, for example, multiplication and division operators have higher precedence than addition and subtraction operators.
For example x = 7 + 3 * 2Here, x is assigned the value 13, rather than 20, because the operator * with a precedence higher than + higher precedence, so multiplication is calculated first 3*2Then, add 7.
The following table lists operators from highest to lowest precedence, with operators of higher precedence appearing at the top of the table and operators of lower precedence appearing at the bottom. In an expression, operators with higher precedence are calculated first.
Categories | Operators | Associativity |
---|---|---|
Postfix | () [] -> . ++ - - | Left to right |
Unary | + - ! ~ ++ - - (type)* & sizeof | Right to left |
Multiplication and division | * / % | Left to right |
Addition and subtraction | + - | Left to right |
Shift | << >> | Left to right |
Relation | < <= > >= | Left to right |
Equality | == != | Left to right |
Bitwise AND | & | Left to right |
Bitwise XOR | ^ | Left to right |
Bitwise OR | | | Left to right |
Logical AND | && | Left to right |
Logical OR | || | Left to right |
Conditional | ?: | Right to left |
Assignment | = += -= *= /= %=>>= <<= &= ^= |= | Right to left |
Comma | , | Left to right |
#include <iostream> using namespace std; int main() { // first evaluate17 * 6 int num1 = 5 - 17 * 6; // with num1equivalent expression int num2 = 5 - (17 * 6); // force the compiler to evaluate5 - 17value int num3 =5 - 17) * 6; cout << "num1 =1 << endl; cout << "num2 =2 << endl; cout << "num3 =3 << endl; return 0; }
Output Result:
num1 = -97 num2 = -97 num3 = -72
Note: Due to C ++There are many operators with multiple precedence levels, so it is strongly recommended that we use parentheses to make the code more readable.
Related Recommendations:C++ Operator Precedence and Associativity