Page 376 - Webapplication11_C11_Flipbook
P. 376

Example:

                 <HTML>
                 <BODY text="blue">
                 <H2> Using while loop in JavaScript</H2>
                 <SCRIPT>
                 var i = 1;
                 while (i <=5) {
                   document.write("<br><br> The square of "+i+" is "+(i*i));
                   i++;
                 }
                 </SCRIPT>
                 </BODY>
                 </HTML>
              On running the above program, we get the following output:




















                                   Do You Know?


                                   Any  non-zero  value  in  the  while  loop  indicates  always  a  true  condition,
                                   whereas zero indicates always-false condition.


              The do….while Loop

              Do while is a different type of loop than either the for or while loops. This loop will always execute the statement at
              least once, even if the condition is false. After one iteration of the loop has been completed, the condition is checked.
              Depending on the condition, the loop will continue to run further or terminate.
              Syntax of the do…while loops is as follows:

                 initialisation_expression;
                 do
                 {
                 //Loop body
                 update_expression;
                 } while (conditional_expression);
              Where,

              Ð Ðinitialisation_expression: In this expression, a variable is initialized outside the loop.
              Ð Ðupdate_expression: The loop control variable is updated here.
                374     Touchpad Web Applications-XI
   371   372   373   374   375   376   377   378   379   380   381