JavaScript tutorial:
do...while statement

 

The do...while statement is use to execute a statement block once, and then repeats execution of the loop until a condition expression evaluates to false.

Syntax

do
   statement
while (expression) ;

The do...while statement syntax has these parts:

Part

Description

statement

The statement to be executed if expression is true. Can be a compound statement.

expression

An expression that can be coerced to Boolean true or false. If expression is true, the loop is executed again. If expression is false, the loop is terminated.

Example

The value of expression is not checked until after the first iteration of the loop, guaranteeing that the the loop is executed at least once. Thereafter, it is checked after each succeeding iteration of the loop.

The following code uses the do...while statement to iterate the Drives collection:

<html>
<head>
<script language = "JavaScript">
function GetDriveList()
{
    var fso, s, n, e, x;
    fso = new ActiveXObject("Scripting.FileSystemObject");
    e = new Enumerator(fso.Drives);
    s = "";
    do
    {
        x = e.item();
        s = s + x.DriveLetter;
        s += " - ";
        if (x.DriveType == 3)
            n = x.ShareName;
        else if (x.IsReady)
            n = x.VolumeName;
        else
            n = "[Drive not ready]";
        s += n + "<br&gt";
        e.moveNext();
    }
    while (!e.atEnd());
    return(s);
}
</script>
</head>
<body>
    <script language = "JavaScript">
        document.write(GetDriveList());
    </script>
</body>
</html>

To run the code above, paste it into JavaScript Editor, save as hmt and to to View, Internal Page Viewer or press F5.

See also: break Statement, continue Statement, for Statement, for...in Statement, while Statement, Labeled Statement