Pre and Post Decrement Operator in Programming
Last Updated :
26 Mar, 2024
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;
}
OutputpostNum = 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;
}
OutputpostNum = 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);
}
}
OutputpostNum = 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)
OutputpostNum = 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);
}
}
OutputpostNum = 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);
OutputpostNum = 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.
Similar Reads
Pre Increment and Post Increment Operator in Programming
Pre Increment Operator and Post Increment Operator are the two ways of using the Increment operator to increment the value of a variable by 1. They can be used with numeric data values such as int, float, double, etc. Pre-increment and Post-increment perform similar tasks with minor distinctions. In
6 min read
Increment and Decrement Operators in Programming
Increment and Decrement Operators are Unary Operators commonly used in programming to increase or decrease the value of a variable by one, respectively. They provide a shorthand way to perform these common operations. Table of Content Increment OperatorsIncrement Operators in CIncrement Operators in
7 min read
Assignment Operators in Programming
Assignment operators in programming are symbols used to assign values to variables. They offer shorthand notations for performing arithmetic operations and updating variable values in a single step. These operators are fundamental in most programming languages and help streamline code while improvin
7 min read
Bitwise AND operator in Programming
In programming, Bitwise Operators play a crucial role in manipulating individual bits of data. One of the fundamental bitwise operators is the Bitwise AND operator (&). In this article, we'll dive deep into what is Bitwise AND operator, its syntax, properties, applications, and optimization tech
6 min read
What are Operators in Programming?
Operators in programming are essential symbols that perform operations on variables and values, enabling tasks like arithmetic calculations, logical comparisons, and bitwise manipulations. In this article, we will learn about the basics of operators and their types. Operators in Programming Table of
15+ min read
Binary Operators in Programming
Binary Operators are essential tools in programming that perform operations on pairs of data, enabling tasks like calculations, comparisons, logical operations, and bitwise manipulations. They are fundamental for processing and manipulating data efficiently in computer programs. Table of Content Wha
14 min read
C# Program to Overload Unary Increment (++) and Decrement (--) Operators
In C#, overloading is the common way of implementing polymorphism. It is the ability to redefine a function in more than one form. A user can implement method overloading by defining two or more methods in a class sharing the same name but with different method signatures. So in this article, we wil
3 min read
Operator Precedence and Associativity in Programming
We have many types of operators in a programming language. Operators serve many purposes. But when we have many operators within the same statement, Operator Precedence and Associativity come into the picture. They define which operator should be taken into account first. It is similar to the BODMAS
3 min read
Comparison Operators in Programming
Comparison Operators in programming are used to compare values and determine their relationship, such as equality, inequality, greater than, less than, etc. They evaluate expressions and return a Boolean value (true or false) based on the comparison result, crucial for decision-making in conditional
10 min read
Logical AND operator in Programming
In programming, Logical operators play a crucial role in manipulating individual data. One of the fundamental logical operators is the Logical AND operator(&&).In this article, weâll discuss what is a Logical AND operator, its syntax, properties, applications, and optimization techniques, an
5 min read