Pre Increment
Pre increment is used to first increase value of the variable and then perform assignment.
Let’s see an example-
int a ,x;
a=5; // a is assigned 5
x=++a; //Pre increment
By using pre increment operator value of a will increase to 6 and x will assigned value of a i.e 6*/
x = ++a is equivalent to writing these lines of code.
a = a+1;//first increase a
x = a; // then assign a value to x
Post Increment
Post increment operator is use to first perform assignment and then increase the value of variable.
Let’s see an example-
int a ,x;
a=5; //
x=a++; //Post increment
By using post increment operator x will be assigned 5 and then value of a will increase to 6
x = a++; is equivalent to writing these lines of code.
x = a; // first assigning a to x
a = a+1; // then increase value of a
No comments:
Post a Comment