Previous Example-|-Next Example-|-Return to Chapter Listing

Example 6.18:
Breaking Out of Loops--break


The button calls function1(), which uses a 'for' loop. This displays the contents of the counter variable ( i ) in a confirm dialog box. Clicking on OK continues the loop, clicking on Cancel executes the break and ends the loop:


This is the script we used: <SCRIPT LANGUAGE="JavaScript"> <!-- function function1() { var bResult for (i = 1; i < 4; i++) { bResult = confirm("i = " + i + "\nClick cancel to execute a \'break\' statement","") if (bResult == false) { break; } } alert("The loop has finished\ni = " + i) } //--> </SCRIPT> </HEAD> <BODY> <BR> <form> <input type="button" value=" Button A " onclick="function1()"> </form>
Previous Example-|-Next Example-|-Return to Chapter Listing