Prefix and postfix increment/decrement operators differ in whether they increment/decrement the variable before or after assignment. With prefix (++i, --i), the operation is performed before assignment, changing the value. With postfix (i++, i--), the original value is assigned before the operation, which doesn't change the value until later. For example, if i=5, then ++i results in i=6, while i++ results in i=6 but uses the original value of 5 for any other calculations in that statement.
1 of 7
Downloaded 20 times
More Related Content
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