<html lang="en"> <head> <title>Increment and decrement operators - Frequently asked questions about Octave (with answers)</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="Frequently asked questions about Octave (with answers)"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Octave-Features.html#Octave-Features" title="Octave Features"> <link rel="prev" href="Exclamation-mark-as-not-operator.html#Exclamation-mark-as-not-operator" title="Exclamation mark as not operator"> <link rel="next" href="Unwind_002dprotect.html#Unwind_002dprotect" title="Unwind-protect"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Increment-and-decrement-operators"></a> <p> Next: <a rel="next" accesskey="n" href="Unwind_002dprotect.html#Unwind_002dprotect">Unwind-protect</a>, Previous: <a rel="previous" accesskey="p" href="Exclamation-mark-as-not-operator.html#Exclamation-mark-as-not-operator">Exclamation mark as not operator</a>, Up: <a rel="up" accesskey="u" href="Octave-Features.html#Octave-Features">Octave Features</a> <hr> </div> <h3 class="section">5.8 Increment and decrement operators</h3> <p><a name="index-Increment-operators-4"></a><a name="index-Decrement-operators-5"></a><a name="index-Operators_002c-increment-6"></a><a name="index-Operators_002c-decrement-7"></a> If you like the ‘<samp><span class="samp">++</span></samp>’, ‘<samp><span class="samp">+=</span></samp>’ etc operators, rejoice! Octave includes the C-like increment and decrement operators ‘<samp><span class="samp">++</span></samp>’ and ‘<samp><span class="samp">--</span></samp>’ in both their prefix and postfix forms, in addition to ‘<samp><span class="samp">+=</span></samp>’, ‘<samp><span class="samp">-=</span></samp>’, ‘<samp><span class="samp">*=</span></samp>’, ‘<samp><span class="samp">/=</span></samp>’, ‘<samp><span class="samp">^=</span></samp>’, ‘<samp><span class="samp">.*=</span></samp>’, ‘<samp><span class="samp">./=</span></samp>’, and ‘<samp><span class="samp">.^=</span></samp>’. <p>For example, to pre-increment the variable <var>x</var>, you would write <code>++</code><var>x</var>. This would add one to <var>x</var> and then return the new value of <var>x</var> as the result of the expression. It is exactly the same as the expression <var>x</var><code> = </code><var>x</var><code> + 1</code>. <p>To post-increment a variable <var>x</var>, you would write <code>x++</code>. This adds one to the variable <var>x</var>, but returns the value that <var>x</var> had prior to incrementing it. For example, if <var>x</var> is equal to 2, the result of the expression <code>x++</code> is 2, and the new value of <var>x</var> is 3. <p>For matrix and vector arguments, the increment and decrement operators work on each element of the operand. </body></html>