Pre and Post Decrement Operator in C++
Here are the implementation of Pre and Post Decrement Operator in C++ language:
#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 Programming
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.
Table of Content
- What is a Pre-Decrement Operator?
- What is a Post-Decrement Operator?
- Pre and Post Decrement Operator in C
- Pre and Post Decrement Operator in C++
- Pre and Post Decrement Operator in Java
- Pre and Post Decrement Operator in Python
- Pre and Post Decrement Operator in C#
- Pre and Post Decrement Operator in Javascript
- Application of Pre and Post-Decrement Operators