Tutorial#14 - Increment and Decrement Operators in C++

Increment and Decrement Operators

Increment and Decrement Operators:

Now that you know how to declare a variable and enter data into a variable, in this section, you will learn about two more operators: the increment and decrement operators. These operators are used frequently by C++ programmers and are useful
programming tools.
Suppose count is an int variable. The statement:
count = count + 1;
increments the value of count by 1. To execute this assignment statement, the computer first evaluates the expression on the right, which is count + 1. It then assigns this value to the variable on the left, which is count.
As you will see, such statements are frequently used to count how many times certain things have happened. To expedite the execution of such statements, C++ provides the increment operator, ++ (two plus signs), which increases the value of a variable by 1, and the decrement operator, -- (two minus signs), which decreases the value of a variable by 1. Increment and decrement operators each have
Two forms, pre and post. The syntax of the increment operator is:
Pre-increment: ++variable
Post-increment: variable++
The syntax of the decrement operator is:
Pre-decrement: --variable
Post-decrement: variable--
Let’s look at some examples. The statement:
++count;
    or:
count++;
increments the value of count by 1. Similarly, the statement:
--count;
    or:
count--;
decrements the value of count by 1.
Because both the increment and decrement operators are built into C++, the value of the variable is quickly incremented or decremented without having to use the form of an assignment statement.

Comments