Why doesn't the shorthand arithmetic operator ++ after the variable name return 2 in the following statement?

前端 未结 3 1091
-上瘾入骨i
-上瘾入骨i 2020-11-27 22:10

I have a very simple arithmetic operator but am at my wits end why it doesn\'t return 2. The code below returns 1. I thought that x++ equates to x = x + 1;

C

3条回答
  •  醉梦人生
    2020-11-27 22:27

    PostIncrement(variable++) & PostDecrement(variable--)

    When you use the ++ or -- operator after the variable, the variable's value is not incremented/decremented until after the expression is evaluated and the original value is returned. For example x++ translates to something similar to the following:

    document.write(x);
    x += 1;
    

    PreIncrement(++variable) & PreDecrement(--variable)

    When you use the ++ or -- operator prior to the variable, the variable's value is incremented/decremented before the expression is evaluated and the new value is returned. For example ++x translates to something similar to the following:

    x += 1;
    document.write(x);
    

    The postincrement and preincrement operators are available in C, C++, C#, Java, javascript, php, and I am sure there are others languages. According to why-doesnt-ruby-support-i-or-i-increment-decrement-operators, Ruby does not have these operators.

提交回复
热议问题