English

Explain the following operator with an example program: Conditional Operator

Advertisements

Question

Explain the following operator with an example program:

Conditional Operator

Code Writing
Explain
Advertisements

Solution

The Increment Operator (++) in Java is a unary operator used to increase the value of a variable by 1, whereas the Decrement Operator (--) is a unary operator used to decrease the value of a variable by 1.

Both operators can be used in prefix (++a, --a) and postfix (a++, a--) forms.

Example:

class IncrementDecrement
{
    public static void main(String[] args)
    {
        int a = 10, b = 20;
        a++;
        b--;
        System.out.println("Increment: " + a);
        System.out.println("Decrement: " + b);
    }
}

Output:

 

Increment: 11
Decrement: 19

shaalaa.com
  Is there an error in this question or solution?
Chapter 6: Variables and Expressions - EXERCISE [Page 132]

APPEARS IN

Dheeraj Mehrotra Computer Science [English] Class 12
Chapter 6 Variables and Expressions
EXERCISE | Q 31. (e) | Page 132
Advertisements
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×