Difference between x++ and ++x

# Thread: Difference between x++ and ++x

1. Senior Member
Join Date
Dec 1969
Posts
353

## Difference between x++ and ++x

I never got the difference between x++ and ++x. Can someone explain?

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## It's all in the wrists...

&nbsp;<BR>&#060;HTML&#062;&#060;BODY&#062;<BR><BR> &#060;SCRIPT Language=JavaScript&#062;<BR><BR>var x = 17;<BR><BR>document.write("We start with x=" + x <BR>&nbsp; &nbsp; + " and then set y to the value of ++x: " + (y = ++x)<BR>&nbsp; &nbsp; + "&#060;P&#062;"<BR>&nbsp; &nbsp; + "Now x=" + x + " and y=" + y + "&#060;P&#062;"<BR>&nbsp; &nbsp; + " and we set the value of z to x++: " + (z = x++)<BR>&nbsp; &nbsp; + "&#060;P&#062;"<BR>&nbsp; &nbsp; + "Ending up with x=" + x + ", y=" + y + ", z=" + z<BR>&nbsp; &nbsp; + "&#060;P&#062;"<BR>&nbsp; &nbsp; );<BR><BR>&#060;/SCRIPT&#062;<BR><BR>&#060;/BODY&#062;&#060;/HTML&#062;<BR><BR>Try it. Does that explain it?<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
412

## RE: Difference between x++ and ++x

the difference is in the order of precedance<BR>++x adds one to x, then uses the value of x<BR>x++ uses x and then adds a one to it.<BR><BR>Bill&#039;s example shows this just great.<BR>

4. Senior Member
Join Date
Dec 1969
Posts
353

## RE: Difference between x++ and ++x

x++ is shorthand for x = x + 1.<BR><BR>++x is shorthand for... what?

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## No, you are wrong...

&nbsp;<BR>*BOTH* are shorthand for x=x+1<BR><BR>But the difference is *WHEN* the increment takes place!<BR><BR>If you just code <BR>&nbsp; &nbsp; x++;<BR>or<BR>&nbsp; &nbsp; ++x;<BR>it doesn&#039;t matter which you use!<BR><BR>Go look again--carefully, this time!--at my example code.<BR><BR>To repeat it explicitly:<BR><BR>var x = 17;<BR><BR>var y = ++x;<BR>-- that says: *first* increment x and *then* use that incremented value <BR>-- as what will be assigned to variable y<BR>------ so x is bumped to 18 and then 18 is assigned to y.<BR><BR>var z = x++;<BR>-- that says: *first* ASSIGN the value of x to variable z and *THEN* increment x!<BR>------ so 18 (current value of x) is assigned to z and *then* x is bumped to 19<BR><BR>Okay?<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
353

## Sorry, I'm a bit stupid...

I ususally just use x++ on its own, no y =<BR><BR>That&#039;s probably why I never understood it.

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## It's why you never noticed ...

...any difference:<BR><BR>&#062; I ususally just use x++ on its own<BR><BR>Since when you do that, there isn&#039;t any.<BR><BR>Not stupid; just lack of experience.<BR><BR>If you care, one of the "tricks" in C programming is copying a string this way:<BR><BR>// all the set up code:<BR>str = "abcdefg";<BR>newStr = new char[100]; // or whatever<BR><BR>from = str;<BR>to = newStr;<BR><BR>// and then this does the copy!<BR>while( *newStr++ = *str++ );<BR><BR>As an alternative, to avoid the from/to variables:<BR><BR>i = 0;<BR>while( newStr[i] = str[i++] );<BR><BR>The funny thing is, people who learned C years ago tend to use the first version, because C was developed on PDP11 and similar machines which had hardware the matched that construction. But with the x86 architecture (i.e., Pentium chips) the second is one instruction per loop shorter.<BR><BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•