The difference between += and =+

前端 未结 8 1232
陌清茗
陌清茗 2020-12-01 05:28

I\'ve misplaced += with =+ one too many times, and I think I keep forgetting because I don\'t know the difference between these two, only that one

相关标签:
8条回答
  • 2020-12-01 06:03

    It's simple.

    x += 1 is the same as x = x + 1 while

    x =+ 1 will make x have the value of (positive) one

    0 讨论(0)
  • += is a compound assignment operator - it adds the RHS operand to the existing value of the LHS operand.

    =+ is just the assignment operator followed by the unary + operator. It sets the value of the LHS operand to the value of the RHS operand:

    int x = 10;
    
    x += 10; // x = x + 10; i.e. x = 20
    
    x =+ 5; // Equivalent to x = +5, so x = 5.
    
    0 讨论(0)
提交回复
热议问题