Page 161 - Computer Science Class 11 With Functions
P. 161

7.5.2 Relational Operators

            A relational operator is used to compare the values of operands on either side. The result of applying a relational
            operator is either True or False. Table 7.2 describes the relational operators.
               Table 7.2: Relational  Operators (Assume that the variables a, b, c  have values 10, 5, and -2 respectively)

              Operator      Operation                   Explanation                           Examples
                 ==      equal to         Yields True if the operands on either side  >>> c == -2
                                          are equal and False otherwise.              True
                                                                                  >>> a == b
                                                                                      False
                 !=      not equal to     Returns True if operands on the two sides  >>> a != b
                                          of  the operator are not  equal,  False       True
                                          otherwise.                              >>> c != -2
                                                                                      False
                 >       greater than     Yields True if the value on the LHS of the  >>> a > b
                                          operator is greater than that on its RHS,       True
                                          and False otherwise.                    >>> c > 0
                                                                                      False
                 <       less than        Yields  True if the value on the LHS of the   >>> c < b
                                          operator is less than that on the RHS of the       True
                                          operator, and  False otherwise.         >>> b < 0
                                                                                      False
                 >=      greater  than or  yields  True if  the value  on  the LHS  of  >>> c >= -2
                         equal to         the operator is greater  than or equal  to       True
                                          the value on the RHS of the operator, and   >>> b >= a
                                          False otherwise.                            False
                 <=      less   than   or Yields True if the value on the LHS of the  >>> c <= -2
                         equal to         operator is less than or equal to the value on       True
                                          the RHS of the operator, otherwise False.  >>> b <= c
                                                                                      False

            7.5.3 Assignment Operators

            Assignment operators are used to assign the value of the operand on the right-hand side of the operator to the
            operand on the left-hand side of the operator. (see Table 7.3: Assignment  Operators)
            (Assume the value of a, b, and c to be 10, 5, and -2 respectively for computations in the table)
                                                   Table 7.3: Assignment  Operators

              Operator        Operation                     Explanation                          Examples
                  =       Assignment          The operator assigns the value on the RHS of  >>> a = 10
                                              the operator to the operand on the LHS of the   >>> b = a + b
                                              operator.                                 >>> b
                                                                                            15
                 +=       Addition and        The operator adds the value on the RHS of  >>> a += 20
                          assignment          the  operator  to  the  operand  on  the  left  of  >>> a
                                              the operator and  assigns  the  result to the      30
                                              operand  on  the LHS  of  the operator.  Note  >>> a += 3 * 4
                                              that the expression on the RHS is evaluated   >>> a
                                              before applying  +=                           22



                                                                                        Data Types and Operators  159
   156   157   158   159   160   161   162   163   164   165   166