Increment and Decrement Operators in C++

Increment and Decrement Operators in C++

In the C++ programming language, the increment (++) and decrement (–) operators are used to increase or decrease the current value of a user-defined parameter variable by 1. The increment (++) and decrement (–) operators are shorthand program loop statements or notations that help update the value of a user-defined program-declared variable by increasing or decreasing it according to an existing condition expression.

Increment and Decrement Operators in C++

So, let’s understand the increment (++) and decrement (–) operator concepts in C++ programming.

Increment and Decrement Operator Concepts in C++.

  • Pre-increment (++p) expression – It increases the value of the p parameter by 1 before the user-defined variable is used in the condition expression in the existing C++ program.
  • Post-increment (p++) expression – It increments the value of the p parameter by 1 after it is used in a user-defined variable condition expression in the current C++ program.
  • Pre-decrement (–p) expression – It decrements the value of the p parameter by 1 before it is used in a user-defined variable condition expression in the current C++ program.
  • Post-decrement (p–) expression – It decrements the value of the p parameter by 1 after it is used in a user-defined variable condition expression in the current C++ program.

Pre-increment (++p) operator in C++.

Pre-increment operator – It increments the current value of the existing variable by 1 and displays it before it is used in a user-defined variable condition expression in the current C++ program.

Syntax of the pre-increment (++p) operator.

++p;

Example of the pre-increment (++p) operator.

#include <iostream>

using namespace std;

int main() {

int p = 1;

cout << “default Value of p parameter before pre-increment expression – ” << p << endl; // Result is – 1

cout << “default Value of p parameter after pre-increment expression – ” << ++p << endl; // Result is – 2

cout << “Value of p parameter after pre-increment expression – ” << p << endl; // Result is – 2

return 0;

}

explanation of the pre-increment (++a) operator.

  • Here, in a C++ program, the ++p expression first pre-increments the value of the p parameter variable by 2 and then displays the incremented value.

Post-increment (p++) operator in C++.

The post-increment operator displays the current value of an existing variable by 1 after it has been used in a user-defined variable condition expression in the current C++ program.

Syntax of the post-increment (p++) operator.

p++;

Example of the post-increment (p++) operator.

#include <iostream>

using namespace std;

int main() {

int p = 1;

cout << “default value of p parameter before post-increment expression – ” << p << endl; // Result is – 1

cout << “Default value of p parameter after post-increment expression – ” << p++ << endl; // Result is – 1

cout << “Value of p parameter after post-increment expression – ” << p << endl; // Result is – 2

return 0;

}

Explanation of the post-increment (p++) operator.

  • Here, the p++ expression in a C++ program first returns the current value of the p parameter variable (which is 1), and then increments the value of p by 2 and displays it.

Pre-decrement (–p) operator in C++.

The pre-decrement operator decrements the current value of an existing variable before it is used in a user-defined variable condition expression in the current C++ program.

Syntax of the pre-decrement (–p) operator.

–p;

Example of the pre-decrement (–p) operator.

#include <iostream>

using namespace std;

int main() {

int p=2;

cout << “default Value of p parameter before pre-decrement expression – ” << p << endl; // Result is – 2

cout << “default Value of p parameter after pre-decrement expression – ” << –p << endl; // Result is – 1

cout << “Value of p parameter after pre-decrement expression – ” << p << endl; // Result is – 1

return 0;

}

explanation of the pre-decrement (–p) operator.

  • Here in the C++ program, the –p expression first displays the current value of the p parameter variable by decrementing it by 1, and then displays the value of the decremented p variable.

Post-decrement (p–) operator in C++.

The post-decrement operator displays the current value of a variable after it has been decremented and used in a user-defined variable condition expression in the current C++ program.

Syntax of the post-decrement (p–) operator.

p–;

Example of the post-decrement (p–) operator.

#include <iostream>

using namespace std;

int main() {

int p = 2;

cout << “Default value of p parameter before post-decrement expression – ” << p << endl; // Result is – 2

cout << “Default value of p parameter after post-decrement expression – ” << p << endl; // Result is – 2

cout << “Value of p parameter after post-decrement expression – ” << p << endl; // Result is – 1

return 0;

}

Explanation of the post-decrement (p–) operator.

  • Here, in a C++ program, the p– expression first displays the current value of the p parameter variable (which is 2), and then displays the decremented value of the p variable.

Main Differences Between Pre and Post Operators

OperatorIncrement and Decrement operators DescriptionExecution of stepsExample
++pIt used to Increments the value of any variable before using itIn this variable value is incremented expression first and then used by systemint q = ++p;
p++It used to Increments the value of any parameter after using itIn this variable value is used expression first and then incremented then used by systemint q = p++;
–pIt used to Decrements the value of user define variable value before using itIn this variable value is decremented first and then used by systemint q = –p;
p–It used to Decrements the value of user define variable value after using itIn this variable value is used first and then decrementedint q = p–;

When to use pre-increment/decrement vs. post-increment/decrement in a C++ program.

The pre-increment (++p) and pre-decrement (–p) variable operators are used in C++ programs when a C++ user needs to modify or update and display a user-defined, declared, or existing variable before using it in a specific calculation or expression.

In C++ programs, the post-increment (p++) and post-decrement (p–) operators are commonly used when the current value of an existing variable is required before the default value is modified.

Example of pre- and post-increment/decrement operators in a loop statement.

In C++, the increment and decrement operators are applied to a large-scale program loop with parameters to modify the loop counter value in a user-defined program.

Example of the pre-increment operator in a loop.

#include <iostream>

using namespace std;

int main() {

int p = 0;

while (p < 9 ) {

cout << ++p << ” “; // Here the pre-increment expression – increment before using the variable value

} // result – 1 2 3 4 5 6 7 8 9

return 0;

}

Explanation of the pre-increment operator in a loop.

  • Here in this C++ program, the ++p expression increments the value of the p parameter and displays it before printing it in the while loop.

Example of the post-increment operator in a loop.

#include <iostream>

using namespace std;

int main() {

int p = 0;

while (p < 11) {

cout << p++ << “

“; // Here the post-increment expression – use the current value before incrementing the variable

} // result – 0 1 2 3 4 5 6 7 8 9 10

return 0;

}

Explanation of the post-increment operator in a loop.

  • In this C++ program, the p++ expression uses the current value of the p parameter before incrementing it in the while loop.

Detail Summary of Increment/Decrement Operators in c++

OperatorIncrement/Decrement Operators DescriptionExample
++pPre-increment expression – in any c++ program it Increments the variable value before it is usedint q = ++p;
p++Post-increment expression – in any c++ program it Increments the variable value after it is usedint q = p++;
–pPre-decrement expression – in any c++ program it Decrements the variable value before it is usedint q = –p;
p–Post-decrement expression – in any c++ program it Decrements the variable value after it is usedint q = p–;

Summary of Increment and Decrement Operators in C++.

  • In any C++ program, the pre-increment (++p) and pre-decrement (–p) operators modify the value of a variable before using the condition expression.
  • In any C++ program, the post-increment (p++) and post-decrement (p–) operators modify or update the value of a variable after using the condition expression.

Leave a Reply