Thursday 9 April 2020

Increment and Decrement Operators


C++ has two very useful operators ++ and -- called increment and decrement operators respectively. These are generally not found in other languages. These operators are called unary operators as they require only one operand. This operand should necessarily be a variable not a constant.

The increment operator (++) adds one to the operand while the decrement operator (--) subtracts one from the operand.
These operators may be used either before or after the operand. When they are used before the operand, it is termed as prefix, while when used after the operand, they are termed as postfix operators.
Example.:            int i = 5;
       i++;
       ++i;
       ––i;
       i––;
When used in an isolated C statement, both prefix and postfix operators have the same effect, but when they are used in expressions, each of them has a different effect.
In expressions, postfix operator has the effect of use then change, while the prefix operator has the effect of change thEn use.
Example: b = a ++; this is postfix increment expression. In the expression
                firstly b = a; then a = a+1; will be executed, while in prefix increment
                        expression
                b = - - a;
                firstly a = a-1;  then b = a; will be executed.



 Next TopicConditional Operators
Previous Topic: Special Operators

No comments:

Post a Comment