Difference between Prefix and Postfix Operators
Key Difference: Prefix and Postfix Operators are primarily used in relation to increment and decrement operators. If the increment and decrement operators are written before the operand, then they are termed as prefix operators. However, if they are written after the operand, then they are termed as postfix operators.
include("ad4th.php"); ?>Prefix and Postfix Operators are primarily used in relation to increment and decrement operators within object oriented programming (OOP) languages such as Java, C Programming, PHP, etc. They are commonly used in C++ more so than in anything else.
In order to understand prefix and postfix operators, one must first understand increment (++) and decrement (--) operators. In the most basic terms, increment operators increase the value of operand by 1 respectively, while decrement operators do the opposite; they decrease e value of operand by 1. The increment operator is written as ++ and the decrement operator is written as --.
Now, there are two ways to incorporate increment and decrement operators. They can be written either before the operand or after it. Depending on its location, they will be classified as either prefix operators or postfix operators. If they are written before the operand, then they are termed as prefix operators. However, if they are written after the operand, then they are termed as postfix operators. The main difference between the two is the placement: either before or after the operand.
include("ad3rd.php"); ?>Furthermore, their placement also has an impact on the values of the operant. Programiz.com explains the different impact that prefix and postfix operators have on the operand when using an increment operator: “If you use ++ operator as prefix like: ++var; then, the value of operand is increased by 1 then, only it is returned but, if you use ++ as postfix like: var++; then, the value of operand is returned first then, only it is increased by 1.” The decrement operator would have a similar impact on the operand, except that instead of increasing the value of the operand, it would decrease it.
Comparison between Prefix and Postfix Operators:
|
Prefix Operators |
Postfix Operators |
Location |
Added before the operand |
Added after the operand |
Related to |
Increment (++) and decrement (--) operators |
|
Used in |
Imperative programming languages such as Java and C-like languages, especially C++ |
Image Courtesy: slideshare.net, stuff-heap.blogspot.com
Add new comment