
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Increment ++ and decrement -- Operators in C++
The increment operator ++ adds 1 to its operand, and the decrement operator -- subtracts 1 from its operand. So,
x = x+1; is the same as x++;
And similarly,
x = x-1; is the same as x--;
Both the increment and decrement operators can either precede (prefix) or follow (postfix) the operand.
x = x+1; can be written as ++x;
Note that, When an increment or decrement is used as part of an expression, there is an important difference in prefix and postfix forms. If you are using prefix form then increment or decrement will be done before resting of the expression, and if you are using postfix form, then increment or decrement will be done after the complete expression is evaluated.
- In the prefix version (i.e., ++i), the value of i is incremented, and the value of the expression is the new value of i. So basically it first increments then assigns a value to the expression.
- In the postfix version (i.e., i++), the value of i is incremented, but the value of the expression is the original value of i. So basically it first assigns a value to expression and then increments the variable.
Example
Let's look at some code to get a better understanding −
#include<iostream> using namespace std; int main() { int x = 3, y, z; y = x++; z = ++x; cout << x << ", " << y << ", " << z; return 0; }
Output
This would give us the output −
5, 3, 5
Why is this? Let's look at it in detail −
- Initialize x to 3
- Assign y the value we get by evaluating the expression x++, ie, the value of x before increment then increment x.
- Increment x then assign z the value we get by evaluating the expression ++x, ie, value of x after the increment.
- Print these values
- Related Articles
- Python Increment and Decrement Operators
- Increment and decrement operators in Java
- Increment and Decrement Operators in Python?
- Increment and Decrement Operators in C#
- PHP Increment/Decrement Operators
- Why avoid increment (“++”) and decrement (“--”) operators in JavaScript?
- What are increment (++) and decrement (--) operators in C#?
- Interesting facts about Increment and Decrement operators in Java
- What are the restrictions on increment and decrement operators in java?
- Increment ++ and Decrement -- Operator Overloading in C++
- Pre-increment (or pre-decrement) in C
- Increment Negative and Decrement Positive Numbers by 1 in an Array in Java
- Write a C program to demonstrate post increment and pre increment operators
- Count of suffix increment/decrement operations to construct a given array in C++
- Create increment decrement plus minus buttons programmatically for HTML input type number in JavaScript

Advertisements