Post increments vs pre increment | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Post increments vs pre increment

x = 1; // ++x VS x++ I'm at mess, forgot the basics. So I thought that when you print variable x, with pre increment, the value is 2, while post increment will give value 1, and nothing happens. Because you just call them, but Im wrong. So when we set variable x to post increment, then just call them, it will trigger increment. What is the condition of post increment? How is it trigger and when will increment not triggered?

23rd Sep 2021, 5:42 AM
Jonathan P. Jundarino
Jonathan P. Jundarino - avatar
4 Answers
+ 3
Pre increment first assigns the value then execute the line, post increment first execute the line then assign the value. When you write ++x first the value of x will increase then the line execute. When you write x++ first it execute the line and then increase the value of x. If you write y = ++x; First the value of x increase by 1 then it execute the line ie, y is assigned as x. Now if u write y = x++; First y is assigned as x , then the value of x increase. Same happen while printing. Print(++x) will first increase x by 1 then print. Print (x++) will first print the value of x then increase the value by 1. Hope you got it....
23rd Sep 2021, 5:52 AM
Arun Ruban SJ
Arun Ruban SJ - avatar
+ 1
Arun Ruban SJ thanks I understand now by the word "line" or statement, so the declaration of statement, nothing will happen in post increment, but the second declaration, will trigger it, thanks, very appreciated, answered fast. ❤️
23rd Sep 2021, 6:00 AM
Jonathan P. Jundarino
Jonathan P. Jundarino - avatar
+ 1
Yeah it's statement and not line sorry for that
23rd Sep 2021, 6:04 AM
Arun Ruban SJ
Arun Ruban SJ - avatar
+ 1
It's okay, still answered and I appreciate it
23rd Sep 2021, 6:06 AM
Jonathan P. Jundarino
Jonathan P. Jundarino - avatar