C++ Increment Operator
Increment Operator
In this tutorial, we will learn about the C++ Increment operator with examples. In C++, the increment operator is denoted by a ++ operator symbol. We can apply the operator before or after the variable.
Pre-increment operator
A pre-increment operator is used to increment the variable’s value before using and evaluating the expression.
//Declare integer variable number
cout << “—— Pre-Increment ——-” << 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 incremented and then used in the expression.
Output
—— Pre-Increment ——-
@Before number is = 10
resultExpr is = 11
@After number is = 11
Post-increment operator
A post-increment operator is used to increment the value of the variable after evaluating the expression.
//Declare integer variable number
cout << “—— Post-Increment ——-” << 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 used and then the variable is incremented.
Output
—— Post-Increment ——-
@Before number is = 10
resultExpr is = 10
@After number is = 11
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