Increment and Decrement Operator in C++

Google Advertisements

Prev Tutorial Next Tutorial

Increment and Decrement Operator in C++

Increment operators are used to increase the value of the variable by one and decrement operators are used to decrease the value of the variable by one.

Both increment and decrement operator are used on single operand or variable, so it is called as unary operator. Unary operators are having higher priority than the other operators it means unary operators are execute before other operators.

Syntax

 ++  // increment operator
 --   // decrement operator

Note: Increment and decrement operators are can not apply on constant.

Example

x= 4++;  // gives error, because 4 is constant

Type of Increment Operator

  • pre-increment
  • post-increment

pre-increment (++ variable)

In pre-increment first increment the value of variable and then used inside the expression (initialize into another variable).

Syntax

++ variable;

Example pre-increment in C++

#include<iostream.h>
#include<conio.h>

void main()
{
int x,i;
i=10;
x=++i;
cout<<"x: "<<x;
cout<<"i: "<<i;
getch();
}

Output

x: 11
i: 11

In above program first increase the value of i and then used value of i into expression.

post-increment (variable ++)

In post-increment first value of variable is use in the expression (initialize into another variable) and then increment the value of variable.

Syntax

variable ++;

Example post-increment

#include<iostream.h>
#include<conio.h>

void main()
{
int x,i;
i=10;
x=i++;
cout<<"x: "<<x;
cout<<"i: "<<i;
getch();
}

Output

x: 10
i: 11

In above program first used the value of i into expression then increase value of i by 1.

Type of Decrement Operator

  • pre-decrement
  • post-decrement

Pre-decrement (-- variable)

In pre-decrement first decrement the value of variable and then used inside the expression (initialize into another variable).

Syntax

-- variable;

Example pre-decrement

#include<iostream.h>
#include<conio.h>

void main()
{
int x,i;
i=10;
x=--i;
cout<<"x: "<<x;
cout<<"i: "<<i;
getch();
}

Output

x: 9
i: 9

In above program first decrease the value of i and then value of i used in expression.

post-decrement (variable --)

In Post-decrement first value of variable is use in the expression (initialize into another variable) and then decrement the value of variable.

Syntax

variable --;

Example post-decrement

#include<iostream.h>
#include<conio.h>

void main()
{
int x,i;
i=10;
x=i--;
cout<<"x: "<<x;
cout<<"i: "<<i;
getch();
}

Output

x: 10
i: 9

In above program first used the value of x in expression then decrease value of i by 1.

Example of increment and decrement operator

increment and decrement operator

Example

#include<iostream.h>
#include<conio.h>

void main()
{
int x,a,b,c;
a = 2;
b = 4;
c = 5;
x = a-- + b++ - ++c;
cout<<"x: "<<x;
getch();
}

Output

x: 0

Prev Tutorial Next Tutorial

Google Advertisements

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. If you want to post your article on our website/FB Group/FB Page please contact on [email protected] or 8076671483

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: [email protected] or 8076671483

Path variable is set for providing path for all Java tools like java, javac, javap, javah, jar, appletviewer which are used in java programming. All these tools are available in bin folders so we set path upto bin folders.

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar.

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user.

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects.

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: [email protected] or 8076671483 Try this Keyword C++ Programs