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

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.

Leave a Comment