Increment And Decrement Operators In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Increment And Decrement Operators In C++

Increment And Decrement Operators In C++

BLUF: Mastering Increment And Decrement Operators In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Increment And Decrement Operators In C++

C++ is renowned for its efficiency. Learn how Increment And Decrement Operators In C++ enables low-level control and high-performance computing in the tutorial below.

What is the definition of a C++ increment Operator?

The increment operator within C++ functions as a unary operator. It is denoted by the symbol (++). This operator increments the value stored in the variable by one. It is specifically designed for use with numeric data types.

C++ increment Operators are classified into two types:

  • Pre-Increment
  • Post-Increment
  • 1. The operator for post-increment (a++)

The postfix operator denotes that the value must be employed before incrementing it. This means that the value is initially utilized in the operation and then incremented by one.

C++ Code:

Example

#include <iostream>
using namespace std;
int main()
{
	int p = 7;
	cout << " The value before employing the post increment operator is"
			": "
		<< p << endl;
	int temp = p++;
	cout << " The value held by temp is: " << temp
		<< endl;
	cout << " The result of applying the post increment operator is: "
		<< p << endl;
	return 0;
}

Output

Output

The value before employing the post increment operator is: 7
The value held by temp is: 7
The result of applying the post increment operator is: 8

2. The operator for pre-increment (++a)

The postfix operator indicates that the value should be increased before utilization. This implies that the value is incremented by one and then applied in the variable.

C++ Code:

Example

#include <iostream>
using namespace std;
int main()
{
	int p = 7;
	cout << " The value before employing the pre increment operator is"
			": "
		<< p << endl;
	int temp = ++p;
	cout << " The value held by temp is: " << temp
		<< endl;
	cout << " The result of applying the pre increment operator is: "
		<< p << endl;
	return 0;
}

Output

Output

The value before employing the pre increment operator is: 7
The value held by temp is: 8
The result of applying the pre increment operator is: 8

What exactly is a C++ decrement Operator?

The decrement operator within C++ functions as a unary operator. Represented by the symbol (-), it decreases the value of a variable by one. It can only be used with numeric values.

C++ decrement operators are classified into two types:

  • Operator for post-decrement
  • Operator for pre-decrement
  • 1. Operator for post-decrement (a--)

The postfix operator indicates that the operand should be evaluated first and then decremented. This means that the operand is utilized in the operation before decrementing its value by one.

C++ Code:

Example

#include <iostream>
using namespace std;
int main()
{
	int p = 7;
	cout << " The value before employing the post decrement operator is"
			": "
		<< p << endl;
	int temp = p--;
	cout << " The value held by temp is: " << temp
		<< endl;
	cout << " The result of applying the post decrement operator is: "
		<< p << endl;
	return 0;
}

Output

Output

The value before employing the post decrement operator is: 7
The value held by temp is: 7
The result of applying the post decrement operator is: 6

2. Operator for pre-decrement (--a)

The postfix operator signifies a decrement in value before its utilization. This operation entails decreasing the value by one and then assigning the updated value to the variable.

C++ Code:

Example

#include <iostream>
using namespace std;
int main()
{
	int p = 7;
	cout << " The value before employing the pre decrement operator is"
			": "
		<< p << endl;
	int temp = --p;
	cout << " The value held by temp is: " << temp
		<< endl;
	cout << " The result of applying the pre decrement operator is: "
		<< p << endl;
	return 0;
}

Output

Output

The value before employing the pre decrement operator is: 7
The value held by temp is: 6
The result of applying the pre decrement operator is: 6

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience