HTML and CSS Reference
In-Depth Information
That's all you need to know about branching, so on to looping. Looping
lets you repeat an operation multiple times. The most common loop is
for .
Loop while this
expression is true.
Increment the
variable after
every loop.
Starting state
for (var i = 0; i < 10; i++) {
console.log('Loop ' + i);
}
statement block to
repeat
This screenshot shows the output
in the console from the for loop. It
logs 10 lines, counting up from 0
to 9. This is the normal program-
mer way of counting 10 things,
starting at 0, so get used to it!
It's also normal to use the loop
index variable, i in this case, to
modify the code's behavior on each
iteration through the loop.
An alternative to the for loop is the while loop. As you can see from the
following diagram, it has all the same features as the for loop, but the
arrangement is slightly different.
Starting state
Loop while this
expression is true.
var i = 0;
while (i < 10) {
console.log('Loop ' + i);
i += 2;
}
statement block
to repeat
Increment the
variable after
every loop.
Search WWH ::




Custom Search