Java – Increment and Decrement Operator2 min read

The ++ and the – – are Java’s increment and decrement operators. ++ is used to increase the value by 1 and – – is used to decrease the value by 1. There are two kinds of Increment and Decrement Operators.

They are:

  • Post-Increment or Post-Decrement:
    First, the value is used for operation and then incremented or decremented. Represented like a++ or a–.
  • Pre-Increment Pre-Decrement:
    Here First the value is incremented or decremented then used for the operation. Represented like ++a or – –a.

Example on Increment and Decrement Operator in java

Output: After execution following result will be displayed.

Limitations of Increment and Decrement Operators:

Increment and decrement operators can only be applied to variables but not on constant values. If we apply on canstants then we will get a compile-time error.


Example of Pre increment and Post increment in java:

The above shows the use of a++ and ++a and at the end, if you print the value of a, you will get 7 as the value of a.

Pre-increment: (++a)

The major point to remember is that ++a increments the value and immediately returns it.

Post-increment: (a++)

a++ also increments the value but returns an unchanged value of the variable. It does not return the value immediately but if it is executed on the next statement then a new value is used.

Example of Pre decrement and Post decrement in java:

Pre-decrement: (a)

The major point to remember is that ––a decrements the value and immediately returns it.

Post-decrement: (a)

a–– also decrements the value but returns an unchanged value of the variable. It does not return the value immediately but if it is executed on the next statement then a new value is used.


MORE

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …