+ 5
Can some one explain prefix and postfix increment in simple words
7 Antworten
+ 6
Do you mean prefix increment and postfix increment? Prefix and postfix simply means something which comes in front of and after a term. If it's about the increment operators (++), you can refer to the existing threads:
https://www.sololearn.com/Discuss/407846/?ref=app
+ 5
int y=1,x;
x=y++;
cout<<x<<y;
Then output is
x=1 and y=2.
Similarly if the equation would be x=++y then the output would have been, x=2 and y=2.
+ 4
Prefix uses the rule change then use, ie, value is first incremented then changed. Postfix uses the rule use then change, ie, the is used first in the equation then changed
+ 4
Remember these r for C++.
Hope these answers have helped u
+ 3
I don't think I can explain the prefix notation better than Wikipedia: https://en.m.wikipedia.org/wiki/Polish_notation#Explanation
Postfix is just its reverse, and is explained here: https://en.m.wikipedia.org/wiki/Reverse_Polish_notation#Explanation
If you are confused about something even after reading those, let us know 😊
Edit: Now I see that you wanted to know about pre- and post- increment operators. In that case my comment is not so relevant. Please check out the link Hatsy shared.
+ 3
prefix - will be calculated first then proceed to the next operation
a = ++i; // first add 1 to i then assign value
postfix - first calculate all the operations then increment/decrement
b = i—; // first assign value of i to b then remove 1 from i
+ 2
increment and decrement
consider the following code in php:
$a = 2;
$b = $a++;
echo $b;
As you see A is 2, B is assigned A which is 2, Then A is incremented (postfix). so the print and answer of B is 2 and A is 3.
----------------------------------
$a = 2;
$b = ++$a;
echo $b;
As you see A is 2, B is incremented (prefix), Then assigned A which is 2. so the print and answer of B is 3 and A is also 3.
in first example B is assigned to A and then incremented, but in second example B is first incremented and then assigned to A.
i hope that it helps.