Page 103 - DCAP202_Fundamentals of Web Programming
P. 103

Fundamentals of Web Programming




                    Notes           {
                                     document.write(“The  number  is  “  +  i);
                                     document.write(“<br  />”);
                                     i++;
                                    }
                                   </script>
                                   </body>
                                   </html>
                                   Output
                                   The number is 0
                                   The number is 1
                                   The number is 2

                                   The number is 3
                                   The number is 4
                                   The number is 5
                                   Explanation:

                                   i is equal to 0.
                                   While i is less than, or equal to, 5, the loop will continue to run.
                                   i will increase by 1 each time the loop runs.

                                   8.2.3 The do...while Loop

                                   The do...while loop is a variant of the while loop. This loop will execute the block of code ONCE,
                                   and then it will repeat the loop as long as the specified condition is true.
                                   Syntax
                                   do
                                    {
                                     code  to  be  executed
                                    }
                                   while  (var<=endvalue);


                                          Example: do while example
                                   The example below uses a do...while loop. The do...while loop will always be executed at least
                                   once, even if the condition is false, because the statements are executed before the condition is
                                   tested:
                                   <html>
                                   <body>
                                   <script  type=”text/javascript”>
                                   var  i=0;
                                   do
                                    {
                                   document.write(“The  number  is  “  +  i);
                                     document.write(“<br  />”);
                                     i++;




          96                                LOVELY PROFESSIONAL UNIVERSITY
   98   99   100   101   102   103   104   105   106   107   108