JavaScript Tutorial/Statement/Do While — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 08:25, 26 мая 2010

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>