Page 333 - Web Applications (803) Class 11
P. 333

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.
                 Ð Ðconditional_expression: Similar to for loop, this expression contains a logical condition which evaluates either true
                   or false.
                 Flow Diagram




                                                              do...while Loop Body






                                                          True       Test
                                                                   Condi
on



                                                                       False

                                                                Loop Terminates
                 Example:
                   <HTML>
                   <BODY text="blue">
                   <H2> Using do...while loop </H2>
                   <script>
                   var i = 2;
                   document.write("Loop to display even numbers less than 20 <br><br>");
                   do
                   {
                     document.write(i + "<br><br>");
                     i = i + 2;
                   }while(i<20);
                   </SCRIPT>
                   </BODY>
                   </HTML>


                                                                      Introduction to Dynamic Websites Using JavaScript  331
   328   329   330   331   332   333   334   335   336   337   338