Java Program To Demonstrate Increment And Decrement Operators

Write Java program to demonstrate the use of increment and decrement operators

// WAP to demonstrate Increment and Decrement operator.

public class IncDecOperator 
{
    public static void main(String[] args)
    {
        int a = 6, ans;

        System.out.println("Value of a :--> "+a);

        ans = a++;      //post increment
        System.out.println("The value a++ :"+ans);
        //a is updated after execution of ans

        System.out.println("Value of a :-->"+a);
        ans = a--;    //post decrement

        System.out.println("The value a-- : "+ans);
        //a is updated after execution of ans

        System.out.println("Value of --a :--> "+a);
        ans = --a;   //pre decrement
        //a is updated before execution of the ans
        System.out.println("The value of --a :"+ans);

        System.out.println("Value of a :--> "+a);
        ans = ++a;   //pre increment
        //a is updated before execution of the ans
        System.out.println("The value of ++a : "+ans);
    }
}

Output:


<< Previous | Next >>


Leave a Reply

Your email address will not be published.