The Big Master

Increment Operator

#include <iostream> using namespace std; int main() { int x = 10...
#include <iostream>
using namespace std;
int main()
{
    int x = 10 ;
    x = x + 5 ; 	cout << x << endl; 
    x = x - 1 ; 	cout << x << endl;
    x = x * 2 ;     	cout << x << endl ;
    x = x / 14 ;	cout << x << endl ;
    return 0;
}

For single increment, you can use x++ instead of x = x+1 or x+=1

#include <iostream>
using namespace std;
int main()
{
    int x = 10 ;
    x = x + 1  ; 	 cout << x << endl;     // 11
    x +=    1  ; 	 cout << x << endl;    // 12
    x++        ;     	cout << x << endl ;    //13     	
    return 0;
}

Prefix increment/Decrement

Prefix Increment (++variable):

  • The prefix increment operator ++ is placed before the variable (operand).
  • It increases the value of the variable by 1 before the expression’s evaluation takes place.
  • The result of the expression is the updated value of the variable.

 

Postfix Increment (variable++):

The postfix increment operator ++ is placed after the variable (operand).

It increases the value of the variable by 1 after the expression’s evaluation is complete.

The result of the expression is the original value of the variable (before increment).

#include <iostream>
using namespace std;
int main()
{
    int x = 10 , y ; 
    y = x ++;
    cout << "value of x " << x << endl ;
    cout << "value of y " << y << endl ;	 
return 0;
}
#include <iostream>
using namespace std;
int main()
{
    int x = 10 , y ; 
    y = ++ x  ;
    cout << "value of x " << x << endl ;
    cout << "value of y " << y << endl ;	 
return 0;
}
#include <iostream>
using namespace std;
int main()
{
    int x = 10 , y = 20  , z ; 
    z = ++ x * y  ;
    cout << "value of x " << x << endl ;
    cout << "value of y " << y << endl ;	 
    cout << "Value of z " << z << endl ;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
    //for (int x = 10 ; x >= 1 ; x -=2 )
    //	cout << x <<  endl ;
    int x = 10 , y = 20  , z ; 
    z =  x++ * y  ;
    cout << "value of x " << x << endl ;
    cout << "value of y " << y << endl ;	 
    cout << "Value of z " << z << endl ;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
    //for (int x = 10 ; x >= 1 ; x -=2 )
    //	cout << x <<  endl ;
    int x = 10 , y = 20  , z ; 
    z =  x++ * y --  ;
    cout << "value of x " << x << endl ;
    cout << "value of y " << y << endl ;	 
    cout << "Value of z " << z << endl ;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
    int x = 10 , y = 20  , z ; 
    z =  ++x++ * --y   ; // not allowed 
    cout << "value of x " << x << endl ;
    cout << "value of y " << y << endl ;	 
    cout << "Value of z " << z << endl ;
return 0;
}