Page 161 - ComputerScience_Class_11
P. 161

5.  If int p = 5, k;
                       k = (++p * (p-- * 2));
                  Ans.  72                    Working: 6 * (6 * 2) = 6 * (12) = 72

                 Assignment Operator
                 The assignment operator is used to assign a value or an expression to a variable. Java has one assignment operator
                 which is the “=” operator. For example:
                 int a = 50;
                 double d = 34.7;
                 In an assignment statement, the part on the left side of the operator and the part on the right side have different roles.
                 The differences between the left-hand side and the right-hand side of an assignment are given in the table below:

                                    Left-Hand Side                                 Right-Hand Side

                      It is the variable that receives the value.  It is the value or expression that is assigned to the variable.
                      It represents the memory location where the result  It represents the data or calculation whose result will be stored.
                      is stored.
                      It must be a variable name.               It can be a constant, variable or expression.

                 Shorthand Operators
                 Java allows a way to represent some binary operators in a shorter way using shorthand assignment operators. They
                 assign an expression to a variable. They are also known as compound assignment operators.
                 Let us see all the different types of shorthand operators.

                    Operator           Meaning                   Description            Example      Equivalent Arithmetic
                                                                                                         Expression
                       +=       Addition Assignment    It is used to add the value of the  c += 1   c = c + 1
                                                       right operand  to  the variable  and
                                                       assign the result to the variable.
                       -=       Subtraction Assignment  It is used to subtract the value of the  d -= 5  d = d - 5
                                                       right operand from the variable and
                                                       assign the result to the variable.
                       *=       Multiplication Assignment It is used to multiply the variable by  m *= b  m = m * b
                                                       the value of the right operand and
                                                       assign the result to the variable.
                       /=       Division Assignment    It is used to divide the variable by the  div /= 4  div = div / 4
                                                       value of the right operand and assign
                                                       the result to the variable.

                       %=       Remainder Assignment   It is  used  to  divide  the variable  by  rem %= 5  rem = rem % 5
                                                       the value of the right operand and
                                                       assign the remainder to the variable.


                 Relational Operators
                 A relational operator is used to compare two variables or expressions and result in true or false. There are six types of
                 relational operators.







                                                                                            Variables and Expressions  159
   156   157   158   159   160   161   162   163   164   165   166