8.7 Increment Operators
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
-
This expression increments the variable x. The value of the expression is the new value of x. It is equivalent to the expression
x = x + 1
. --x
-
This expression decrements the variable x. The value of the expression is the new value of x. It is equivalent to the expression
x = x - 1
. x++
-
This expression causes the variable x to be incremented. The value of the expression is the old value of x.
x--
-
This expression causes the variable x to be decremented. The value of the expression is the old value of x.
© 1996–2020 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/v5.2.0/Increment-Ops.html