0

Is there a better way to explain increaments?

I am still a little bit confused about increaments like x++ or ++x especially when it comes to the prefix/postfix part. Could someone please try explaining it in a different way than they did in the lesson? Thanks!

13th May 2017, 6:22 AM
Emily
3 Answers
13th May 2017, 6:28 AM
MR Programmer
MR Programmer - avatar
+ 1
I'll try to give an example, I hope it will help: y = x++; means the same as y = x; x = x + 1. y = ++x; means the same as x = x + 1; y = x. Basically x++ means that x is used before it is incremented, whereas ++x means that you increment x before using it. I don't remember how they explained it in the lesson, but this is the best way I could explain it.
13th May 2017, 6:45 AM
Mrs. Coder123
Mrs. Coder123 - avatar
0
Prefix example: int x = 3; int y = ++x; // x is 4, y is 4 Postfix example: int x = 3; int y = x++; // x is 4, y is 3
13th May 2017, 7:17 AM
Ahmed Abdel Hafeez
Ahmed Abdel Hafeez - avatar