The do while LoopThe do while loop executes its statements until a test condition evaluates to false. Unlike a while loop, the statements in the body of the do while loop are executed at least once. Here's the syntax of this loop: do statements while (condition) The following parts comprise this loop:
The do while loop was new in version 4.0 of the Netscape Navigator and Internet Explorer, as you see in Table 3.4. Table 3.4. The do while Loop
Here's how we can search for "Claire" in an array, just as we did in the preceding example with a while loopbut this time, we're using a do while loop: (Listing 03-05.html on the web site)<HTML> <HEAD> <TITLE> Using the do...while Loop </TITLE> </HEAD> <BODY> <H1>Using the do...while Loop</H1> <SCRIPT LANGUAGE="JavaScript"> <!-- var index = 0, data = new Array(5) data[0] = "Fank" data[1] = "Tom" data[2] = "Claire" data[3] = "Sara" data[4] = "Jane" do{ index++ }while(data[index] != "Claire") alert("Found Claire at index " + index) // --> </SCRIPT> </BODY> </HTML> |