For Loop Overview

[Total: 0    Average: 0/5]

This file shows how to use a for loop. The head portion of a for loop contains three parts, each of which could be null. The body portion of a foor loop contains 1 statement.

Body Code:

<script type=text/javascript>
document.write('<BR><div class=desc>"i" is a variable that exists in the scope of the for loop only.  (we'll see that this isn't entirely true below)</div>')
document.write('<div class=code>for (var i = 0; i < 10; i++) null;</div>')
document.write('<br><div class=desc>This creates an endless loop.</div>')
document.write('<div class=code>for (;;) null;</div>')
document.write('<br><div class=desc>"i", "a", and "q" all exist outside the scope of the for loop</div>')
document.write('<div class=code>for (i = 20; a < 3; q="what") null;</div>')
document.write('<br><hr><br>')
document.write('<br><div class=desc>Note: we set i=34 before this example (we will look at its value after the example.</div>')
var i=34
document.write('<div class=code>var i = 34</div>')
document.write('<br><div class=desc>This is an example of how to use a for loop.<br>Note how there are compound statements inside the for loop header.</div>')
document.write('<div class=code>for (var i = j = k = fib = 0; i < 5; i++, fib = j+k, j = k, k = fib)')
document.write('<br>&nbsp;&nbsp;document.write("Fib("+i+")="+fib</div>')
document.write('<BR><div class=desc>This results in the following output:</div></br>')
for (i=0,j=1,k=0,fib=0;i<8;i++,fib=j+k,j=k,k=fib)
 document.write("<div class=result>&nbsp;&nbsp;Fib(i="+i+")="+fib+"</div>")
document.write('<BR><BR><div class=desc>Now to see what i is... </div>')
document.write('<BR><span class=desc>i is </span><span class=result>'+i+'</span><BR>')
document.write('<br><div class=desc>Be careful... things aren't always what they seem...</div>')
</script>

Times Viewed: 0

Leave a Reply

Your email address will not be published. Required fields are marked *