Results 1 to 20 of 20

Thread: += vs =+

  1. #1

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Resolved += vs =+

    Is there a difference between these two lines? (Aside from the fact that they are indeed different)
    Code:
    x += 1;
    x =+ 1;
    Last edited by crptcblade; Apr 13th, 2005 at 11:28 AM.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: += vs =+

    I'm not a JAva person, but I do know that in C they are are different. If it works the same, I think that the first one will inc the variable by one. The second one, will assign 1 to the vairable first, then increment it (to 2). It would be the same as x = 2;
    I *think* that's how it works. I could be wrong, but from my days of C, that sounds about right. It all has to do with when the variable is assigned it's value, and when it's incremented (or decremented). The order of the operators determines the order in which that happens.

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: += vs =+

    ??? ???


    As far as I know, there is nothing called =+. I looked it up, and in my book it said nothing about it. And when I just tested it, it gave me this (in netbeans):

    ------------
    myVar = 5
    m = 5
    ------------

    myVar += m (gives myVar = 10)
    myVar =+ m (gives myVar = 5, it is treated as = (+m))
    myVar =- m (gives myVar = -5, it is treated as = (-m))
    -----------


    What techgnome is talking about in C, is for ++x vs x++, where in the latter one is usualy slower then the first one. You will probably don't see that performance penalty for normal datatypes, but if you use it on classes, then the over head is getting bigger. The reason is that (for most common implementations of) postfix operators retain a temporary copy of original variable and because the return value is returned by value, not by reference.

  4. #4

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: += vs =+

    Quote Originally Posted by NoteMe
    ??? ???


    As far as I know, there is nothing called =+. I looked it up, and in my book it said nothing about it. And when I just tested it, it gave me this (in netbeans):

    ------------
    myVar = 5
    m = 5
    ------------

    myVar += m (gives myVar = 10)
    myVar =+ m (gives myVar = 5, it is treated as = (+m))
    myVar =- m (gives myVar = -5, it is treated as = (-m))
    -----------
    I thought so. Its been a while since I've done anything in Java, and this quack instructor I have made mention of it.

    Thanks
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: += vs =+

    No problemo, mr. Just glad to help out. Hope to see you around more here now...


    ØØ

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Thumbs up Re: += vs =+

    Quote Originally Posted by NoteMe
    ??? ???


    As far as I know, there is nothing called =+. I looked it up, and in my book it said nothing about it. And when I just tested it, it gave me this (in netbeans):

    ------------
    myVar = 5
    m = 5
    ------------

    myVar += m (gives myVar = 10)
    myVar =+ m (gives myVar = 5, it is treated as = (+m))
    myVar =- m (gives myVar = -5, it is treated as = (-m))
    -----------


    What techgnome is talking about in C, is for ++x vs x++, where in the latter one is usualy slower then the first one. You will probably don't see that performance penalty for normal datatypes, but if you use it on classes, then the over head is getting bigger. The reason is that (for most common implementations of) postfix operators retain a temporary copy of original variable and because the return value is returned by value, not by reference.
    I thought I was talking out my arse.... thanks for confirming that for me.

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: += vs =+

    Quote Originally Posted by techgnome
    I thought I was talking out my arse.... thanks for confirming that for me.

    Tg


    Nhaaa...you where pretty much right about it...



    ØØ

  8. #8
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177

    Re: += vs =+

    Quote Originally Posted by NoteMe
    ??? ???
    What techgnome is talking about in C, is for ++x vs x++, where in the latter one is usualy slower then the first one.
    I don't know about one being faster than the other, but the real concern is when x is incremented in the equation.
    ++x - x is incremented and then used in the equation
    x++ - x is used in the equation and then incremented

    So, if
    x = 10;

    then

    y = ++x; // end results; x = 11, y = 11

    whereas

    y = x++; // end results; x = 11, y = 10

  9. #9
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: += vs =+

    Quote Originally Posted by ccoder
    I don't know about one being faster than the other, but the real concern is when x is incremented in the equation.
    ++x - x is incremented and then used in the equation
    x++ - x is used in the equation and then incremented

    So, if
    x = 10;

    then

    y = ++x; // end results; x = 11, y = 11

    whereas

    y = x++; // end results; x = 11, y = 10

    I am right.......tell me if you need proof....

  10. #10
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: += vs =+

    += is a valid operator but =+ is not but i guess the compiler treats =+ as = +, just putting the sign to the integer.

  11. #11

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: += vs =+

    Quote Originally Posted by Dilenger4
    += is a valid operator but =+ is not but i guess the compiler treats =+ as = +, just putting the sign to the integer.
    Yeah, I didn't think it was valid as he was telling us, but its been a while, so I wasn't sure. The Format button in the IDE cleared that one up. Oh, and what's his face, too.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  12. #12
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: += vs =+

    Sorry I had to drag this one up again. But blame Mendhak, it was he that asked me, and I am not sure if I understand it...


    Code:
    i = 0
    
    i = i++;
    
    //now i = 0

    Can anyone explain that? Shouldn't that equal:

    Code:
    i = 0
    
    i = i
    
    i++;
    
    
    //now i = 1
    They are referencing the same place in memory. I don't get it...

    Of course this is a dummy question, because no one would do it (unless they like to write confusing code). But I still don't get why i isn't 1....grrr...



    ØØ

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: += vs =+

    In C, this is a classical code snippet. By the C language specification, it yields undefined behaviour. I don't know about Java.

    However, I can tell you how you arrive at i == 0.
    i -> 0
    > i = i++;
    execute right side
    i -> 1, expr -> 0
    execute assignment
    i -> 0
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: += vs =+

    I must admitt that I didn't understand that.....w00t...is the expression i = i executed before i++ and then after ...ehhh no, that doesn't seem right either...no matter what order I do this in, it ends up to be 1...

  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: += vs =+

    In this line:
    i = i++;

    You first increment i by one, so that i now contains 1. But the result of i++ is still 0, so by replacing the expression on the right side by its result, you get
    i = 0;

    i is 1 by now, but now the reduced statement is executed, and the 1 is replaced by the value assigned, which is 0.

    But if you think that's complicated, try this:

    i = i++ + ++i;

    This line is truly undefined, by the way. A compiler may reject it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  16. #16
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: += vs =+

    Quote Originally Posted by CornedBee
    In this line:
    i = i++;

    You first increment i by one, so that i now contains 1. But the result of i++ is still 0, so by replacing the expression on the right side by its result, you get
    i = 0;

    i is 1 by now, but now the reduced statement is executed, and the 1 is replaced by the value assigned, which is 0.

    But if you think that's complicated, try this:

    i = i++ + ++i;

    This line is truly undefined, by the way. A compiler may reject it.

    I don't even want to think about the last one......but I still don't get why i++ is executed before i = i, i++ is a postfix operator, and are supposed to be executed after the increment. But I can settle with it, as long as I know it. It is a stupid pease of code anyway..

  17. #17
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: += vs =+

    There's no i = i there! It's as simple as that.

    = has a lower precedence than ++. The code is parsed as
    i = (i++);

    Does that help?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  18. #18
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: += vs =+

    a bit, but how does then this work:


    i = 0;
    k = 0;


    i = k++;


    //now i = 0 and k = 1


    as in post number 8 here?

  19. #19
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: += vs =+

    i = k++; State: i = 0, k = 0
    -> operator precedence
    i = (k++); State: i = 0, k = 0
    -> evaluate (k++). k is incremented by one, the result of the expression is k's old value
    i = 0; State: i = 0, k = 1
    -> evaluate (i = 0).
    ; State: i = 0, k = 1
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  20. #20
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: += vs =+

    Ahha....now I totaly get it.....hehe...some things doesn't work too well in my brain, but I got it now. Will rate it when I get my spread covered...


    Thanks a bunch for beeing so patient with an old lady..



    - ØØ -

Posting Permissions

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



Click Here to Expand Forum to Full Width