do…while loop always executes the loop once before evaluating the expression for the first time.
This difference is seen in the following format:
do
    {
       statement;
    }
    while (expression);
Once the loop has executed for the first time, the expression is evaluated.
If true, the loop is executed again.
When the expression evaluates to false, the next line of code following the while structure is executed.
A semicolon (;) must be placed after the rightmost parenthesis.
Example 1:
<html>
<body>
<SCRIPT LANGUAGE='JavaScript'>
<!--
var counter = 1;
do
{
document.write(counter + "<BR>");
counter++;
}
while (counter < 5);
//-->
</SCRIPT>
</body>
</html>

Example 2:

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
var x = 1
do
{
++x;
document.write(x+" ");
}
while (x < 10);

//  -->
</script>
</head>
<body>

</body>
</html>

0 comments:

Post a Comment

 
Top
Blogger Template