Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 146 - CoreGPT(Podar)_Flipbook_C8
P. 146

    a += 2;
                          cout<<"The value of a is: "<< a <<"\n";

                          b -= 1;
                          cout<<"The value of b is: "<< b <<"\n";

                          c *= a;
                          cout<<"The value of c is: "<< c <<"\n";                           The value of a is: 7
                                                                                            The value of b is: 6
                          d %= 3;                                                           The value of c is: 14
                                                                                            The value of d is: 1
                          cout<<"The value of d is: "<< d <<"\n";
                          getch();
                      }
                      Increment and Decrement Operators

                      C++  provides  increment  (++)  and  decrement  (–  –)  operators  which  are  used  to  increase  or
                      decrease the value of a variable by 1. These are called unary operators because they operate on

                      a single operand. For example:
                      a = 10;
                      a = ++a;   // means a = a + 1
                      a = --a;  // means a = a - 1

                      C++ allows you to use these operators in two ways:

                          Prefix (++a or --a): The operator is placed before the operand.
                          Postfix (a++ or a--): The operator is placed after the operand.
                      The difference between prefix and postfix (++ and – –) operators is that, in case of prefix operators
                      the value of the variable is incremented/decremented first and then the expression is evaluated.
                      In case of postfix operators, the expression is evaluated first and then the value of the variable is
                      incremented/decremented. Consider the preceding example:

                      int a = 10, b = 0;
                      b = ++a;       //Prefix increment: a is incremented first, then assigned to b.
                      b = a++;  // Postfix increment: a is assigned to b first, then incremented.
                      b = --a;  //Prefix decrement: a is decremented first, then assigned to b.
                      b = a--;  // Postfix decrement: a is assigned to b first, then decremented.
                      Program 6: To use prefix increment/decrement operators.

                      #include<iostream.h>
                      #include<conio.h>                        The old value of a is: 10
                                                               The value of a after prefix increment is: 11
                      void main()
                                                               The value of b is: 11
                      {
                                                               The value of a after prefix decrement is: 10
                          clrscr();                            The value of c is: 10

                    144     Premium Edition-VIII

| 141 142 143 144 145 146 147 148 149 150 151 |