Introduction to The Increment And Decrement Operators in C++ :: Part-A-15

1

THE INCREMENT AND DECREMENT OPERATORS

 Increment And Decrement Operators

Of the many features C++ inherited form C, some of the most useful are the increment operator  ++  and decrement operator  –. These operators transform a variable into a statement expression that abbreviates a special form of assignment.

EXAMPLE: Increment and Decrement Operators

         This shows how the increment and decrement operators work:

                 # include  <iostream.h>

                 // Test the increment and decrement operators :

                  main ()

                  {

                             int m  =  44,  n  =  66;

                             cout  <<  “m  =  “  <<  m  <<  “ ,  n  =  “  <<  n  <<  end1;

                             ++m;

                              –n;

                             cout  <<  “m  =  “  <<  m  <<  “ ,  n  =  “  <<  n  <<  end1;

                             m++;

                             n–;

                             cout  <<  “m  =  “  <<  m  <<  “ ,  n  =  “  <<  n  <<  end1;

                             return  0;

                     }

       OUTPUT:

                  m=  44,  n  =  66

                  m=  45,  n  =  65

                  m=  46,  n  =  64

Both the pre-increment operator  ++m  and the post-increment operator m++ have the same effect here: they add 1 to the value of m. Similarly, both the pre-decrement operator  –n  and the post-decrement operator  n–  have the same effect here: they subtract  1  form the value of  n.

When used as a stand- alone expression statement,  ++m  and  m++  are both equivalent to the assignment

                     m  =  m  +  1;

they simply increase the value of  m  by  1. Similarly, the expression statements  –n  and  n–   are both equivalent to the assignment.

                       n  =  n  –  1;

they simply decrease the value of  n  by  1. (The increment operator  ++  was used in the name “C++” because it “increments” the original C programming language; it has everything that C has, and more.)

However, when used as subexpressions (i,e., expressions within expressions), the pre-increment operation  ++m  is different form the post-increment operation  m++. The pre-increment increases the variable first before using it in the larger expression, where as the post-increment increases the value of the variable only after using the prior value of the variable within the larger expression.

Since the incrementing process is equivalent to a separate assignment, there are really two statements to be executed when the increment operation is used as a subexpression: the incrementing assignment and the post-increment and larger enclosing statement. The difference between the pre-increment and the post-increment is simply the difference between executing the assignment before or after the enclosing statement.

EXAMPLE : Pre-Increment and post-Increment Operators

              This shows the difference between the pre-increment:

                      #include  <iostream.h>

                       // Tests the increment and decrement operators :

                       main ()

                       {

                              int m  =  66,  n;

                              n   =  ++m;

                              cout  <<  “m  =  “  <<  m  <<  “ ,  n  =  “  <<  n  <<  end1;

                               n  =  m++;

                               cout  <<  “m  =  “  <<  m  <<  “ ,  n  =  “  <<  n  <<  end1;

                               cout  <<  “m  =  “  <<  m++  <<  end1;

                                cout  <<  “m  =  “  <<  m  <<  end1;

                                cout  <<  “m  =  “  << ++ m  <<  end1;

                                 return  0;

                        }

                  OUTPUT:

                   m  =  67,  n  =  67

                   m  =  68,  n  =  67

                   m  =  68

                   m  =  69

                   m  =  70

In the first assignment,  m is pre-incremented, increasing its value to 67, which is then assigned to  n. In the second assignment, m is post-incremented, 67, which is then assigned to  n  and then  m  is increased to 68.

 In the third output statement,  m  is post-incremented, so the current value of m  (68) is dropped into the output stream and then  m  is increased to 69. In the last output statement,  m  is pre-incremented, so m is increased to  70 first and then that value is dropped into the output stream.

Use of the increment and decrement operators as subexpressions can be tricky and should be used with caution. For example, the order of evaluations of expressions that involve them is not defined by the language and consequently can be uppredictable:

EXAMPLE : The Unpredictablilty of the order of Evaluation of subexpressions

                       #include  <iostream.h>

                         main ()

                             {

                                    int  n  =  5,  x;

                                    x  =  ++n  *  –n;

                                    cout  <<  “n  =  ‘’  <<  n  <<  “ ,  x  =  “  <<  x  <<  end1;

                                    cout  <<  ++n  <<  “  “  <<  ++n  <<  “  “  <<  ++n  <<  end1;

                            }

                    OUTPUT:

                    N  =  5,  x  =  25

                    8  7  6

In the assignment to  x,  n  is first increment to  6  and then decremented back to 5  before the multiply operator is evaluated, computing 5*5. In the last line, the three subexpressions are evaluated form right to left. The left associativity of the output operator  <<  is irrelevant because there are no other operators involved that have the same precedence level.

Ref. By: JOHN R. HUBBARD, Ph.D. Professor of Mathematics and Computer Science, University of Richmond

                                    —————- Thanks everyone

Share.

About Author

TN365

1 Comment

Leave A Reply

Powered by themekiller.com