Page 197 - Web Applications (803) Class 12
P. 197

Let us understand how this program works.

                        No. of                       Condition
                       Iteration      Variable          i <5                             Action

                          1st           i = 0      true            Web Applications is printed i is incremented to 1.

                         2nd            i = 1      true            Web Applications is printed i is incremented to 2.

                         3rd            i = 2      true            Web Applications is printed i is incremented to 3.

                         4th            i = 3      true            Web Applications is printed i is incremented to 4.

                         5th            i = 4      true            Web Applications is printed i is incremented to 5.

                         6th            i = 5      false           The loop is terminated.


                 JavaScript Infinite for loop

                 If the test condition of a for loop is always true, it executes repeatedly (until memory is full). For example,
                    for(i = 5; i > 0; i++) {
                        // block of code to execute – body of the loop
                    }
                 Here, the condition is always true due to which the code continue to execute for infinite times.

                 The while loop

                 JavaScript has a while loop that allows code to be run continuously until a certain condition is met. While loop
                 just needs a condition expression, unlike for loop.
                 Syntax:

                        while (condition) {
                          // statements to execute – body of the loop }
                 Flow diagram:

                                             Initialization/Starting
                                                   statement

                                                          Next iteration of the loop



                                                      Test       True   Execute statement(s)
                                                   Statement             inside body of loop



                                                         False

                                                 Exit from loop










                                                                                          Web Scripting—JavaScript   195
   192   193   194   195   196   197   198   199   200   201   202