Page 48 - Open Soource Technologies 304.indd
P. 48

Web Technologies-I



                   Notes         Comparison Operators
                                 As their name suggests, comparison operators compare operands. The result is always either true,
                                 if the comparison is truthful, or false, otherwise.
                                 Operands to the comparison operators can be both numeric, both string, or one numeric and one
                                 string. The operators check for truthfulness in slightly different ways based on the types and
                                 values of the operands, either using strictly numeric comparisons or using lexicographic (textual)
                                 comparisons. Table 2.4 outlines when each type of check is used.
                                          Table 2.4: Type of Comparison Performed by the Comparison Operators

                                             First operand              Second operand         Comparison
                                    Number                        Number                     Numeric
                                    String that is entirely numeric  String that is entirely numeric  Numeric
                                    String that is entirely numeric  Number                  Numeric
                                    String that is not entirely numeric  Number              Lexicographic
                                    String that is entirely numeric  String that is not entirely numeric Lexicographic
                                    String that is not entirely numeric  String that is not entirely numeric Lexicographic

                                 One important thing to note is that two numeric strings are compared as if they were numbers. If
                                 you have two strings that consist entirely of numeric characters and you need to compare them
                                 lexicographically, use the strcmp ( ) function.
                                 The comparison operators are:
                                 Equality (==)
                                 If both operands are equal, this operator returns true; otherwise, it returns false.

                                 Identical (===)
                                 If both operands are equal and are of the same type, this operator returns true; otherwise, it returns
                                 false. Note that this operator does not do implicit type casting. This operator is useful when
                                 you do not know if the values you are comparing are of the same type. Simple comparison may
                                 involve value conversion. For instance, the strings “0.0” and “0” are not equal. The == operator
                                 says they are, but === says they are not.

                                 Inequality (! = or <>)
                                 If both operands are not equal, this operator returns true; otherwise, it returns false.
                                 Not identical (! ==)
                                 If both operands are not equal, or they are not of the same type, this operator returns true;
                                 otherwise, it returns false.
                                 Greater than (>)
                                 If the left-hand operator is greater than the right-hand operator, this operator returns true;
                                 otherwise, it returns false.
                                 Greater than or equal to (>=)

                                 If the left-hand operator is greater than or equal to the right-hand operator, this operator returns
                                 true; otherwise, it returns false.
                                 Less than (<)

                                 If the left-hand operator is less than the right-hand operator, this operator returns true; otherwise,
                                 it returns false.


        42                                LOVELY PROFESSIONAL UNIVERSITY
   43   44   45   46   47   48   49   50   51   52   53