Home » eVirtualGuru » What is Increment and Decrement Operators

What is Increment and Decrement Operators

  
        
          

Increment and Decrement Operators

C++ provides two Unary Operators for incrementing and decrementing variables values by 1.

Increment Operator ++

Decrement Operator 

These Operators operate on one Operand and used in two modes.

1)      Postfix Mode

2)      Prefix Mode

Postfix Mode:

When increment and Decrement operator appear after the operands, the corresponding operation is called Postfix Increment and Postfix Decrement. Postfix increment and decrement are performed on the basis of first-use-then-change rule. This rule states that C++ first uses the value of the operand in the expression and then increments or decrements its value.

Example:             To show the output of post increment

x=10

y==x++

Then after execution the values of x and y will be

Y=10 and x=11.

Program:To understand the working of Postfix Expression.

# include<iostream.h>

#include<conio.h>

void main()

{

clrcsr();

int x,y=10;

int a=8, b;

cout<<”y:”<<y++<<endl;

x=y++;

cout<<”x”<<x<<endl;

cout<<”a:”<<a–<<endl;

b=a–;

cout<<”b:”<<b<<endl;

getch();

}

The output for the variable will be

 

y: 10

x: 12

a: 8

b: 6

Prefix Mode

When increment and Decrement operator appear before the operands, the corresponding operation is called Postfix Increment and Prefix Decrement. Prefix increment and decrement are performed on the basis of first-change-then-use rule. This rule states that C++ first increment or decrement its value of the operand in the expression and then uses that increment or decremented value.

 

Example:             To show the output of Preincrement and Predecrement Operators.

Num=4;

cout<<++num;

cout<< –num;

The output will be 5 for increment and 4 for decrement operator.

Program:To understand the working of Prefix Expression.

                               

# include<iostream.h>

#include<conio.h>

void main ()

{

int big=10, small=1;

cout<<”value of big is”<<big;

cout<<”and value of small is”<<small<<”initially”<<endl;

cout<<”big — :”<<big –<<endl;

cout<<small ++ :”<<small ++<< endl;

cout<<” Now, the value of big is :”<< big<<endl;

cout<<” Now, the value of small is :”<<small<<endl;

cout<<” – big :”<< — << endl;

cout<<” ++small :”<< ++small<<endl;

}

The output for the variable will be

Value of big is 10 and value of small is 1 initially

big — : 10

small ++ : 1

Now, the value of big is : 9

Now, the value of small is : 2

– big :   8

++ small : 3

 

About

I am a person who is positive about every aspect of life.I am a Computer Science lecturer in a school. I love to spend my free time in learning new things.Working for eVirtualGuru gives me a lot extra knowledge and immense joy.

commentscomments

  1. sahil singh says:

    can you add some software for c++ or many languages

Leave a Reply

Your email address will not be published. Required fields are marked *