Page 30 - Modular_V1.1_Flipbook
P. 30

cout <<”The value of a is: “ << a << “\n”;
                  cout <<”The value of b is: “ << b << “\n”;

                  c = a--;                     //Value is assigned first and then decremented
                  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;
                  where,  conditional_expression,  expression1 and  expression2 are expressions. If
                  conditional_expression  returns  true  then  the  expression1  will be  evaluated  otherwise
                  expression2 will be evaluated.
                  Program 8: To check the greatest in two numbers.

                  #include<iostream.h>
                  #include<conio.h>
                  void main()
                  {

                  int a, b, c = 0;
                  a = 20;
                  b = 5;
                  clrscr();
                  (a > b)? cout <<”a is greater than b”: cout <<”b is greater than a”;
                  getch();

                  }

                      OPERATOR PRECEDENCE

                  Operator Precedence determines the order in which the operators are executed.  If an expression
                  contains multiple operators with the same precedence, then the expression is resolved from
                  left-to-right  or  right-to-left  according  to  the  associativity of  the  operators.  The  operator
                  precedence and associativity in C++ is listed in the following table:

                           Category                            Operator                          Associativity
                      Postfix                               () [] -> . ++ - -                     Left to right
                      Unary                                   + - ! ~ ++ - -                      Right to left

                      Multiplicative                             * / %                            Left to right
                      Additive                                    + -                             Left to right
                      Relational                             <  <=  >  >=                         Left to right

                      Equality                                  ==  !=                            Left to right
                      Logical AND                                 &&                              Left to right

                  28      Touchpad MODULAR (Version 1.1)-X
   25   26   27   28   29   30   31   32   33   34   35