Difference between Increment and Decrement Operators
Programming languages like C/C++/Java have increment and decrement operators. These are very useful and common operators.
- Increment Operators: The increment operator is used to increment the value of a variable in an expression. In the Pre-Increment, value is first incremented and then used inside the expression. Whereas in the Post-Increment, value is first used inside the expression and then incremented.
Syntax:
// PREFIX ++m // POSTFIX m++ where m is a variable
Example:
#include <stdio.h>intincrement(inta,intb){a = 5;// POSTFIXb = a++;printf("%d", b);// PREFIXintc = ++b;printf("\n%d", c);}// Driver codeintmain(){intx, y;increment(x, y);return0;}chevron_rightfilter_none - Decrement Operators: The decrement operator is used to decrement the value of a variable in an expression. In the Pre-Decrement, value is first decremented and then used inside the expression. Whereas in the Post-Decrement, value is first used inside the expression and then decremented.
Syntax:
// PREFIX --m // POSTFIX m-- where m is a variable
Example:
#include <stdio.h>intdecrement(inta,intb){a = 5;// POSTFIXb = a--;printf("%d", b);// PREFIXintc = --b;printf("\n%d", c);}// Driver codeintmain(){intx, y;decrement(x, y);return0;}chevron_rightfilter_none - Increment (Decrement) operators require L-value Expression
- Interesting facts about Increment and Decrement operators in Java
- Pre-increment (or pre-decrement) in C++
- Increment (++) and Decrement (--) operator overloading in C++
- Count of suffix increment/decrement operations to construct a given array
- What is the difference between = (Assignment) and == (Equal to) operators
- Pre-increment and Post-increment in C/C++
- Operators in C | Set 2 (Relational and Logical Operators)
- LongAdder decrement() method in Java with Examples
- Minimum decrement operations to make Array elements equal by only decreasing K each time
- Operators in C | Set 1 (Arithmetic Operators)
- Why does sizeof(x++) not increment x in C?
- LongAdder increment() method in Java with Examples
- Print matrix after applying increment operations in M ranges
- # and ## Operators in C
Differences between Increment And Decrement Operators:
| Increment Operators | Decrement Operators |
|---|---|
| Increment Operator adds 1 to the operand. | Decrement Operator subtracts 1 from the operand. |
| Postfix increment operator means the expression is evaluated first using the original value of the variable and then the variable is incremented(increased). | Postfix decrement operator means the expression is evaluated first using the original value of the variable and then the variable is decremented(decreased). |
| Prefix increment operator means the variable is incremented first and then the expression is evaluated using the new value of the variable. | Prefix decrement operator means the variable is decremented first and then the expression is evaluated using the new value of the variable. |
| Generally, we use this in decision making and looping. | This is also used in decision making and looping. |
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : him_ansh_1608

