View Full Version : an easy question

doyouknow

07-15-2011, 06:19 PM

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/j474/london110685/makesnosense.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++ ?

alykins

07-15-2011, 06:36 PM

i don't know the exact order of it but (i pretty sure anyways) one is setting x=y and then adding one to x, the other is setting x=y and then adding one to y

doyouknow

07-15-2011, 06:47 PM

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

alykins

07-15-2011, 07:16 PM

so let's look at this statement

x=++y

and let's assume for this example that y=5

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

x=y++

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=5

jmrker

07-15-2011, 08:14 PM

When in doubt, try it out ... :thumbsup:

<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>

Old Pedant

07-16-2011, 12:06 AM

Alykins wrote:

x=y++

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=5

Not true in JavaScript, and not true in C#, C, C++, Java, PHP, or any other language I know of.

*************

Learn about PRECEDENCE OF OPERATORS.

The ++ operator, either pre-increment or post-increment, has the highest prececdence. So that operator will *ALWAYS* take place before the assignment (=) operator.

Post-increment and post-decrement 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 post-increment 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:

x = y++;

will assign 5 (the original value of y) to x *AND* will bump the value of y to 6.

Notice that I said "variable or element".

The code applies to array elements and object fields as well:

var ar = [0,1,2,3,4];

var x = ar[2]++;

Result: x will be 2, ar[2] will be 3!

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 post-increment operator. And the "accumulator" was all of 16 bits long.)

Old Pedant

07-16-2011, 12:07 AM

p.s.: Now go try it all in C# and see that it's the same there.

Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.