Sunday 7 October 2018

Pre-increment and Post-increment operator in C++

Increment operator


The increment operator(++) is a unary operator which increases the value of operand by one(1). It is used to abbreviate the assigning of variable's increased value (by 1) to the same variable i.e. a++ means a=a+1.

Unary operators are those operators which perform an operation upon one operand to produce a new value.

The increment operator can be utilized in two different ways, depending on whether the operator is written before or after the operand.

Pre-increment operator
If the increment operator precedes the operand, then it is called the pre-increment operator(E.g. ++a). The value of operand will be incremented before its value is utilized in the expression.
Example:
y=++a;     means   a=a+1; and y=a; will be executed serially.
First, the value of a will be incremented, then it will be assigned to y.

Post-increment operator
If the increment operator follows the operand, then it is called the post-increment operator(E.g. a++). The value of operand will be incremented after its value is utilized in the expression.
Example:
y=a++;     means   y=a; and a=a+1; will be executed serially.
First, the value of a will be assigned to y, then it will be incremented.

Write a program in C++ to give an idea about the pre-increment and post-increment operator.

In this program, we perform different operations using pre-increment and post-increment operator. We will input a number from the user and the altered values due to pre-increment and post-increment operator will be printed.


Program


1    #include<iostream.h>                                                                                                          
2    #include<conio.h>                                                                                                                    
3    void main()                                                                     
4    {                                                                             
5    int a,y;                                                                           
6    clrscr();                                                                         
  cout<<"Enter a number= ";
8    cin>>a;
  cout<<endl<<"a= "<<a<<endl;
10  y=a++;
11  cout<<"y= a++= "<<y<<endl;
12  cout<<"a= "<<a<<endl;
13  y=++a;
14  cout<<"y= ++a= "<<y<<endl;
15  cout<<"a= "<<a<<endl;
16  cout<<"a++= "<<a++<<endl;
17  cout<<"++a= "<<++a<<endl;
18  a++;
19  cout<<"a++"<<endl<<"a= "<<a<<endl;
20  getch();
21  }


Output


Increment operator in C++
USE OF INCREMENT OPERATOR IN C++




Explanation

In this program, the user enters 5 as the input number(i.e. a=5). The remaining part of the program will be executed as follows:

Line number                        Action                                                               Output
9                                            Value of a(i.e. 5) is printed.                               5
10                                          y=a++ i.e. y=5 and a=5+1=6                                             
11                                          Value of y(i.e. 5) is printed.                               5
12                                          Value of a(i.e. 6) is printed.                               6
13                                          y=++a i.e. a=6+1=7 and y=7                         
14                                          Value of y(i.e. 7) is printed.                               7
15                                          Value of a(i.e. 7) is printed.                               7                                   
16                                          Value of a++ is printed i.e. the value
                                              of a(i.e. 7) is printed and  a=7+1=8.                  7                                     
17                                          Value of ++a is printed i.e. a=8+1=9  
                                              and the value of a(i.e. 9) is printed.                   9       
18                                          a++ i.e. a=9+1=10                                         
19                                          Value of a(i.e. 10) is printed.                             10   

In this way, the statements are executed and we obtain our required output on the screen.            
                                                     







No comments:

Post a Comment