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
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;
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.