Page 171 - ComputerScience_Class_11
P. 171

Similarly, in an expression “a%b*c”, the operators % and * are given without any parenthesis and the modulus will be
                 executed before the multiplication.
                 The associativity of some operators is as shown below.

                            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 Solved Examples:
                 If int a=5, b=6, c=7, d;
                 1.  d  =  a%b/c*b;
                     =  5%6/7*6
                     =  5/7*6
                     =  0*6
                     =  0
                 2.  d  =  a+b-c+a;
                     =  5+6-7+5
                     =  11-7+5
                     =  4+5
                     =  9


                     7.6 ARITHMETICAL EXPRESSION AND STATEMENT
                 While doing calculations in Java, we come across the term Java expression. Before understanding what it is and how it
                 differs from a mathematical expression, we must understand the difference between an arithmetical expression and
                 an arithmetical statement.
                 Let us see an example:
                                                                 c = a+b;

                 Here, a + b is said to be an arithmetic expression and c = a + b; is called an arithmetic statement.

                                     Arithmetic Expression                        Arithmetic Statement
                         Any meaningful  statement containing  identifiers,  When an  arithmetical  expression  is  assigned  to  a
                         literals and arithmetical operators which can produce  variable, then it is called an arithmetical statement.
                         a result is called an arithmetical expression.
                         Examples: a * b, 2 * (l + b)                 Examples: c = a * b, p = 2 * (l + b)

                 7.6.1 Interconversion of Mathematical Expression and Java Expression
                 We are already familiar with the fact that we cannot write a mathematical expression in a Java program. We must
                 follow certain rules and convert a mathematical expression into a Java expression. The following table demonstrates
                 some examples of mathematical expressions and their equivalent Java expressions:

                                    Mathematical Expression                          Java Expression
                         pr 2                                         22.0/7.0 * r * r
                          2
                         3x + 2y                                      3 * x * x + 2 * y
                         prt
                         100                                          (p * r * t) / 100.0

                         ut+1/2at 2                                   (u * t) + (1.0 / 2.0 * a * t * t)



                                                                                            Variables and Expressions  169
   166   167   168   169   170   171   172   173   174   175   176