Home | Computer | Increment and Decrement Operators in C++

# Increment and Decrement Operators in C++

August 23, 2022 written by Sehrish Basharat

Lets discuss the increment and decrement operators in detail

## Increment Operator

The incremental operator is used to increase the value of variables by 1. The symbol used for this is ++. It is a unary operator and works with a single variable.

The increment operator cannot increase the value of the constants and expressions. For example A++ and X++ are valid statements but 8++ is an invalid statement. Similarly , (a+b)++ or ++(a+b) are also invalid.

## Forms of Increment operator

The increment operator can be used in two forms.

### Prefix Form

In prefix form, the increment operator is written before the variable as follows:

++y; this increase the value of variable y by 1.

### Postfix Form

In postfix form, the increment operator is written after the variable as follows:

Y++; This increase the value of variable y by 1.

#include <iostream.h>

#include <conio.h>

Void main()

{

clrscr();

Int a, x, y;

a = b = x = y = 0;

a++;

b = a;

++x;

Y = x;

Cout << ” a=”<<a<<endl<<“b = “<< b<< endl;

Cout << ” x=”<<x<<endl<<“y = “<< y<< endl;

Getch();

}

## Decrement Operator

The decrement operator is used to decrease the value of a variable by 1. It is denoted by the symbol –. It is a unary operator and works with a single variable.

The decrement operator cannot decrease the value of constant and expression. For example, A++ and X– are valid statements but 1– is an invalid statement. Similarly, (a+)– or–(a+b) are also invalid.

## Two forms of decrement operator

The decrement operator can be used in two forms.

### Prefix Form

In prefix form, the decrement operator is written before the variable as follows:

–y; This operator decreases the value of variable y by 1.

### Postfix Form

In prefix form, the decrement operator is written after the variable as follows:

y–;  This operator decreases the value of variable y by 1.

Example

#include <iostream.h>

#include <conio.h>

Void main()

{

clrscr();

Int a, x, y;

a = b = x = y = 0;

a–;

b = a;

–x;

Y = x;

Cout << ” a=”<<a<<endl<<“b = “<< b<< endl;

Cout << ” x=”<<x<<endl<<“y = “<< y<< endl;

Getch();

}

Example

int main()

{

int x = 15 ; int y = 30 ;

— — x ;

y — — ;

cout << x << endl << y ;

}

File Under: