Increment Decrement Operator in C++

Increment Decrement Operator in C++ with Examples

In this article, I am going to discuss Increment Decrement Operator in C++ with Examples. Please read our previous article where we discussed Compound Assignment Operator in C++ with Examples. The increment or Decrement operator will increment/ Decrement the variable value and assign it back to the variable.

Increment / Decrement Operator in C++:

Let’s understand what are these operators and their use in C++ programming. These are the most commonly used operators in C++ programming. So let us look at the operators. There are two types of operators. One is the increment operator and the second one is the decrement operator. In increment and decrement, again there are 2 types:

Increment Decrement Operator in C++ with Examples

We have taken one example here,
int x = 4;
x = x + 1;

Here we have a variable ‘x’ is having a value of 4 then I have written a statement. We have already discussed this type of statement in our previous article. This statement means that add 1 to x and store it in x itself. So, with this, the value of x will increase by one. The new value of x is 5. The same statement we can write by using the compound assignment operator. We can write the above statement as:

x += 1;

We discussed compound statements in our Previous Articles, so you can learn from there. So, by adding ‘1’ every time we can go on increment the value of x.

What is the use of such a variable? And why do we want to increment it?

This type of variable is mostly useful for counting. For counting purposes, we will have to go on adding 1 to the number. It will go on increasing by 1 every time, so it helps for counting. So here ‘x’ is just like a counter. If we want to count from the beginning then initialize x to 0 then increment it by 1. So, 1 then 2 then 3 and so on. Counting is more common in programming which is the reason C++ has introduced an increment operator. So instead of writing it like: x += 1 or x = x + 1,

we can write it as x++;

This is the same as the above two statements. This statement also means adding one. That ‘++’ means increasing the value by one. So, this is more commonly useful in loops where we will perform repeated statements or repeatedly, we go on incrementing it, so it behaves like a counter. Let us have a look at the difference between pre-increment and post-increment.

Pre and Post Increment in C++:

int x = 6, y;

So here we have an example. Let us understand pre-increment and post-increment in C++ Language. We have a variable ‘x’ having a value of 6, then if I say ‘x++’ then x becomes 7 and even if I say ‘++x’ then also x becomes 7. Whatever the statement may be, the result is the same. Then what is the difference between pre and post? Now let’s again take that example,

int x = 6, y;
if I say, y = ++x;

There are two operations here. One is the increment other one is the assignment. So, which has to be done first? So, the first increment has to be done then the assignment will be done. In increment, x becomes 7, and 7 is stored in y. After this, if we print the result then ‘x’ is also 7, and ‘y’ is also 7. So, this means first increment value then assign it to y. This is the meaning of pre-increment.

Now, what does it mean by post-increment? So, we are writing the same thing again as
int x = 6, y;
if I say, y = x++;

Now, there are two operations here. Assignment and increment. So, which has to be done first? Here, the assignment is done first, and then an increment will perform. It means the value of x is assigned to y first. So, y = 6. Then ‘x++’, x becomes 7 afterward. So, this will be the result of this statement.

Pre-Increment means, first incrementing the variable and then using the variable, and Post Increment means, first using the variable in the assignment and then incrementing it. I will show you one more example.
int x = 6, y = 2, z;
z = x++ * y;

Here actually x and y have to be multiplied but x should also be incremented. So, the first increment is done, or afterward, is it done? It has done afterward, so first multiplication performs. So, z = 6 * 2 = 12; and then x = 7;

This is the difference between pre-increment and post-increment. So, depending on your requirement you should know which one you want in a particular formula or an expression. Decrement is the subtraction of the value. It is the same as the increment. So, you can try it by yourself.

Let’s Code Pre and Post increment Operator in C++ with Examples:

Pre-Increment Code in C++:
#include <iostream>
using namespace std;
int main()
{
    int x, y;
    cout << "Enter value of x: ";
    cin >> x;

    y = ++x;

    cout << "Value of x and y: " << x << " " << y;
    return 0;
}
Output:

Pre-Increment Code in C++

Post Increment Code in C++:
#include <iostream>
using namespace std;
int main()
{
    int x, y;
    cout << "Enter value of x: ";
    cin >> x;

    y = x++;

    cout << "Value of x and y: " << x << " " << y;
    return 0;
}
Output:

Post Increment Code in C++

Pre-Decrement Code in C++:
#include <iostream>
using namespace std;
int main()
{
    int x, y;
    cout << "Enter value of x: ";
    cin >> x;

    y = --x;

    cout << "Value of x and y: " << x << " " << y;
    return 0;
}
Output:

Pre-Decrement Code in C++

Post Decrement Code in C++:
#include <iostream>
using namespace std;
int main()
{
    int x, y;
    cout << "Enter value of x: ";
    cin >> x;

    y = x--;

    cout << "Value of x and y: " << x << " " << y;
    return 0;
}
Output:

Post Decrement Code in C++

Note. We should not use multiple ++/– on the same variable in a single expression. Because the result will become unpredictable.

In the next article, I am going to discuss Overflow in C++ with Examples. Here, in this article, I try to explain the Increment Decrement Operator in C++ with Examples and I hope you enjoy this Increment Decrement Operator in C++ with Examples article.

Leave a Reply

Your email address will not be published.