| operator | name |
|---|---|
| + | addition |
| - | substraction |
| * | multiplication |
| / | division |
| % | modulus |
| operator | name | example | description |
|---|---|---|---|
| ++x | pre-increment | ++5 | first increment x then return x |
| –x | pre-decrement | –5 | first decrement x then return x |
| x++ | post-increment | 5++ | first copy x then increment x then return copy |
| x– | post-decrement | 5– | first copy x then decrement x then return copy |
When place before the operand, it is a pre-increment/decrement operator.
int x = 10;
// x is decremented to 9, then assigned to y
int y = --x;
std::cout << x << ' ' << y;
//output: 9 9
When place after the operand, it is a post-increment/decrement operator.
int x = 10;
// a copy of x is made, x is decremented to 9
// the value of the copy (10) is assigned to y
int y = x--;
std::cout << x << ' ' << y;
//output: 9 10
Operators that assign value to a variable.
| operator | example |
|---|---|
| = | x = 5 |
| += | x += 5 |
| -= | x -= 5 |
| *= | x *= 5 |
| /= | x /= 5 |
| %= | x %= 5 |
Compare two variables or values.
The result of the operation is a bool.
| operator | example |
|---|---|
| == | x == y |
| != | x != y |
| > | x > y |
| < | x < y |
| >= | x > y |
| <= | x < y |
Compare the logic of two or more conditions.
The result of the logical operation is a bool.
| operator | example |
|---|---|
| && | x == y |
| ∣∣ | x ∣∣ y |
| ! | !x |
| operator | name | action | exemple |
|---|---|---|---|
| « | insertion | std::cout « “hello” | |
| » | extraction | std::cin » x | |
| :: | scope resolution | identifies on the left, the namespace of the right identifier | std::cout |
std::cout << "hello there" << name;
std::cin >> variable_name;
Manipulate variables at bit level.
Commonly used with numerical variables.
| operator | name | example | description |
|---|---|---|---|
| « | left shift | x « y | shift bits in x left by y |
| » | right shift | x » y | shifht bits in x right by y |
| ~ | bitwise NOT | ~x | flip all bits in x |
| & | bitwise AND | x % y | each bit in x AND each bit in y |
| ∣ | bitwise OR | x ∣ y | each bit in x OR each bit in y |
| ^ | bitwise XOR | x ^ y | each bit in x XOR each bit in y |
More info
In a compound expression, operators are evaluated in the order of their precedence level in descending order (smallest precedence value go first).
Find the complete list here.