The dowhile Loop


The do while Loop

The 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:

  • statements . The body of the loop, a set of statements that are executed at least once, and executed again each time the condition evaluates to true.

  • condition . Evaluated after each pass through the loop. If condition evaluates to true, the statements in the loop's body are executed again. When condition evaluates to false, the loop terminates.

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

Statement

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

do while

   

x

x

   

x

x

x

x

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> 


Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net