Can anyone explain better the difference and the reason of 'x++' and '++x' to me? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anyone explain better the difference and the reason of 'x++' and '++x' to me?

I understand that 'x++' is adding to 'x' by 1 correct? I had to come back here from the next section even because it is just a bit strange to me...

24th Jul 2016, 1:07 AM
Luke
8 Answers
+ 5
int y; int x; //posfix, first assign x value to y. after x is incremented. x = 0; y = x++; Console.WriteLine(y); //output 0 //prefix, first x is incremented, after assign x value to y. x = 0; y = ++x; Console.WriteLine(y); //output 1
24th Jul 2016, 2:56 AM
Ricardo Pucca
Ricardo Pucca - avatar
+ 3
both method add +1 to x. but using x++ you can use the original value before add +1 to x.
24th Jul 2016, 3:26 AM
Ricardo Pucca
Ricardo Pucca - avatar
+ 1
x++ // Uses the variable and then adds one. ++x // Adds one to the variable and then uses it.
24th Jul 2016, 10:13 AM
Andreas BeEm Kvist
Andreas BeEm Kvist - avatar
0
Would you be able to say 'x++' includes x, whereas '++x' does not?
24th Jul 2016, 3:12 AM
Luke
0
its either im dumb for not understanding this only...or im just dumb...:/
24th Jul 2016, 4:57 AM
Iyann
Iyann - avatar
0
int y; int x; printf("Enter integers:\n"); scanf("%d", &y); for(x=0;x<5;x++) return 0;
26th Jul 2016, 11:55 AM
Ben Cla
Ben Cla - avatar
0
For x++ the value of x is assigned first and then it is incremented by 1;;;;but where as coming to ++x the value of x is incremented firstly and then the value is assigned to x;;;;;; Ex::: int x=3; X++=3+1=4 ++x=1+3=4
5th Aug 2016, 8:23 AM
vamsi
vamsi - avatar
- 1
if you have two variables, var1 & var2. if var1=9 var2=var1++ assign 9 to var2, then increment var1=10 var2=++var1 var1 is10, then increment var2=10
26th Jul 2016, 11:59 AM
Ben Cla
Ben Cla - avatar