C++ provides various different types of Operators in order to compute mathematical and logical statements and expressions in the program. Increment Operator and Decrement Operator are one such type of Unary Operators in C++ which are used to add or subtract the value of 1 from the operand respectively. This article focuses on and tries to explain the in-depth concept of these 2 Unary operators.
Table of content
1 Definition
2 Increment Operator
2.1 Browse all the Topics Under Operator and Expressions: Operators
3 Decrement Operator
Definition
- Increment Operator is used to increase the value of the operand by 1 whereas the Decrement Operator is used to decrease the value of the operand by 1.
- In C++, the value of the variable is increased or decreased by 1 with the help of theIncrement operator and the Decrement Operator
Increment and Decrement Operators are used only with Integer Variables and Operands having Numerical values. They cannot be used with a Variable containing the values of a Character or String.
The Increment Operator and Decrement Operator can be used in both the Prefix and the Postfix position of the operand. The Position used by the Increment/Decrement Operator in the program statement decides how the operator will function.
The Postfix Operator position has a higher precedence level than the Prefix Operator position. Postfix Operators are evaluated from left-to-right associativity and the Prefix Operators are evaluated from right-to-left associativity.
Increment Operator
The main function of Increment Operators is to increase the numerical count of the variable by a value of 1. In a programming language, Increment Operators are denoted by the sign ‘++’.
Syntax
Pre-Increment Operator:
++ variable_name ;
Post-Increment Operator:
variable_name ++ ;
Note 1
Both the Prefix and Postfix Increment operators positions have the same functional values and produce the same results if they are not used in any expression format.
- Example
int main(){int x = 15 ; int y = 30 ;++x ;y++ ;cout << x << endl << y ;}
- Output
1631
Note 2
Both the Prefix and Postfix positions provide different computational results if they are being used in an expression.
If the Increment Operators are being used in the Prefix position, then, the Increment function will be done before the expression.
If the Increment Operators are being used in the Postfix position, then, the Increment function will be done after the evaluation of the expression.
- Example
int main(){int x = 10 ;int a ;x = ++x ;cout <<"Value of x = "<< x << endl ;a = x++ ;cout <<"Value of a = "<< a << endl ;cout <<"New Value of x = "<< x << endl ;return 0;}
- Output
Value of x = 11Value of a = 11New value of x = 12
- Explanation
In our First ‘cout’ statement, the Pre-Increment Operator is used. Thus, the value of ‘x’ is incremented by 1 i.e., x = x + 1 = 10 + 1 = 11
Then we assign value to the variable ‘a’ in the expression ‘a = x++’
This means that First the value of ‘x’ will be assigned to ‘a’ and then ‘x’ will be incremented by 1. Hence the Output a = 11, new value of x = 12
Browse all the Topics UnderOperator and Expressions: Operators
- Arithmetic Operators
- Assignment Operator
- C++ Shorthands
- Unary Operator
- Relation Operator
- Logical Operators
Decrement Operator
The main function of the Decrement Operator is to decrease the numerical count of the variable by a value of 1. In a programming language, Decrement Operator is denoted by the sign ‘– –’.
Syntax
Pre-Increment Operator:
— — variable_name ;
Post-Increment Operator:
variable_name — — ;
Note 1
Both the Prefix and Postfix Decrement operator positions have the same functional values and produce the same results if they are not used in any expression format.
- Example
int main(){int x = 15 ; int y = 30 ;-- -- x ;y -- -- ;cout << x << endl << y ;}
- Output
1429
Note 2
Just like the Increment Operators, Decrement Operator also performs differently if the operator is used in any of the expressions.
If the Decrement Operator is being used in the Prefix position, then, the Increment function will be done before the expression.
If the Decrement Operator is being used in the Postfix position, then, the Increment function will be done after the evaluation of the expression.
- Example
int main(){int x = 10 ;int a ;cout <<"Value of x = "<< -- -- x << endl ;a = x -- -- ;cout <<"Value of a = "<< a << endl ;cout <<"New Value of x = "<< x << endl ;return 0;}
- Output
Value of x = 9Value of a = 9New value of x = 8
- Explanation
In our First ‘cout’ statement, the Pre-Decrement Operator is used. Thus, the value of ‘x’ will be decremented by 1 i.e., x = x – 1 = 10 – 1 = 9
Then we assign value to the variable ‘a’ in the expression ‘a = x — —’
This means that First the value of ‘x’ will be assigned to ‘a’ and then ‘x’ will be decremented by 1. Hence the Output a = 9, new value of x = 8
FAQs onIncrement & Decrement Operators
Q1. How many types of Increment/Decrement Operators are there in C++?
- 1
- 2
- 3
- 4
Answer. Option B
Q2. What will be the output of the following code?
int main(){int x = 10 ; int y = 20 ;cout << ++x << -- -- y << endl;return 0;}
- 1121
- 921
- 1119
- 919
Answer. Option C
Q3.What will be the output of the following code?
int main(){int a = 3 ; int b = 6 ;int c = a++ + ++b ;cout << ‘Value of c = ’<< c ;return 0;}
- 10
- 12
- 9
- 11
Answer. Option A
Q4. If an Increment/Decrement Operator is placed after the variable, then it is called as?
- Post-Increment
- Pre-Decrement
- Post-Decrement
- Both A and C
Answer. Option D
Q5. The Unary Operator ++ is generally called as?
- Decrement Operator
- Increment Operator
- Unary Plus Operator
- None of the Above
Answer. Option B
As a seasoned expert in C++ programming, my extensive experience allows me to provide an insightful analysis of the concepts covered in the provided article. Let's delve into the details of the Increment and Decrement Operators, highlighting key points and elucidating the intricacies involved.
Increment and Decrement Operators in C++: A Comprehensive Exploration
1. Definition:
The Increment and Decrement Operators are unary operators in C++ designed to manipulate the numerical values of variables. The Increment Operator (++
) increases the value of an operand by 1, while the Decrement Operator (--
) decreases it by 1.
2. Increment Operator:
-
Usage: The Increment Operator is applied to integer variables or operands with numerical values.
-
Prefix and Postfix Positions: It can be used in both Prefix and Postfix positions, each influencing the operator's behavior.
-
Pre-Increment and Post-Increment Syntax:
Pre-Increment Operator: ++variable_name; Post-Increment Operator: variable_name++;
-
Functionality: Both Prefix and Postfix positions yield the same result outside of expressions.
Example:
int main(){ int x = 15; int y = 30; ++x; y++; cout << x << endl << y; }
Output:
16 31
-
Expression Impact: Prefix and Postfix positions produce different results within expressions.
Example:
int main(){ int x = 10; int a; x = ++x; cout << "Value of x = " << x << endl; a = x++; cout << "Value of a = " << a << endl; cout << "New Value of x = " << x << endl; return 0; }
Output:
Value of x = 11 Value of a = 11 New value of x = 12
3. Decrement Operator:
-
Usage: Similar to the Increment Operator, the Decrement Operator (
--
) is employed with integer variables or numerical operands. -
Pre-Decrement and Post-Decrement Syntax:
Pre-Decrement Operator: --variable_name; Post-Decrement Operator: variable_name--;
-
Example:
int main(){ int x = 15; int y = 30; --x; y--; cout << x << endl << y; }
Output:
14 29
-
Expression Impact: Similar to Increment Operators, Decrement Operators behave differently in expressions based on their position.
Example:
int main(){ int x = 10; int a; cout << "Value of x = " << --x << endl; a = x--; cout << "Value of a = " << a << endl; cout << "New Value of x = " << x << endl; return 0; }
Output:
Value of x = 9 Value of a = 9 New value of x = 8
4. FAQs on Increment & Decrement Operators: The article concludes with a set of Frequently Asked Questions, addressing topics such as the number of Increment/Decrement Operator types, output prediction, and operator classifications.
By comprehensively exploring these concepts, this article provides a valuable resource for programmers seeking a deeper understanding of Increment and Decrement Operators in C++.