Page 150 - DCAP408_WEB_PROGRAMMING
P. 150

Web Programming




                    Notes          Logical Operator

                                   The logical operators evaluate expressions and then return a true or false value based on the result.
                                     Operator   What does it do?   Example/Explanation
                                     &&       Looks at two expressions   if day='friday'&&date=13 then alert("Are You
                                              and returns a value of   Superstitious?")
                                              "true" if both expressions   Compares the value of the day and the value of the date. If
                                              are true.            it is true that today is a Friday and if it is also true that the
                                                                   date is the 13th, then an alert box pops up with the
                                                                   message "Are You Superstitious?"
                                     ||       Looks at two expressions   if day='friday'&&date=13 then alert("Are You
                                              and returns a value of   Superstitious?") else if day='friday'||date=13 then
                                              "true" if either one—but   alert("Aren't you glad it isn't Friday the 13th?")
                                              not both—of the      Compares the value of the day and the value of the date. If
                                              expressions are true.   it is true that today is a Friday and if it is also true that the
                                                                   date is the 13th, then an alert box pops up with the
                                                                   message "Are You Superstitious?" If both are not true, the
                                                                   script moves onto the next line of code...
                                                                   Which compares the value of the day and the value of the
                                                                   date. If either one—but not both—is true, then an alert box
                                                                   pops up with the message "Aren't you glad it isn't Friday
                                                                   the 13th?"

                                   Computational Operator

                                   The computational operators perform a mathematical function on a value or values, and return
                                   a single value.

                                     Operator   What does it do?   Example/Explanation
                                     +        Adds two values together   counter+2
                                                                   Returns the sum of the counter plus 2
                                     -        Subtracts one value from   counter-2
                                              another              Returns the sum of the counter minus 2
                                     *        Multiplies two values   counter*10
                                                                   Returns the result of the variable times 10
                                     /        Divides the value on the   counter/2
                                              left by the one on the right   Divides the current value of the counter by 2 and returns
                                              and returns the result   the result
                                     ++X      Increments the value, and   ++counter
                                              then returns the result   Looks at the current value of the counter, increments it by
                                                                   one, and then returns the result. If the counter has a value
                                                                   of 3, this equation returns the value of 4.
                                     X++      Returns the value, and   counter++
                                              then increments the value   Returns the value of the counter, then increments the
                                                                   counter. If the counter has a value of 3, this equation
                                                                   returns the value of 3, then sets the counter value to 4.
                                     --X      Decreases the value, and   --counter
                                              then returns the result   Looks at the current value of the counter, decreases it by
                                                                   one, and then returns the result. If the counter has a value
                                                                   of 7, this equation returns the value of 6.
                                     X--      Returns the value, and   counter--
                                              then decreases the value   Returns the value of the counter, then decrease the
                                                                   counter value. If the counter has a value of 7, this
                                                                   equation returns the value of 7, then sets the counter
                                                                   value to 6.


          144                               LOVELY PROFESSIONAL UNIVERSITY
   145   146   147   148   149   150   151   152   153   154   155