C++ Decrement Operator
Overview
In this tutorial, we will learn about C++ Decrement Operator with examples.
Decrement Operator
The decrement operator in C++ is denoted as a — operator symbol. We can apply the operator before or after the variable.
Pre-decrement operator
A pre-decrement operator is used to decrement the value of the variable before evaluating the expression.
cout << “—— Post-Decrement ——-” << endl;
int number = 10;
cout << “@Before number is = ” << number << endl;
int resultExpr = –number;
cout << “resultExpr is = ” << resultExpr << endl;
cout << “@After number is = ” << number << endl;
The value of the variable is first decremented and then used in the expression.
Post-decrement operator
A post-decrement operator is used to decrement the value of the variable after evaluating the expression.
cout << “—— Post-Decrement ——-” << endl;
int number = 10;
cout << “@Before number is = ” << number << endl;
int resultExpr = number–;
cout << “resultExpr is = ” << resultExpr << endl;
cout << “@After number is = ” << number << endl;
The value of the variable is used in the expression and decremented after that.
—— Post-Decrement ——-
@Before number is = 10
resultExpr is = 10
@After number is = 9
That’s it.
—
C++ Tutorials
C++ Tutorials on this website:
https://www.testingdocs.com/c-coding-tutorials/
For more information on the current ISO C++ standard