This post will discuss what i++ means and how to use it in Java.
What Does i++ Mean in Java?
“i++” in Java is called the “Post-increment” operator, which belongs to the Arithmetic group of operators. It increases the specified variable’s value by one after performing the specific operation.
Syntax
Let’s see some examples related to using the Post-increment operator “i++” in Java.
Example 1: Using i++ to Post-increment Value of a Variable
In this example, we have an integer type variable “i” that is initialized with the value “10”:
We will print out the value of the “i” variable on the console by using the “System.out.println()” method:
Now, we will increment the value of “i” by using the Post-increment operator as “i++”. This statement will print out the original value without incrementing because Post-increment first performs the task and then increments the value:
Again, execute the System.out.println() method to display the Post-incremented value:
The output of the above program will print three lines: the first line will contain the original value of the “i” variable, and the third displays the updated value after the increment.
As stated above, the Post-increment operator increases the value after completing the specified task. That’s why the System.out.println() method present in the second line will first display the original value, then the “++” increment operator adds one to it:
Example 2: Using i++ as Post-increment Operator in “for” Loop
In the below-given program, we will print the even numbers from 0 to 10 by using the “for” loop. The for loop contains a conditional operator “i” that also acts as a Post-increment operator “i++”.
When the for loop executes, firstly, the value of the specified value is checked according to the added condition “i<=10”. If it is evaluated as “true”, then the code added in the body of the for loop will be executed. After completing this process, the Post-increment operator increments the value of the “i” value variable, and the loop goes on until the added condition is evaluated as “false”:
Output
We have provided all the essential information related to the Post-increment operator “i++” and its usage in Java.
Conclusion
i++ is an increment operator also called the Post-increment operator. It is used to increment the value of a variable by 1. The Post-increment operator belongs to the group of Arithmetic operators. It returns the value after performing a particular task. It is primarily utilized to control the execution of the program in loops such as for loop. In this post, we discussed what i++ means and how it works in Java.