JavaScript DHTML/Language Basics/While
Версия от 09:59, 26 мая 2010; (обсуждение)
Содержание
- 1 A While Loop That Decrements from 10 to 1
- 2 Check the loop counter for while loop
- 3 Do while loop
- 4 Do ... while loop and output calculation result
- 5 Loop in while
- 6 The do..while Statement Ensures at Least One Iteration
- 7 The while Statement
- 8 Using a Do/While Loop to Reverse a Text String
- 9 Using the while Loop in JavaScript
- 10 While loop
- 11 While loop test
A While Loop That Decrements from 10 to 1
<HTML>
<BODY>
<H1>
<SCRIPT>
var counter = 10;
while (counter > 0)
{
document.write (counter + "<br>");
counter--;
}
</SCRIPT>
</H1>
</BODY>
</HTML>
Check the loop counter for while loop
<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>
Do while loop
<html>
<body>
<script type="text/javascript">
i = 0
do{
document.write("The number is " + i)
document.write("<br>")
i++
}while (i <= 5)
</script>
</body>
</html>
Do ... while loop and output calculation result
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
var x = 1
do {
++x;
document.write(x+"<BR>");
}while (x < 10);
</script>
</head>
<body>
</body>
</html>
Loop in while
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
<!--
count=1;
while(count<16)
{
document.write(count+". Repetitive.<BR>");
count+=1;
}
//-->
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
The do..while Statement Ensures at Least One Iteration
<html>
<head>
<title>The do..while Statement Ensures at Least One Iteration</title>
</head>
<body>
<script language="JavaScript1.2" type="text/javascript">
<!--
var userEntry = 0;
var x = 1;
do{
document.writeln(x);
x++;
}while(x <= userEntry);
// -->
</script>
</body>
</html>
The while Statement
<HTML>
<HEAD>
<TITLE>Using the While Statement</TITLE>
</HEAD>
<BODY>
<SCRIPT><!--
i=1
while(i<7){
document.write("<H"+i+">This is a level "+i+" heading." +"</H"+i+">")
++i
}
// --></SCRIPT>
</BODY>
</HTML>
Using a Do/While Loop to Reverse a Text String
<HTML>
<BODY>
<H1>
<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>
</H1>
</BODY>
</HTML>
Using the while Loop in JavaScript
<html>
<head>
<title>Using the while Loop in JavaScript</title>
</head>
<body>
<script type="text/javascript">
<!--
var i = 0;
var result = 0;
var status = true;
document.write("0");
while(status){
result += ++i;
document.write(" + " + i);
if(i == 10){
status = false;
}
}
document.writeln(" = " + result);
// -->
</script>
</body>
</html>
While loop
<html>
<body>
<script type="text/javascript">
i = 0
while (i <= 5){
document.write("The number is " + i)
document.write("<br>")
i++
}
</script>
</body>
</html>
While loop test
<html>
<script language="JavaScript">
<!--
function loopTest() {
var index = 1;
while (index <= 12) {
if (index == 6)
break;
index++;
}
document.write("The break statement brings us here.");
}
loopTest();
//-->
</script>
</html>