Thursday, 17 October 2013

Increment and Decrement Operators in Programming

Increment decrement operator in c
(increment decrement operator overloading c++, difference between increment and decrement operators, increment and decrement operators in java, increment and decrement operators in c, decrement operator in c)

We have seen that in while, do-while and for loop we write a statement to increase the value of a variable. For example, we used the statements like counter = counter + 1; which adds 1 to the variable counter. This increment statement is so common that it is used almost in every repetition structure (i.e. in while, do-while and for loop). The C language provides a unary operator that increases the value of its operator by 1. This operator is called increment operator and sign ++ is used for this. The statement counter = counter + 1; can be replaced with the statement
counter ++ ;
 The statement counter++ adds 1 to the variable counter. Similarly the expressions i = i + 1 ; and j = j + 1 ; are equivalent to i++ ; and j++; respectively. There is also an operator -- called decrement operator. This operator decrements, the value of its operand by 1. So the statements counter = counter - 1; and j = j - 1; are equivalent to counter--; and j--; respectively.

The increment operator is further categorized as pre-increment and post-increment. Similarly, the decrement operator, as pre-decrement and post-decrement. In pre-increment, we write the sign before the operand like ++j while in post-increment, the sign ++ is used after the operand like j++. If we are using only variable increment, pre or post increment does not matter. In this case, j++ is equivalent to ++j. The difference of pre and post increment matters when the variable is used in an expression where it is evaluated to assign a value to another variable. If we use pre-increment (++j ), the value of j is first increased by 1. This new value is used in the expression. If we use post increment ( j++ ), the value of j is used in the expression. After that it is increased by 1. Same is the case in pre and post decrement.

The operators ++ and -- are used to increment or decrement the variable by 1. There may be cases when we are incrementing or decrementing the value of a variable by a number other than 1. For example, we write counter = counter + 5; or j = j – 4;. Such assignments are very common in loops, so C provides operators to perform this task in short. These operators do two things they perform an action (addition, subtraction etc) and do some assignment. These operators are +=, -=, *=, /= and %=. These operators are compound assignment operators. These operators assign a value to the left hand variable after performing an action (i.e. +, -, *, / and %). The use of these operators is explained by the following examples.

Let’s say we have an expression, counter = counter + 5;. The equivalent of this expression is counter += 5;. The statement counter += 5; does two tasks. At first, it adds 5 to the value of counter and then assigns this result to counter. Similarly the  following expressions

x = x + 9;
x = x - 6 ;
 x = x * 8 ;
x = x / 4 ;
 x = x % 2;

can be written in equivalent short statements using the operators ( +=, -=, *=, /=, %= ) as follows:

x += 9;
x -=  6 ;
 x *= 8 ;
x /= 4 ;
 x %= 2;

Note that there is no space between these operators. These are treated as single signs. Be careful about the operator %=. This operator assigns the remaider to the variable. These operators are alternate in short hand for an assignment statement. The use of these operators is not necessary. A programmer may use these or not. It is a matter of style.

No comments:

Post a Comment