Join our newsletter for the latest updates.

Increment ++ and Decrement -- Operator as Prefix and Postfix

In this article, you will learn about the increment operator ++ and the decrement operator -- in detail with the help of examples.

In programming (Java, C, C++, JavaScript etc. ), the increment operator ++ increases the value of a variable by 1. Similarly, the decrement operator -- decreases the value of a variable by 1.

a = 5
++a;          // a becomes 6
a++;          // a becomes 7
--a;          // a becomes 6
a--;          // a becomes 5

Simple enough till now. However, there is a slight but important difference you should know when these two operators are used as prefix and postfix.


++ and -- operator as prefix and postfix

  • If you use the ++ operator as prefix like: ++var. The value of var is incremented by 1 then, it returns the value.
  • If you use the ++ operator as postfix like: var++. The original value of var is returned first then, var is incremented by 1.

The -- operator works in a similar way like the ++ operator except it decreases the value by 1.

Let's see the use of ++ as prefix and postfix in C, C++, Java and JavaScript.


Example 1: C Programming

#include <stdio.h>
int main() {
   int var1 = 5, var2 = 5;

   // var1 is displayed
   // Then, var1 is increased to 6.
   printf("%d\n", var1++);

   // var2 is increased to 6 
   // Then, it is displayed.
   printf("%d\n", ++var2);

   return 0;
}

Example 2: C++

#include <iostream>

using namespace std;

int main() {
   int var1 = 5, var2 = 5;

   // var1 is displayed
   // Then, var1 is increased to 6.
   cout << var1++ << endl;

   // var2 is increased to 6
   // Then, it is displayed.
   cout << ++var2 << endl;

   return 0;
}

Example 3: Java Programming

class Operator {
    public static void main(String[] args) {
        int var1 = 5, var2 = 5;

        // var1 is displayed
        // Then, var1 is increased to 6.
        System.out.println(var1++);

        // var2 is increased to 6
        // Then, var2 is displayed
        System.out.println(++var2);
    }
}

Example 4: JavaScript

let var1 = 5, var2 = 5;

// var1 is displayed
// Then, var1 is increased to 6
console.log(var1++)

// var2 is increased to 6
// Then, var2 is displayed
console.log(++var2)

The output of all these programs will be the same.

Output

5
6