Page 106 - PortGPT_V2.1_C8_Flipbook
P. 106

INFINITE LOOP

                  If the condition given in a loop never becomes false, then the loop will never terminate and run

                  indefinitely. This type of loop is called an infinite loop. For example:
                  while(true)
                  {
                      document.write(i);
                      i=i-2;
                  }
                  Preceding loop will run infinitely as the conditional expression will never become false.



                         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.


                       The Break Statement

                  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. The syntax of the break statement
                  is shown below:

                  initialisation_expression;
                  while (conditional_expression) {
                  if(condition)
                  {
                  break;
                  }
                  update_expression;

                  }
                  Let us create a web page to use the break statement.


                   Program 5: To terminate the execution of the while loop using the break statement when the
                   value of a variable i becomes 3 and display a message “loop break”.


                  <HTML>
                  <HEAD>
                  <TITLE> Using break </TITLE>

                  </HEAD>
                  <BODY>


                  104   Premium Edition-VIII
   101   102   103   104   105   106   107   108   109   110   111