10x Blogs Directory

Monday, February 23, 2009

Increment and Decrement Operators



  • unary increment operator (++)
  • unary decrement operator (--)‏
  • Increment and decrement operators increase and decrease a value stored in a number variable by 1.
  • For example, the expression, count=count + 1;//increment the value of count by 1 is equivalent to, count++;
  • The increment and decrement operators can be placed before or after an operand.
  • When used before an operand, it causes the variable to be incremented or decremented by 1, and then the new value is used in the expression in which it appears.
  • For example,
int i = 10;
int j = 3;
int k = 0;
k = ++j + i; //will result to k = 4+10 = 14


  • When the increment and decrement operators are placed after the operand, the old value of the variable will be used in the expression where it appears.
  • For example,
    int i = 10;
    int j = 3;
    int k = 0;
    k = j++ + i; //will result to k = 3+10 = 13