Increment / Decrement Operator

Author: Ravi Poswal

The unary operators is the increment (++) and decrement (--) operators commonly use to increase or decrease a variable's value by one. There are two types

1. Prefix Increment /Postfix Increment

2. Prefix Decrement/Postfix Decrement 

Increment operators are used in programming languages to increase the value of a variable by one. There are two types of increment operators: the prefix increment operator (++x) and the postfix increment operator (x++).

Prefix Increment Operator (++x)

The prefix increment operator increases the one value of the variable but before the increasing value is used in the program.

Postfix Increment Operator (x++):

The postfix increment operator increases the one value of the variable after the increasing value is used in the program.

 

Code Example

class A
{
public static void main(String arg[])
{  
int a=9,b;  
b=a++;
System.out.println("with increment the value "+a);  // with increment the value 10
System.out.println("without increase the value "b);  // without increase the value 9
int x=9,y;  
y=++x;
System.out.println("with increment the value "x); //with increment the value 10
System.out.println("with increment the value "y); //with increment the value 10
}
}

The decrement operator in Java is used to decrease the value of a variable by 1. It is represented by the symbol --. This operator is commonly used in loops and counting operations.

Prefix Decrement Operator (--x)

The prefix decrement operator decreases the 1 value of the variable but before the value is used.

Postfix Decrement Operator (x--)

The postfix decrement operator decreases the 1 value of the variable but after the value is used.

Code Example

class A
{
public static void main(String arg[])
{  
int a=9,b;  
b=a--;
System.out.println(a);
System.out.println(b);
 int x=9,y;  
 y=--x;
 System.out.println(x);
 System.out.println(y);
}
}