We've started
to get familiar with some of JavaScripts control statements. We continue this
class by introducing looping.
Very often when you
write code, you want the same block of code to run a number of times. You can
use looping statements in your code to do this.
In JavaScript we have the following looping statements:
- while - loops through a block of code while a condition
is true
- do...while - loops through a block of code at least
once, and then repeats the loop while a condition is true
- for - run statements a specified number of times
Counter-controlled
repetition requires:
- The name of a control variable (also called the loop counter).
- The initial value of the control variable.
- The increment (or decrement) by which the control variable
is modified each time through the loop (also know as each iteration of the
loop).
- The condition that tests for the final value of the control variable
to determine whether looping should continue.
While
Table of Contents