In this C++ tutorial, you shall learn about Subtraction Assignment operator, its syntax, and how to use this operator, with examples.
C++ Subtraction Assignment
In C++, Subtraction Assignment Operator is used to find the difference of the value (right operand) from the value in this variable (left operand) and assign the result back to this variable (left operand).
The syntax to subtract a value of 2
from variable x
and assign the result to x
using Subtraction Assignment Operator is
</>
Copy
x -= 2
Example
In the following example, we take a variable x with an initial value of 5
, subtract a value of 2
from x
and assign the result to x
, using Subtraction Assignment Operator.
main.cpp
</>
Copy
#include <iostream>
using namespace std;
int main() {
int x = 5;
x -= 2;
cout << "x : " << x << endl;
}
Output
x : 3
Program ended with exit code: 0
Conclusion
In this C++ Tutorial, we learned about Subtraction Assignment Operator in C++, with examples.