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

4.18 JUMP STATEMENTS IN JAVASCRIPT
                 Sometimes, there is a situation when the control of the program needs to be transferred out of the loop body, even
                 if all the values of the iterations of the loop have not been completed. For this purpose, jump statements are used in
                 JavaScript. JavaScript offers two jump statements—break and continue.
                 You have already seen the use of break to exit the switch statement. You can also use the break statement to “jump
                 out” of a loop.

                 The break is a keyword in JavaScript which is used for bringing the program control out of the loop. The break statement
                 halts the execution of a loop and program flow switches to the statement after the loop. A single break statement will
                 break out of only one loop.








                                                                                 Condi onal Code
                                                                      True
                                                    Condi on                          Break
                                                                                    statement

                                                   False


                 The following example shows the use of break statement in a loop:
                 Output:

                   <HTML>
                   <BODY text="blue">
                   <H2 style="color:red;">Using a loop with a break statement.</H2>
                   <SCRIPT>
                   for(i=1;i<10;i++) {
                     if (i%3==0)
                       { break; }
                     else {
                        document.write("The number is " + i + "<br> <br>"); }
                   }//for ends
                   document.write("Statement outside for loop");
                   </SCRIPT>
                   </BODY>
                   </HTML>
                 Output:


















                                                                      Introduction to Dynamic Websites Using JavaScript  335
   332   333   334   335   336   337   338   339   340   341   342