Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Pre-Increment Operator
2.1.
Syntax
2.2.
Example 
2.3.
Implementation
2.4.
C++
3.
Post-Increment Operator
3.1.
Syntax
3.2.
Example
3.3.
Implementation
3.4.
C++
4.
Special Case for Post-Increment Operator
4.1.
Example
4.2.
C++
5.
Readability
6.
The Increment Step
7.
Frequently Asked Questions
7.1.
What is a unary operator in Java?
7.2.
What is the difference between pre-increment and post-increment?
7.3.
How many types of increment operators are there?
7.4.
Difference between i++ and ++i in Java?
7.5.
What is the use of the operators?
8.
Conclusion
Last Updated: Apr 29, 2024
Easy

What is the Difference Between i++ and ++i in Java?

Author Sohail Ali
2 upvotes

Introduction

Increment operators are the unary operators used to raise the value of an operand by unity. This operator works solely with variables only. The ++ symbol denotes the increment operator. Java and C++ support increment operators while Python does not. Depending on how a value is increased, increment operators are of two types, i.e., pre-increment (++<operand>) and post-increment (<operand>++) operators.

i++ and ++i Difference

This blog will discuss the increment operator and the difference between i++ and ++i operators.

Pre-Increment Operator

Whenever we write the ++ sign before a variable or an operator, it is known as a pre-increment operator. In a pre-increment process, the operand's value is first raised by one before being used for any further calculation.

Syntax

++<operand>

Example 

++a, where a is a variable.

Implementation

  • C++

C++

#include <iostream>
using namespace std;
int main(){
   int a = 10;
   cout << "Value of a before pre-incrementing: " << a << endl;
   // pre-increment
   int i = ++a;
   cout << "Value of i: " << i << endl;
   // value after pre-incrementing
   cout << "Value of a after pre-incrementing: " << a << endl;
}
You can also try this code with Online C++ Compiler
Run Code

Output

pre-increment output

Explanation 

In this code, we are using a pre-increment operator on variable a. The pre-incremented value of a is 11, which is further stored in the variable i. That is because the pre-increment operator raises the variable's value before being used in the expression.

Post-Increment Operator

Whenever we write the ++ sign after a variable or an operator, it is known as a post-increment operator. The value of the operand is first used for calculation or in the expression; after that only, it is increased by one.

Syntax

<operand>++

Example

 a++, where a is a variable

Implementation

  • C++

C++

#include <iostream>
using namespace std;
int main(){
   int a = 10;
   cout << "Value of a before post-incrementing: " << a << endl;
   // post-increment
   int i = a++;
   cout << "Value of i: " << i << endl;
   // value after post-incrementing
   cout << "Value of a after post-incrementing: " << a << endl;
}
You can also try this code with Online C++ Compiler
Run Code

Output

post-increment output

Explanation 

In this code, we are using the post-increment operator on variable a. The post-incremented value of a is 11, but the variable i equals 10. That is because the post-increment operator increases the variable's value after being used in the expression.

Special Case for Post-Increment Operator

Do you wonder what would happen if we assign the post-incremented value of a variable to the same variable? 

The answer is variable's value stays constant. This is because the post-increment operation is performed after the assignment operation of the same variable. i.e the value of the variable is incremented after it is assigned in the same variable.

Example

  • C++

C++

#include <iostream>
using namespace std;
int main(){
   int a = 10;
   cout << "Value of a before increment " << a << endl;
   a = a++;
   cout << "Value of a after increment " << a << endl;
}
You can also try this code with Online C++ Compiler
Run Code

Output

Special-case output

Explanation 

In this code, the value of variable is the same because the post-increment operator raises the count of the variable after it has been assigned to the variable. Here the value 10 is stored in variable and then it is incremented. This is why change is not reflected in the variable itself.

Readability

Both i++ and ++i are increment operators in C, but they differ in their behavior and readability. i++ is the postfix increment operator, which increments the value of i after it is used in an expression. On the other hand, ++i is the prefix increment operator, which increments the value of i before it is used in an expression.

The Increment Step

  • i++: Postfix increment operator. The current value of i is used in the expression, and then i is incremented.
  • ++i: Prefix increment operator. i is incremented first, and then the updated value of i is used in the expression.

Frequently Asked Questions

What is a unary operator in Java?

An operator with only one operand is a unary operator in Java.

What is the difference between pre-increment and post-increment?

In a pre-increment process, the operand's value is first raised by one before being used for any other purpose. In contrast, in a post-increment process, the operand's value is first used before being raised by one.

How many types of increment operators are there?

There are two increment operators. The first one is pre-increment, and the second is post-increment. In pre-increment, the value of the operand is raised by one before using it. In the post-increment case, the operand's value is raised by one after we have used it.

Difference between i++ and ++i in Java?

In Java, i++ and ++i are called post-increment and pre-increment operators. Both operators increase the value of a variable by unity. The main difference between i++ and ++i is that the value of a variable is used first and then raised in the pre-increment operation. In the case of post-increment operation, the value is used before using for calculation.

What is the use of the operators?

An operator is used to alter certain data elements and output a result. These data elements are referred to as operands or arguments. The operator is applied to the operand to make the required changes in the operand.

Conclusion

This article discusses the difference between pre-increment (++i) and post-increment (i++) operators. We hope this blog has helped you enhance your knowledge of increment operators. If you want to learn more, then check out our articles.

And many more on our platform Coding Ninjas Studio.

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your coding ability, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundles for placement preparations.

However, you may consider our paid courses to give your career an edge over others!

Happy Learning!

Live masterclass