Auto Increment and Decrement

Ads
 

Auto Increment and Decrement

In this example we are demonstrating the significance of prefix and postfix operators by incrementing and decrementing the value of a variable that how both are different with each other.

In this example we are demonstrating the significance of prefix and postfix operators by incrementing and decrementing the value of a variable that how both are different with each other.

Auto Increment and Decrement

     

In this example we are demonstrating the significance of prefix and postfix operators by incrementing and decrementing the value of a variable that how both are different with each other. 

Description of program:

To demonstrate the difference between prefix and postfix operator we are taking an example. In this example first of all, we are taking an integer type variable taking 5 as its value and displaying the original value of this variable. Now we are applying the prefix increment operator and display the value on the console and then applying the postfix increment operator and display the value on the console. After that we are applying the prefix decrement operator and display the value on the console and then applying the postfix decrement operator and displaying the value on the console.

	public class AutoInc {

    public static void main(String[] args) {
    
        int a = 5;
        System.out.println("Original value of a : " + a);
        System.out.println();

        System.out.println("After using pre-increment value of a : " + ++a); 
        System.out.println("After using post-increment value of a : " + a++);
        System.out.println();

        System.out.println("After completing post-increment operation value of a : " + a);
        System.out.println();

        System.out.println("After using pre-decrement value of a : " + --a); 
        System.out.println("After using pre-decrement value of a : " + a--); 
        System.out.println();

        System.out.println("After completing pre-decrement operation value of a : " + a);
    }
}

Here is the output:

C:\Examples>java AutoInc
Original value of a : 5

After using pre-increment value of a : 6
After using post-increment value of a : 6

After completing post-increment operation value of a : 7

After using pre-decrement value of a : 6
After using pre-decrement value of a : 6

After completing pre-decrement operation value of a : 5

Download Source Code

Ads

Ads