Can some one explain prefix and postfix increment in simple words | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Can some one explain prefix and postfix increment in simple words

28th Oct 2018, 5:10 AM
AB Plays
AB Plays - avatar
7 Answers
+ 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
28th Oct 2018, 5:15 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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.
28th Oct 2018, 6:30 AM
Ayush Rawat
Ayush Rawat - avatar
+ 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
28th Oct 2018, 6:26 AM
Ayush Rawat
Ayush Rawat - avatar
+ 4
Remember these r for C++. Hope these answers have helped u
28th Oct 2018, 6:31 AM
Ayush Rawat
Ayush Rawat - avatar
+ 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.
28th Oct 2018, 5:31 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 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
28th Oct 2018, 10:21 PM
Sergei No
Sergei No - avatar
+ 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.
28th Oct 2018, 3:49 PM
Murtaza Mukhtar
Murtaza Mukhtar - avatar