Operator and Expressions: Operators

Increment & Decrement Operators

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.

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 the Increment 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

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

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 = 11

Value of a = 11

New 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 Under Operator and Expressions: 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

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 = 9

Value of a = 9

New 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 on Increment & Decrement Operators

Q1. How many types of Increment/Decrement Operators are there in C++?

  1. 1
  2. 2
  3. 3
  4. 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;

}

  1. 1121
  2. 921
  3. 1119
  4. 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;

}

  1. 10
  2. 12
  3. 9
  4. 11

Answer. Option A

Q4. If an Increment/Decrement Operator is placed after the variable, then it is called as?

  1. Post-Increment
  2. Pre-Decrement
  3. Post-Decrement
  4. Both A and C

Answer. Option D

Q5. The Unary Operator ++ is generally called as?

  1. Decrement Operator
  2. Increment Operator
  3. Unary Plus Operator
  4. None of the Above

Answer. Option B

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.