In this C++ tutorial, you will learn how the increment operator ++ increases a variable by one, how prefix increment and postfix increment differ, and when to use ++ or += with example programs.
C++ Increment Operator ++
The C++ increment operator ++ increases the value of its operand by one. For example, if a contains 12, applying the increment operator changes its value to 13.
The increment operator is a unary operator because it works with one operand. It can be written in two forms:
- Prefix increment:
++a - Postfix increment:
a++
Both forms increment the variable itself by one. The difference appears when the increment expression is used as part of a larger expression. Prefix increment changes the variable first and produces the incremented value. Postfix increment produces the value the variable had before the increment and then increments the variable.
C++ Prefix Increment ++variable
For Prefix Increment, the operand comes to the right of the operator. Following is an example for prefix increment.
++operand
With prefix increment, the operand is incremented before the value of the increment expression is used.
Prefix Increment Example in C++
In the following example, we take an integer and increment it using prefix form.
C++ Program
#include <iostream>
using namespace std;
int main() {
int a = 12;
++a;
cout << a << endl;
}
Output
13
The statement ++a; changes a from 12 to 13. Because the expression is used as a statement by itself, only the updated value matters here.
C++ Postfix Increment variable++
For Postfix Increment, the operand comes to the left of the operator.
operand++
Postfix increment also increases the operand by one. When the expression’s value is needed, however, the postfix form produces the operand’s value from before the increment.
Postfix Increment Example in C++
In the following example, we take an integer and increment it using postfix form.
C++ Program
#include <iostream>
using namespace std;
int main() {
int a = 12;
a++;
cout << a << endl;
}
Output
13
Here, a++; is also a standalone statement. The previous value produced by the postfix expression is not used, so the visible effect is the same as ++a;: a becomes 13.
C++ Postfix vs Prefix Increment
The difference between postfix and prefix increment is easiest to see when the increment expression itself is used, such as when its value is printed or assigned to another variable.
Prefix and Postfix Increment Output Comparison
In the following example, we shall perform postfix and prefix on different variables, and then look into how they act during execution.
C++ Program
#include <iostream>
using namespace std;
int main() {
//postfix
int a = 12;
cout << a++ << endl; //12
cout << a << endl << endl; //13
//prefix
int b = 12;
cout << ++b << endl; //13
cout << b << endl; //13
}
Output
12
13
13
13
cout << a++ << endl; takes the value of current a, executes the cout statement, and then increments a. So, for the next statement, cout << a << endl << endl;, value of a is 13.
cout << ++b << endl; increments the value of b to 13, executes the cout statement. So, when this statement is executed, b is printed as 13.
Assigning Prefix and Postfix Increment Results
The following example stores the value produced by each increment expression in another variable. This makes the difference explicit.
#include <iostream>
using namespace std;
int main() {
int a = 10;
int x = a++;
int b = 10;
int y = ++b;
cout << "a = " << a << ", x = " << x << '\n';
cout << "b = " << b << ", y = " << y;
}
Output
a = 11, x = 10
b = 11, y = 11
For x = a++, x receives the old value 10, while a becomes 11. For y = ++b, b is incremented first, so both b and y are 11.
C++ ++ vs += 1
For ordinary numeric variables, both ++a and a += 1 increase a by one. Their syntax and expression behavior are not identical, however. In particular, postfix a++ produces the value from before the increment, whereas a += 1 updates the variable and produces the updated object.
#include <iostream>
using namespace std;
int main() {
int a = 5;
int b = 5;
++a;
b += 1;
cout << a << '\n';
cout << b;
}
Output
6
6
If the goal is simply to add one to a built-in numeric variable, either form can express that operation. The increment operator specifically communicates an increase of one.
C++ Increment by 2 or Another Amount
The ++ operator always increments by one. To increase a numeric variable by 2 or another amount, use the compound addition assignment operator +=.
variable += amount;
#include <iostream>
using namespace std;
int main() {
int number = 10;
number += 2;
cout << number;
}
Output
12
C++ Increment Operator in for Loops
The increment operator is commonly used in the iteration expression of a for loop. When the result of the increment expression is not otherwise used, both i++ and ++i advance a built-in integer loop counter by one.
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; ++i) {
cout << i << ' ';
}
}
Output
1 2 3 4 5
For built-in integer counters, using i++ instead would produce the same loop sequence because the value produced by the increment expression is discarded. Prefix increment is also commonly used with iterators, where it directly expresses advancing the iterator without requiring the previous value from a postfix operation.
C++ Increment Float Value
Increment operator can take a float variable as operand and increase its values by 1.
Incrementing a Float by One in C++
In the following example, we shall initialize a float variable with some value and increment its value by 1 using ++ operator.
C++ Program
#include <iostream>
using namespace std;
int main() {
float a = 12.5;
a++;
cout << a << endl;
}
Output
13.5
The increment is by exactly one, not by the smallest possible floating-point step. Therefore, incrementing 12.5f produces 13.5f.
Operands That Can Be Incremented in C++
The built-in increment operator requires a modifiable operand to which increment is defined. Numeric variables such as int, long, and floating-point variables can be incremented. Pointers can also be incremented, in which case they advance to the next element according to the pointed-to type.
A literal such as 5 is not a modifiable variable, so an expression such as 5++ is invalid. Likewise, ++ should not be used on an object that cannot legally be modified.
Avoid Multiple Increment Side Effects in One C++ Expression
For clear and predictable code, avoid modifying the same variable multiple times inside a complicated expression. Statements such as i++; or ++i; are easy to reason about, while combining several increments with other uses of the same variable can make sequencing rules difficult to follow and, depending on the expression, may result in undefined behavior.
When several updates are required, write them as separate statements so that the order of operations is explicit.
C++ Increment Operator Key Points
++aanda++both increaseaby one.- Prefix
++aproduces the incremented value. - Postfix
a++produces the value from before the increment. - When used as standalone statements,
++a;anda++;leave a built-in numeric variable with the same final value. - Use
+= 2,+= 5, or another amount when the required increment is not one. - The
++operator can be used with floating-point variables as well as integer variables. - In a
forloop with a built-in integer counter, either prefix or postfix increment can advance the counter when the expression result is discarded.
In this C++ Tutorial, we learned what increment operator does, how to use increment operator in prefix or postfix form, with the help of example C++ programs.
TutorialKart.com