Next: Operator Precedence, Previous: Assignment Ops, Up: Expressions
Increment operators increase or decrease the value of a variable by 1. The operator to increment a variable is written as `++'. It may be used to increment a variable either before or after taking its value.
For example, to pre-increment the variable x, you would write
++
x. This would add one to x and then return the new
value of x as the result of the expression. It is exactly the
same as the expression x =
x + 1
.
To post-increment a variable x, you would write x++
.
This adds one to the variable x, but returns the value that
x had prior to incrementing it. For example, if x is equal
to 2, the result of the expression x++
is 2, and the new
value of x is 3.
For matrix and vector arguments, the increment and decrement operators work on each element of the operand.
Here is a list of all the increment and decrement expressions.
++
x =
x + 1
.
--
x =
x - 1
.
++
--
It is not currently possible to increment index expressions. For
example, you might expect that the expression v(4)++
would
increment the fourth element of the vector v, but instead it
results in a parse error. This problem may be fixed in a future
release of Octave.