Helo guys , I am having trouble understanding increments ..what is the difference between z++ & ++z ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Helo guys , I am having trouble understanding increments ..what is the difference between z++ & ++z ?

C++ increments /decrements

13th Mar 2017, 2:17 PM
Chanda Mataka
Chanda Mataka - avatar
4 Answers
+ 4
Let me give you an illustration. z=0; z++; // z is still 0 z; // now z is 1 ++z; // z is 2.
13th Mar 2017, 2:29 PM
Hummad Khan
Hummad Khan - avatar
+ 4
Look this equation imagine x=5 y=x++ above equation can be divided in to two parts 1. y=x 2. x=x+1 after executing these 2 still the value of y is 5. if the eqaution was like this y=++x 1. x=x+1 2. y=x now the value of y is 6; Hope You'll understand it
13th Mar 2017, 4:38 PM
Yasiru Nayanajith
Yasiru Nayanajith - avatar
+ 3
with z++, the variable z is used and then incremented while ++z means that z is incremented before it is used.
13th Mar 2017, 2:26 PM
Katlego S. Maja
+ 2
When ++ is before the variable, it as increased by 1 e.g x=4 cout<<++x; output is 5: and when the ++ is after the variable it stays the same at the moment e.g x=4 cout<<x++; output is 4
13th Mar 2017, 4:53 PM
Solomon Njobvu
Solomon Njobvu - avatar