Page 168 - Computer science 868 Class 12
P. 168

The below chart shows the list of precedence of the operators.

                          Hierarchy Order              Operators                        Precedence
                                 1        postfix unary                                 cal++, cal--
                                 2        unary including prefix, bitwise complement,   ++cal, --cal, +cal, -cal, ~, !
                                          logical NOT
                                 3        multiplication, division and modulus            *, /, %
                                 4        addition and subtraction                         +, -

                                 5        shift                                         <<, >>, >>>
                                 6        relational                                    <, >, <=, >=
                                 7        equality, non-equality                          ==, !=
                                 8        bitwise AND                                       &
                                 9        bitwise exclusive OR                              ^
                                10        bitwise inclusive OR                              |

                                11        logical AND                                      &&
                                12        logical OR                                       ||
                                13        ternary                                          ? :
                                14        assignment including shorthand           =, +=, -=, *=, /=, %=, &=,
                                                                                   ^=, |=, <<=, >>=, >>>=
              In an expression, there might be some operators having the same hierarchy. In that case, the calculation is done
              according to their associativity, which is either from left-to-right or vice-versa.

              Suppose, in an expression “m+n-p”, the + and - operators are present without any parenthesis. Since the binary
              operators have left-to-right associativity, the addition will be executed before the subtraction.
              In the expression “m-n+p”, m-n will be executed first according to left-to-right associativity.

              Similarly, in the expression “m%n*p”, the % and * are present without any parenthesis. Since the binary operators have
              left-to-right associativity, the modulus will be executed before the multiplication.
              Some important associativities are as follows:

                          Hierarchy Order              Operators                Associativity of the Operators

                                 1        postfix unary                       Right to Left
                                 2        unary including prefix, logical NOT  Right to Left
                                 3        multiplication, division and modulus  Left to Right

                                 4        addition and subtraction            Left to Right




               Some More Programs

              I.   Arithmetic Expressions
                1.  if int m = 5, n = 6, p;
                    p = ++m + m + ++n;
                      = 6+6+7
                      = 19


                166166  Touchpad Computer Science-XII
   163   164   165   166   167   168   169   170   171   172   173