JavaScript Tutorial/Statement/Do While
Содержание
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>