Previous Example-|-Next Chapter's Examples-|-Return to Chapter Listing

Example 6.19:
Returning to the Top of The Loop—continue


The button calls function1(), which uses a 'while' loop. This displays the value held by the counter variable ( i ) in a confirm box. If you press the Cancel button, a continue statement is executed and processing goes back to the top of the loop rather than incrementing the counter variable i:


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