ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
What is difference between Prefix & Postfix
(++i ,--i & i++ , i--)
So what is the difference????
Prefix Postfix
The ++ or -- symbol is
used before the variable.
The ++ or -- symbol is used
after the variable.
Example: ++i or --i Exmple i++ or i--
It means : i= i+1 or It means: i= i+1 or
i= i-1 i= i-1
It will be easy for u to understand these if u keep two things in
ur mind. That is :
In prefix : First increment / decrement by 1 then assign .
In postfix: First assign then increment/ decrement by 1 .
In prefix:
int i =5;
++i;
Output: 6
It works like:
i = ++i ; // First increment
= i+1;
= 5+1;
= 6;
int i = 6; // Then assign
Note:
In program the last & final value is accepted.
In postfix:
int i =5;
i++;
Output: 6
It works like:
int i = 5; // First assign
i =i++; // Then increment
= i+1;
= 5+1;
=6;
Note:
In program the last & final value is accepted.
**In prefix : First increment / decrement then assign**
If i and j are two variables then :
int i, j ;
i=9;
j=++i;
Output: i & j is : 10 10
Here, Compiler first add 1 to the initial value i=9. Then it
use the new incremented value in the same statement.
**In prefix : First assign then increment / decrement **
If i and j are two variables then :
int i, j ;
i=9;
j=i++;
Output: i & j is : 10 9
Try it yourself:
- -i & i- -
Hint: i = i -1;

More Related Content

What is difference between Prefix & Postfix (++i ,--i & i++ , i--)

  • 1. What is difference between Prefix & Postfix (++i ,--i & i++ , i--)
  • 2. So what is the difference???? Prefix Postfix The ++ or -- symbol is used before the variable. The ++ or -- symbol is used after the variable. Example: ++i or --i Exmple i++ or i-- It means : i= i+1 or It means: i= i+1 or i= i-1 i= i-1
  • 3. It will be easy for u to understand these if u keep two things in ur mind. That is : In prefix : First increment / decrement by 1 then assign . In postfix: First assign then increment/ decrement by 1 .
  • 4. In prefix: int i =5; ++i; Output: 6 It works like: i = ++i ; // First increment = i+1; = 5+1; = 6; int i = 6; // Then assign Note: In program the last & final value is accepted. In postfix: int i =5; i++; Output: 6 It works like: int i = 5; // First assign i =i++; // Then increment = i+1; = 5+1; =6; Note: In program the last & final value is accepted.
  • 5. **In prefix : First increment / decrement then assign** If i and j are two variables then : int i, j ; i=9; j=++i; Output: i & j is : 10 10 Here, Compiler first add 1 to the initial value i=9. Then it use the new incremented value in the same statement.
  • 6. **In prefix : First assign then increment / decrement ** If i and j are two variables then : int i, j ; i=9; j=i++; Output: i & j is : 10 9
  • 7. Try it yourself: - -i & i- - Hint: i = i -1;