Difference between Increment and Decrement Operators - GeeksforGeeks

07 Apr, 2020
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>  int increment(int a, int b){    a = 5;      // POSTFIX    b = a++;    printf("%d", b);      // PREFIX    int c = ++b;    printf("\n%d", c);}  // Driver codeint main(){    int x, y;    increment(x, y);      return 0;}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>  int decrement(int a, int b){    a = 5;      // POSTFIX    b = a--;    printf("%d", b);      // PREFIX    int c = --b;    printf("\n%d", c);}  // Driver codeint main(){    int x, y;    decrement(x, y);      return 0;}Differences between Increment And Decrement Operators:Increment OperatorsDecrement OperatorsIncrement 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.My Personal Notes
arrow_drop_upSave
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>  int increment(int a, int b){    a = 5;      // POSTFIX    b = a++;    printf("%d", b);      // PREFIX    int c = ++b;    printf("\n%d", c);}  // Driver codeint main(){    int x, y;    increment(x, y);      return 0;}
Syntax:
// PREFIX
++m

// POSTFIX
m++

where m is a variable

Example:
#include <stdio.h>  int increment(int a, int b){    a = 5;      // POSTFIX    b = a++;    printf("%d", b);      // PREFIX    int c = ++b;    printf("\n%d", c);}  // Driver codeint main(){    int x, y;    increment(x, y);      return 0;}
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>  int decrement(int a, int b){    a = 5;      // POSTFIX    b = a--;    printf("%d", b);      // PREFIX    int c = --b;    printf("\n%d", c);}  // Driver codeint main(){    int x, y;    decrement(x, y);      return 0;}Differences between Increment And Decrement Operators:Increment OperatorsDecrement OperatorsIncrement 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.My Personal Notes
arrow_drop_upSave
Syntax:
// PREFIX
--m

// POSTFIX
m--

where m is a variable

Example:
#include <stdio.h>  int decrement(int a, int b){    a = 5;      // POSTFIX    b = a--;    printf("%d", b);      // PREFIX    int c = --b;    printf("\n%d", c);}  // Driver codeint main(){    int x, y;    decrement(x, y);      return 0;}
Differences between Increment And Decrement Operators:
him_ansh_1608
Technical Scripter 2019
C Language
C++
Difference Between
Java
Programming Language
Technical Scripter
Java
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Multidimensional Arrays in C / C++
rand() and srand() in C/C++
Core Dump (Segmentation fault) in C/C++
Left Shift and Right Shift Operators in C/C++
Substring in C++
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
Inheritance in C++
Map in C++ Standard Template Library (STL)
C++ Classes and Objects