Increment and Decrement Operators

It is very common in programming to add 1 to a variable or subtract 1 from a variable:

int a, b;
a = a + 1; // add 1 to a
b = b - 1; // subtract 1 from b

C++ has two operators that deal with this kind of operation. They are both unary operators:

increment operator:
++
decrement operator: --

The example above could be written:

a++;   // add 1 to a
b--;   // subtract 1 from b

The operators can be written either before or after the variable:

a++;   // post-increment
++a;   // pre-increment
b--;   // post-decrement
--b;   // pre-decrement

In this class, it won't matter which one you use. Later on, you will learn the subtle differences.

Previous slide
Next slide

Back to Lesson 9 Index
Back to Outline