Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 7 of 7
Thread: an easy question

07152011, 07:19 PM #1
 Join Date
 Jul 2011
 Location
 london / uk
 Posts
 5
 Thanks
 3
 Thanked 0 Times in 0 Posts
an easy question
hi guys,
l am trying to learn javascript, although certain things seem to be easy to understand, l still have plenty of questions.
so l was reading some articles on w3schools and l was an interesting table about operations.
http://i1087.photobucket.com/albums/...kesnosense.png
if you check above's table you will see at the increment section as well decrement , you will see that in some cases the x and y dont have the same value, now l was just wondering, what is the difference between x=++y and x=y++ ?
07152011, 07:36 PM
#2
07152011, 07:47 PM
#3
 Join Date
 Jul 2011
 Location
 london / uk
 Posts
 5
 Thanks
 3
 Thanked 0 Times in 0 Posts
l am still not understanding the difference between x=++y and x=y++ .
do you actually mean x=++y is adding 1 ? or x=y++ is adding 1 ???
originally it says :
x=++y x=6 y=6
x=y++ x=5 y=6
07152011, 08:16 PM
#4
so let's look at this statement
and let's assume for this example that y=5Code:x=++y
what is happening is it is taking y and adding 1
"++y"... now after that is done we set x = to it, thus making "++y" equal to 6 and then setting x equal to 6.
then the other statement
now we are taking x and setting it equal to y so now x=5... now we will increment x (y is now out of the equation) so the "++" applies to the x only making it x=6 and y=5Code:x=y++
07152011, 09:14 PM
#5
When in doubt, try it out ...
Code:<script type="text/javascript"> var x = 5; var y = 5; var str = 'From original: \nx='+x+' and y='+y+'\n'; x = ++y; alert(str+'x=++y\t'+x+' : '+y); x = 5; y = 5; str = 'From original: \nx='+x+' and y='+y+'\n'; x = y++; alert(str+'x=y++\t'+x+' : '+y); x = 5; y = 5; str = 'From original: \nx='+x+' and y='+y+'\n'; x = y; alert(str+'x=y\t'+x+' : '+y); x = 5; y = 5; str = 'From original: \nx='+x+' and y='+y+'\n'; x = y; alert(str+'x=y\t'+x+' : '+y); </script>
07162011, 01:06 AM
#6
Alykins wrote:
Not true in JavaScript, and not true in C#, C, C++, Java, PHP, or any other language I know of.now we are taking x and setting it equal to y so now x=5... now we will increment x (y is now out of the equation) so the "++" applies to the x only making it x=6 and y=5Code:x=y++
*************
Learn about PRECEDENCE OF OPERATORS.
The ++ operator, either preincrement or postincrement, has the highest prececdence. So that operator will *ALWAYS* take place before the assignment (=) operator.
Postincrement and postdecrement are weird operators. They came about in C because when C was invented computers, in general, didn't have the wealth of registers and operations built in that they have today. Those operators were and attempt to have two things happen at once for very low CPU cost.
So the *effect* of using a postincrement is this:
 Get the value of the variable (or element) that the operator refers to.
 Hold on to that value.
 Increment the variable (or element).
 Retrieve the value that was held onto.
It was typically implement in machines of that time with code such as:
 Load Y into the accumulator
 Push the accumulator on the stack
 Increment the accumulator
 Store the accumulator into Y
 Pop the accumulator off the stack
So, if y is 5, then doing:
will assign 5 (the original value of y) to x *AND* will bump the value of y to 6.Code:x = y++;
Notice that I said "variable or element".
The code applies to array elements and object fields as well:
Result: x will be 2, ar[2] will be 3!Code:var ar = [0,1,2,3,4]; var x = ar[2]++;
Anyway, this all dates back to AT LEAST the beginnings of the C language, since the original K&R book was published in 1978.
(And, yes, I really helped write a C compiler in 1980 and yes, we really did use the machine code shown above to implement the postincrement operator. And the "accumulator" was all of 16 bits long.)
07162011, 01:07 AM
#7
p.s.: Now go try it all in C# and see that it's the same there.