C++ Increment & Decrement (++,--)
In C++ Introduction & History we have discussed that C++ is real mature baby of C language but having additional features like object oriented support , classes, and other improvements to the C programming language. Then renamed as C++ with ++ increment operator.
++
increment or plus plus ( ++ ) operator use to operate special task.It is use to increment 1 in value of the object
For Example
let x = 5
using ++ operator after variable x++ will add 1 in the value of x
like x = x + 1
now the value of x
x = 6
--
decrement ( -- ) operator also use to operate same functionality like increment operator but it subtract one from the value of the object
For Example
let x = 5
using -- operator after variable x-- will subtract 1
like x = x - 1
now the value of x
x = 4
There is slight difference of using increment and decrement
( ++ , -- ) operators before and after variable.
( x++ , ++x or x-- , --x )
Example Program of increment and decrement operator
Example
Increment and decrement behave same as shown below
- #include <iostream>
- #include <string>
- using namespace std;
- int main(){
- int x = 6 ;
- int y = 6 ;
- cout<< " ++ Operator before variable name \n" ;
- cout << x++ << endl ; // value of x is still 6 at this point
- cout << x << endl ; //it will print 7 after adding
- cout<< " ++ Operator after variable name \n" ;
- cout << ++y << endl ; // value of y is 7 on the spot
- cout << y << endl ; //it will print 7
- return 0 ;
- }
Comments
Post a Comment