Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 147 - CoreGPT(Podar)_Flipbook_C8
P. 147

    int a = 10, b = 0, c = 0;
                         cout<< "The old value of a is: " << a << "\n";

                         // Prefix Increment
                         b = ++a;  // Value is incremented first, then assigned
                         cout<< "The value of a after prefix increment is: " << a << "\n";
                         cout<< "The value of b is: " << b << "\n";

                         // Prefix Decrement
                         c = --a;  // Value is decremented first, then assigned
                         cout<< "The value of a after prefix decrement is: " << a << "\n";
                         cout<< "The value of c is: " << c << "\n";
                         getch();

                     }
                     Program 7: To use postfix increment/decrement operators.
                     #include<iostream.h>

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

                         int a = 10, b = 0, c = 0;
                         cout << "The old value of a is: " << a << "\n";

                         // Postfix Increment
                         b = a++;   // Value is assigned first, then incremented
                         cout<< "The value of a after postfix increment is: " << a << "\n";

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

                         // Postfix Decrement
                           c = a--;   // Value is assigned first, then decremented

                           cout<< "The value of a after postfix decrement is: " << a << "\n";
                           cout<< "The value of c is: " << c << "\n";
                           getch();
                     }
                     Ternary Operator

                     A ternary operator requires three operands to work. In C++, there is only one ternary operator
                     (?:).  It is also called conditional operator. The syntax of the ternary operator is:

                     conditional_expression ? expression1 : expression2;

                                                                                      Developing Programs in C++        145

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