The 'for' loop
EasyProgramming.net tutorial on the for loop
by Nazmus Nasir
HTML
<!-- Easy JavaScript #23 - the for loop-->
<p>
Welcome to the 23rd Easy JavaScript tutorial, part of <a href="http://www.easyprogramming.net">EasyProgramming.net</a>. This tutorial covers the <code>for</code> loop!
</p>
<p>
Loops in general, execute the same code over and over again, often with a different value The <code>for</code> loop allows you to specify a starting point and an ending point to the loop. For example, count from 0 to 9.
</p>
<h2>
Syntax of the <code>for</code> loop:
</h2>
<pre>
for (starting statement; ending condition/statement; increment/decrement value) {
code block to be executed
}
</pre>
<h3>Output:</h3>
<p>
<span id="output"></span>
</p>
<p>
W3Schools: <a href="http://www.w3schools.com/js/js_loop_for.asp">http://www.w3schools.com/js/js_loop_for.asp</a>
</p>
JavaScript
//set the value of 'i' to 0; loop while 'i' is less than 10; increment 'i' by 1 every time it loops
for(var i = 10; i > 0; i--){
console.log("We are at number " + i);
}
for(var i = 0; i < 50; i=i+5){
document.getElementById("output").innerHTML += i + "<br />";
}