JavaScript Tutorial/Statement/Do While

Материал из Web эксперт
Версия от 08:25, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

do...while

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);


Do While loop

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


Use do-while loop and prompt dialog to read user input

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
do
{
    sometext = prompt("Enter some text","");
}
while (sometext == "");
alert(sometext);
//  -->
</script>
</head>
<body>
</body>
</html>


Use integer variable to control a do-while loop

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


Use or (||) in do-while condition

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
do
{
    sometext = prompt("Enter some text","Type some text");
}
while (sometext == "Type some text" || sometext == "" || sometext == null);
alert(sometext);
//  -->
</script>
</head>
<body>
</body>
</html>