by using prefix and postfix the answer comes same eg; int x=42; x++;. //output=43 int x=42; ++x;. //output=43 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

by using prefix and postfix the answer comes same eg; int x=42; x++;. //output=43 int x=42; ++x;. //output=43

1st Jul 2016, 7:44 AM
Ankit
Ankit - avatar
3 Answers
+ 12
difference is if we say x=0 and use this code y=x++; the answer is x=1 but y=0 _____ but if we use code like this y=++x; the answer is x=1 y=1 in other word y=x++ y=x x=x+1 and y=++x x=x+1 y=x
1st Jul 2016, 2:29 PM
Artemis Ariamehr
Artemis Ariamehr - avatar
+ 3
Since you are using only 1 operation per statement thew is no question of precedence. However if you use a statement which has more than 1 operator per statement like x=y++; or x=++y; then there is a difference
1st Jul 2016, 9:38 AM
Midhun Mathew
+ 1
The answer can be explained a bit more simply : when you write "int x=42; y= x++;" that means that you first assign x to y (y=x) and then add +1 to x variable. In that case the y variable output will be the same as it was before adding the ++. And if you use "int x=42; y=++x;" then that means that you first add +1 to x variable and then assign it to y variable, so the output with ++x is 43
6th Jul 2016, 7:38 AM
lolmlol