Shorthand Operators
The assignment operator in C++ has a very low precedence in the order of operations. This means that the entire expression on the right side of the assignment operator is evaluated first, and only then is the resulting value stored in the variable on the left side of the assignment operator.
Interestingly, the variable on the left of an assignment operator can also appear on the right side of the same assignment. This works exactly as expected. For example, consider the following code:
int x = 0;x = x + 2;x = x - 1;x = x * 5;std::cout << x << std::endl;
The output of this code will be 5
. Here’s how it works:
x
is declared and initialized to0
in the first line.- In the second line,
x + 2
is computed (i.e.,0 + 2 = 2
), and the result is stored inx
, overwriting its previous value. - In the third line,
x
is decremented to1
by subtracting1
from the current value. - In the fourth line,
x
is multiplied by5
, resulting in5
. - Finally, the fifth line prints the value of
x
, which is now5
.
This behavior is not limited to int variables; it applies to any numeric type.
Shorthand Arithmetic Assignment Operators
If you have a numeric variable (e.g., my_number
), you can increase its value by some amount c
(where c
is any numeric expression or variable) using the following shorthand: instead of writing my_number = my_number + c
, you can simply write my_number += c
.
In this context:
- The
=
operator is called the assignment operator. - The
+=
operator is called the addition assignment operator. It calculates the sum of the variable on the left and the expression on the right, then stores the result back in the variable on the left.
Similarly, there are shorthand operators for other arithmetic operations:
- Subtraction:
-=
- Multiplication:
*=
- Division:
/=
- Remainder/Modulo:
%=
Each operator works in the same way: the corresponding arithmetic operation is performed, and the result is stored in the left-hand variable.
Increment and Decrement Operators
If you need to increase my_number
by exactly 1
, you can write my_number = my_number + 1
or use my_number += 1
. However, there are two additional shorthand options: my_number++
and ++my_number
.
- Post-Increment (
my_number++
): Increments the variable by1
, but the expression evaluates to the original value of the variable before the increment. - Pre-Increment (
++my_number
): Increments the variable by1
, and the expression evaluates to the new value of the variable after the increment.
To understand the difference, it’s important to know that in C++, assignments are also expressions. For example:
int my_cool_integer = 0;std::cout << (my_cool_integer = 100) << std::endl;std::cout << my_cool_integer << std::endl;
The output of this code will be:
100100
Here’s what happens:
my_cool_integer
is declared and initialized to0
.- In the second line,
my_cool_integer
is assigned the value100
. The assignment itself is an expression, and its value is the new value ofmy_cool_integer
(i.e.,100
), which gets printed. - The third line prints the current value of
my_cool_integer
, which is still100
.
Shorthand assignment operations (e.g., +=
) also behave as expressions. For instance, the statement std::cout << (x += 5) << std::endl;
computes x + 5
, stores the result in x
, and then prints the new value of x
.
Now consider the difference between pre-increment and post-increment operators:
int x = 0;std::cout << (++x) << std::endl; // Pre-incrementstd::cout << x << std::endl;
x = 0;std::cout << (x++) << std::endl; // Post-incrementstd::cout << x << std::endl;
The output will be:
1101
Here’s the explanation:
- In the first print statement, the pre-increment operator (
++x
) incrementsx
to1
and then prints the new value. - The second print statement shows the current value of
x
, which is1
. x
is then reset to0
. In the third print statement, the post-increment operator (x++
) evaluates to the original value ofx
(i.e.,0
), then incrementsx
to1
.- The fourth print statement shows the updated value of
x
, which is1
.
If you don’t use the value of the operation (e.g., in a standalone statement like x++;
or ++x;
), there’s no functional difference between the two. Both simply increment the variable by 1
.
Finally, just as with increment operators, there are pre- and post-decrement operators (--x
and x--
). These behave the same way but decrease the variable’s value by 1
instead of increasing it.