Page 170 - ComputerScience_Class_11
P. 170

S.No.                    Operators                               Precedence
                         9       Bitwise exclusive OR                   ^
                        10       Bitwise inclusive OR                   |
                        11       Logical AND                            &&
                        12       Logical OR                             ||
                        13       Ternary                                ? :
                        14       Assignment including shorthand         =, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>=
              Some Solved Examples:

              1. int m=10, n=20, p=12, q=4, e;
                e  =  m - n * (q % p) + 10 * q;
                   =  10 - 20 * (4 % 12) + 10 * 4           [innermost bracket]
                   =  10 - 20 * 4 + 10 * 4                  [multiplication]
                   =  10 - 80 + 40                          [subtraction]
                   =  -70 + 40                              [addition]
                   =  -30
                Hence, e=-30 (Answer)
              2. y += ++y + y-- + --y; when int y=8
                y  = y + (++y + y-- + --y)                  [bracket]
                   = 8 + (9 + 9 + 7)                        [prefix and postfix]
                   = 8 + (25)                               [addition]
                   = 33
              3.  boolean result = ((4<5) && !(2<3) || (7==7));
                Output: true

                      Note:  Since  the relational  operator has  higher  precedence than  the logical  operator, the relational
                      operator will be executed before the logical operator.


                The precedence of logical operators is as follows:
                Logical NOT (!), Logical AND (&&) and Logical OR (||).
                So, if a statement contains all the three logical operators, then Logical NOT operator will be operated first.
                Examples:
                1.  (2<3)                     =   true
                    !(2<3)                    =   false
                2.  (4<5)                     =   true
                3.  (4<5) && !(2<3)           =   false
                4.  (7==7)                    =   true
                5.  ((4<5) && !(2<3) || (7==7))  =   true
                Hence, the result is true.


                   7.5 ASSOCIATIVITY OF OPERATORS
              Some operators have the same precedence. In that case, the evaluation is done according to their associativity, which
              is either from left to right or vice versa.

              Suppose, in an expression “a+b-c”, the operators + and - are given without any parenthesis. Since the binary operators
              have left-to-right associativity, the addition will be executed before the subtraction.






                  168  Touchpad Computer Science (Ver. 3.0)-XI
   165   166   167   168   169   170   171   172   173   174   175