2b - Javascript:Control Statements II - While statements
The while statement will execute a block of code while a condition is true..
while (condition)
{
code to be executed
}
Let's experiment
Now let's make our own page in Notepad and type in the following:
<html>
<head>
<title>
While statement test
</title>
</head>
<body>
<script type="text/javascript">
<!--
var counter = 1; //initialization
of the loop counter
while (counter <= 7){ //repetition condition
document.writeln("<p style=\"font-size: "
+ counter + "ex\"> XHTML font size " + counter + "ex</p>");
// notice how you escape quotes in the html that gets
written via document.writeln.
//also, notice how var counter gets used bothk to count the
iterations,
//and it gets written into the html.
++counter;
}
//-->
</script>
</body>
</html>