Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Location
    london / uk
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Smile 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++ ?

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,806
    Thanks
    41
    Thanked 199 Times in 198 Posts
    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

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    New to the CF scene
    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

  • #4
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,806
    Thanks
    41
    Thanked 199 Times in 198 Posts
    so let's look at this statement
    Code:
     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
    Code:
    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

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,097
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    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>

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,536
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    Alykins wrote:
    Code:
    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:
    Code:
    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:

    Code:
    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.)

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,536
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    p.s.: Now go try it all in C# and see that it's the same there.


  •  

    Posting Permissions

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