HTML and CSS Reference
In-Depth Information
FORMAT
while (condition) {
statements;
increment/decrement counter;
}
EXAMPLE
6.5
<html>
<head>
<title>Looping Constructs</title>
</head>
<body>
<h2>While Loop</h2>
<font size="+2">
1
<script type="text/javascript">
2
var i=0;
//
Initialize loop counter
3
while ( i < 10 ){
//
Test
4
document.writeln(i);
5
i++;
//
Increment the counter
6
}
//
End of loop block
7
</script>
</font>
</body>
</html>
EXPLANATION
1
The JavaScript program starts here.
2
The variable
i
is initialized to
0
.
3
The expression after the
while
is tested. If
i
is less than
10
, the block in curly brac-
es is entered and its statements are executed. If the expression evaluates to false,
(i.e.,
i
is not less than
10
), the loop block exits and control goes to line 6.
4
The value of
i
is displayed in the browser window (see Figure 6.4).
5
The value of
i
is incremented by 1. If this value never changes, the loop will never
end.
6
This curly brace marks the end of the
while
loop's block of statements.
7
The JavaScript program ends here.