JavaScript DHTML/Language Basics/While
Содержание
- 1 A While Loop That Decrements from 10 to 1
- 2 <SCRIPT> var counter = 10; while (counter > 0) { document.write (counter + ""); counter--; } </SCRIPT>
- 3 <SCRIPT> var newString = ""; var theString = "string"; var counter = theString.length; do { newString += theString.substring(counter-1, counter); counter--; } while (counter > 0 ); document.write(theString + " reversed is " + newString + "!"); </SCRIPT>
A While Loop That Decrements from 10 to 1
<source lang="html4strict">
<HTML> <BODY>
<SCRIPT>
var counter = 10;
while (counter > 0)
{
document.write (counter + "
");
counter--;
}
</SCRIPT>
</BODY> </HTML>
</source>
Check the loop counter for while loop
<source lang="html4strict">
<html> <head> <title></title> </head> <body> <script type="text/javascript"> var i = 1; while (i < 101) {
if (i == 99) { alert("The number is " + i); } i++;
} </script> </body> </html>
</source>
Do while loop
<source lang="html4strict">
<html> <body> <script type="text/javascript"> i = 0 do{
document.write("The number is " + i) document.write("
") i++
}while (i <= 5) </script> </body> </html>
</source>
Do ... while loop and output calculation result
<source lang="html4strict">
<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>
</source>
Loop in while
<source lang="html4strict">
<HTML> <HEAD> <SCRIPT language="JavaScript">
</SCRIPT> </HEAD> <BODY> </BODY> </HTML>
</source>
The do..while Statement Ensures at Least One Iteration
<source lang="html4strict">
<html> <head>
<title>The do..while Statement Ensures at Least One Iteration</title>
</head> <body> <script language="JavaScript1.2" type="text/javascript">
</script>
</body> </html>
</source>
The while Statement
<source lang="html4strict">
<HTML> <HEAD> <TITLE>Using the While Statement</TITLE> </HEAD> <BODY> <SCRIPT></SCRIPT> </BODY> </HTML>
</source>
Using a Do/While Loop to Reverse a Text String
<source lang="html4strict">
<HTML> <BODY>
<SCRIPT> var newString = ""; var theString = "string"; var counter = theString.length; do { newString += theString.substring(counter-1, counter); counter--; } while (counter > 0 ); document.write(theString + " reversed is " + newString + "!"); </SCRIPT>
</BODY> </HTML>
</source>
Using the while Loop in JavaScript
<source lang="html4strict">
<html> <head>
<title>Using the while Loop in JavaScript</title>
</head> <body>
<script type="text/javascript"> </script>
</body> </html>
</source>
While loop
<source lang="html4strict">
<html> <body> <script type="text/javascript"> i = 0 while (i <= 5){
document.write("The number is " + i) document.write("
") i++
} </script> </body> </html>
</source>
While loop test
<source lang="html4strict">
<html> <script language="JavaScript">
</script> </html>
</source>