Java

What Does i++ Mean in Java

Java offers an increment operator that is used to increase the numerical count by one in the variable. It is of two types: Post-increment and Pre-increment operators. In the Post-increment operator “i++”, the value of the operand is incremented after performing the task, while in Pre-increment “++i”, the value is incremented before performing the task.

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

i++

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”:

int i=10;

We will print out the value of the “i” variable on the console by using the “System.out.println()” method:

System.out.println("Original Value:" + i);

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:

System.out.println("Post-Increment Value:" + i++);

Again, execute the System.out.println() method to display the Post-incremented value:

System.out.println("The Value after increment:" + i);

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”:

publicstaticvoidmain(String[] args) {
    System.out.println("Even numbers between 0 to 10:");
    for(int i=0; i<=10; i++) {
        if(i%2==0) {
            System.out.println(i);
        }
    }
}

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.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.