Page 98 - DCAP202_Fundamentals of Web Programming
P. 98

Unit 8: Programming Constructs in JavaScript




          Notice also that the basic JavaScript syntax rules haven’t been broken – a semi-colon is present  Notes
          after both the true statement and the false statement.


                 Example:
          if  (80<100)
          document.write(“The  expression  has  evaluated  to  true!!”);
          You can see the basic if statement used very simply here. Notice the “less-than” operator used
          within the conditional expression - “80<100”. This if statement simply states that if 80 is less than
          100, the statement is considered to be true. Since 80 is actually less than 100, the document.write
          statement is written to the screen. If the conditional expression were “101<100” (which is read as
          “if 101 is less than 100”), then nothing would be written to the screen as the conditional expression
          would have evaluated to false. Use the if statement if you want something to happen only if the
          conditional expression evaluates to true.

          If you’d like an action to be taken when the conditional expression evaluates to false as well as
          true, use the else addition to provide an action to be taken when the conditional expression
          returns a false value, as shown below.
          if  (101<100)  {
            document.write(“The  expression  has  evaluated  to  true!!”);
            }  else  {
            document.write(“The  expression  has  evaluated  to  false!!”);
          }
          The above if/else statement says that if 101 is less than 100, write the first document.write statement
          to the screen. If 101 isn’t less than 100 the second document.write statement is written to the screen.
          Since 101 is not less than 100, the conditional expression returns false, and the second document.write
          statement (“The expression has evaluated to false!!”) is written to the screen.

          So now  that you know the  basic structure of the if and  if/else statements, we’ll delve into
          another aspect – nesting your if/else statements. This nesting structure is used when you have
          an initial condition to be met with a true or false value, and then you require another set of
          conditions that you’d like to have tested to acquire your end result.
          Examine the below syntax example to understand more clearly what  is meant by the  term
          “nesting”.
          if  (expression1)  {
            statement  If  expression1  is  True;
            }  else  {
            if  (expression2)  {
            statement  If  expression2  is  True;
            }  else  {
            statement  If  expression2  is  False;
           }
          }
          You can see that the second if / else statement is nested within the “false” area of the first if / else
          statement. If expression1 evaluates to true, then the first document.write statement of the first
          if / else statement is written to the screen. If expression1 evaluates to false, then the second if /
          else statement is evaluated. If expression2 evaluates to true, the first document.write statement
          of the second if / else statement is written to the screen. If expression2 evaluates to false, then the
          second document.write statement of the second if / else statement is written to the screen. This
          somewhat complicated (but very useful) method will be used widely within your future JavaScript
          coding.



                                           LOVELY PROFESSIONAL UNIVERSITY                                   91
   93   94   95   96   97   98   99   100   101   102   103