For loop
Use a For loop to run the same block
of code a specified number of times
<html>
<body>
<script type="text/javascript">
for (i = 0; i <= 5; i++)
{
document.write("The number is " + i)
document.write("<br>")
}
</script>
<p>Explanation:</p>
<p>The for loop sets <b>i</b> equal to 0.</p>
<p>As long as <b>i</b> is less than , or equal
to, 5, the loop will continue to run.</p>
<p><b>i</b> will increase by 1 each time the
loop runs.</p>
</body>
</html>
The for loop sets i equal to 0. As long as i is
less than , or equal to, 5, the loop will continue to run. i will
increase by 1 each time the loop runs.
Looping through HTML headers
Use the For loop to write the HTML headers.
<html>
<body>
<script type="text/javascript">
for (i = 1; i <= 6; i++)
{
document.write("<h" + i + ">This is header " +
i)
document.write("</h" + i + ">")
}
</script>
</body>
</html>
While loop
Use a While loop to run the same block
of code while or until a condition is true.
<html>
<body>
<script type="text/javascript">
i = 0
while (i <= 5)
{
document.write("The number is " + i)
document.write("<br>")
i++
}
</script>
<p>Explanation:</p>
<p><b>i</b> equal to 0.</p>
<p>While <b>i</b> is less than , or equal
to, 5, the loop will continue to run.</p>
<p><b>i</b> will increase by 1 each time the
loop runs.</p>
</body>
</html>
i equal to 0. While i is less than , or equal
to, 5, the loop will continue to run. i will increase
by 1 each time the loop runs.
Do while loop
Use a Do While loop to run the
same block of code while or until a condition is true. This
loop will always be executed once, even if the condition is
false, because the statements are executed before the condition
is tested
<html>
<body>
<script type="text/javascript">
i = 0
do
{
document.write("The number is " + i)
document.write("<br>")
i++
}
while (i <= 5)
</script>
<p>Explanation:</p>
<p><b>i</b> equal to 0.</p>
<p>The loop will run</p>
<p><b>i</b> will increase by 1 each time the
loop runs.</p>
<p>While <b>i</b> is less than , or equal
to, 5, the loop will continue to run.</p>
</body>
</html>
i equal to 0. The loop will run. i will increase
by 1 each time the loop runs. While i is less than , or
equal to, 5, the loop will continue to run.