SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Amsterdam, The Netherlands
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question about decrement and increment computations

    Hello,

    I finished the W3 Schools Javascript beginners course a while ago, and I passed the test. But there is one thing I don't understand, and that is the increment and decrement computations under JavaScript Arithmetic Operators on http://www.w3schools.com/js/js_operators.asp.

    Could someone explain them to me? I think that y++ means: the next object after y. But then I still don't understand the computation that follows. And I don't know at all what ++y means.

    Thanks in advance.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Frank S View Post
    Could someone explain them to me? I think that y++ means: the next object after y. But then I still don't understand the computation that follows. And I don't know at all what ++y means.
    Those are parts that come from c, and are of little use now. The reason why they are of little use is that once you begin to write code with them your code becomes difficult to understand, and poorly understood code is bad code.

    ++y is a pre-increment where y is incremeneted before it is used in a calculation, and y++ is a post-increment where y is incremented after a calculation is done.

    Code javascript:
    var x = 0, y = 2;
    x = ++y + y++ - ++y;

    What is the value of x, and what does y end up being?
    Is the second pre-increment more than or the same as the first pre-increment from the same statement?
    How does the value of y change throughout that single calculation?

    It's bloody confusing, that's what!

    It's hard to easily understand what is happening there, so rewriting should be performed instead.

    Code javascript:
    var x = 0, y = 2;
    y += 1;
    x = y; // ++y
    x += y;
    y += 1; // y++
    y += 1;
    x -= y; // ++y

    And then refactor in to easier to understand code.

    x = (y + 1) + (y + 1) - (y + 3);
    which is
    x = y - 1;

    Code javascript:
    var x = 0, y = 2;
    x = y - 1;
    y += 3;

    The first piece of code is a line shorter, but the last piece of code is infinitely easier to understand.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Frank S View Post
    I think that y++ means: the next object after y. But then I still don't understand the computation that follows. And I don't know at all what ++y means.

    Thanks in advance.
    These 2 demos might help:

    Code:
    var count = 0;
    
    if(count++ < 5) {
    
         // but at this point count now = 1
    
    }
    is the same as saying

    Code:
    var count = 0;
    
    if(0 < 5) {
    
         // but at this point count now = 1
    
    }
    but

    Code:
    var count = 0;
    
    if(++count < 5) {
    
         // but at this point count now = 1
    
    }
    is the same as saying

    Code:
    var count = 0;
    
    if(1 < 5) {
    
         // but at this point count now = 1
    
    }
    As soon as count++ or ++count are executed, the value of count is incremented by 1. ++ before count means count is first incremented and the new value is then used as the value of count in the statement and ++ after count means the current value of count is first used in the statement and count is then incremented by 1 after the statement is executed.

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Amsterdam, The Netherlands
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks a bunch, guys! I'm in the middle of something that doesn't allow me the time to sit down for this, which I need, but I will certainly report back to you.

  5. #5
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Amsterdam, The Netherlands
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Although I needed http://help.dottoro.com/ljpnvikr.php to understand the matter, I do get it now, and your explanations weren't far off.

  6. #6
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Frank S View Post
    .....I do get it now, and your explanations weren't far off.
    glad you sorted it out.

    I thought the explanations in this thread were spot on


Bookmarks

Posting Permissions

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