How to use the ++ line in both prefix and postfix method? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to use the ++ line in both prefix and postfix method?

"int x=3; int y=x++;" and "int x=3; int y=++x;" What are the difference in the value of x?

16th Mar 2017, 3:05 PM
Zahiruzzaman Chowdhury
Zahiruzzaman Chowdhury - avatar
1 Answer
+ 1
The first version says set y equal to x and then add 1 to x, so here y is 3 and x is 4. The second version says add 1 to x and then set y equal to x, so here x is 4 and y is 4. In both cases x is the same, you are starting at 3 and adding 1 to it, the difference is the value held in y.
16th Mar 2017, 11:30 PM
Shane