Open In App

Pre and Post Decrement Operator in Programming

Last Updated : 26 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Pre-decrement and post-decrement are the two ways of using the decrement operator to decrement the value of a variable by 1. They can be used with numeric data type values such as int, float, double, etc. Pre-decrement and Post-decrement perform similar tasks with minor distinctions.

What is a Pre-Decrement Operator?

In pre-decrement operation, the decrement operator is used as the prefix of the variable. The decrement in value happens as soon as the decrement operator is encountered.

Syntax of Pre-Decrement

--variable_name

What is a Post-Decrement Operator?

In post-decrement operation, the value is decremented after all the other operations are performed i.e. all the other operators are evaluated. The decrement operator is used as the suffix to the variable name.

Syntax of Post-Decrement

variable_name--

Pre and Post Decrement Operator in C:

Here are the implementation of Pre and Post Decrement Operator in C language:

C
// C Program to illustrate the predecrement and post 
// decrement 
#include <stdio.h> 
  
int main() 
{ 
  
    int num1 = 15, num2 = 20; 
  
    int postNum = num1--; // Post Decrement 
    int preNum = --num2; // Pre Decrement 
  
    // Printing value of postNum 
    // after post decrement. 
    printf("postNum = %d \n", postNum); 
    printf("num1 = %d", num1); 
    
    // Printing new line 
    printf("\n"); 
    
    // Printing value of preNum 
    // after pre decrement. 
    printf("preNum = %d \n", preNum); 
    printf("num2 = %d", num2); 
  
    return 0; 
}

Output
postNum = 15 
num1 = 14
preNum = 19 
num2 = 19

Pre and Post Decrement Operator in C++:

Here are the implementation of Pre and Post Decrement Operator in C++ language:

C++
#include <iostream>
using namespace std;

int main() {
    int num1 = 15, num2 = 20;
    
    int postNum = num1--; // Post Decrement
    int preNum = --num2; // Pre Decrement

    // Printing value of postNum after post decrement.
    cout << "postNum = " << postNum << endl;
    cout << "num1 = " << num1 << endl;

    // Printing value of preNum after pre decrement.
    cout << "preNum = " << preNum << endl;
    cout << "num2 = " << num2 << endl;

    return 0;
}

Output
postNum = 15
num1 = 14
preNum = 19
num2 = 19


Pre and Post Decrement Operator in Java:

Here are the implementation of Pre and Post Decrement Operator in java language:

Java
public class Main {
    public static void main(String[] args) {
        int num1 = 15, num2 = 20;
        
        int postNum = num1--; // Post Decrement
        int preNum = --num2; // Pre Decrement

        // Printing value of postNum after post decrement.
        System.out.println("postNum = " + postNum);
        System.out.println("num1 = " + num1);

        // Printing value of preNum after pre decrement.
        System.out.println("preNum = " + preNum);
        System.out.println("num2 = " + num2);
    }
}

Output
postNum = 15
num1 = 14
preNum = 19
num2 = 19


Pre and Post Decrement Operator in Python:

Here are the implementation of Pre and Post Decrement Operator in Python language:

Python3
num1 = 15
num2 = 20

postNum = num1 # Post Decrement
num1 -= 1
preNum = num2 - 1 # Pre Decrement
num2 -= 1

# Printing value of postNum after post decrement.
print("postNum =", postNum)
print("num1 =", num1)

# Printing value of preNum after pre decrement.
print("preNum =", preNum)
print("num2 =", num2)

Output
postNum = 15
num1 = 14
preNum = 19
num2 = 19


Pre and Post Decrement Operator in C#:

Here are the implementation of Pre and Post Decrement Operator in C# language:

C#
using System;

class Program {
    static void Main() {
        int num1 = 15, num2 = 20;
        
        int postNum = num1--; // Post Decrement
        int preNum = --num2; // Pre Decrement

        // Printing value of postNum after post decrement.
        Console.WriteLine("postNum = " + postNum);
        Console.WriteLine("num1 = " + num1);

        // Printing value of preNum after pre decrement.
        Console.WriteLine("preNum = " + preNum);
        Console.WriteLine("num2 = " + num2);
    }
}

Output
postNum = 15
num1 = 14
preNum = 19
num2 = 19


Pre and Post Decrement Operator in Javascript:

Here are the implementation of Pre and Post Decrement Operator in javascript language:

JavaScript
let num1 = 15, num2 = 20;

let postNum = num1--; // Post Decrement
let preNum = --num2; // Pre Decrement

// Printing value of postNum after post decrement.
console.log("postNum = " + postNum);
console.log("num1 = " + num1);

// Printing value of preNum after pre decrement.
console.log("preNum = " + preNum);
console.log("num2 = " + num2);

Output
postNum = 15
num1 = 14
preNum = 19
num2 = 19


Application of Pre and Post-Decrement Operators:

  • Loop Control: Used to decrement loop control variables in loops.
  • Array Indexing: Employed to access array elements by decrementing the index.
  • Iterator Manipulation: Utilized in data structures like linked lists or iterators to move to the previous element.
  • Control Flow: Modify control flow based on certain conditions.
  • Performance Optimization: Pre-decrement may be more efficient than post-decrement in certain scenarios.

Conclusion:

Pre and post-decrement operators are fundamental tools in programming for decrementing variable values. They are commonly used in loops, array indexing, iterator manipulation, control flow, and performance optimization. Understanding the difference between pre and post-decrement operators and their appropriate usage can lead to efficient and readable code.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads