HEELPBOOK - Javascript - Loops ###################### JavaScript performs several types of repetitive operations, called "looping". Loops are set of instructions used to repeat the same block of code till a specified condition returns false or true depending on how you need it. To control the loops you can use counter variable that increments or decrements with each repetition of the loop. JavaScript supports two loop statements: for and while. - The For statements are best used when you want to perform a loop a specific number of times. - The While statements are best used to perform a loop an undetermined number of times. In addition, you can use the break and continue statements within loop statements. ########### The For Loop ###################### The For loop is executed till a specified condition returns false. It has basically the same syntax then in other languages. It takes 3 arguments and looks as follows: for (initialization; condition; increment) { // statements } When the For loop executes, the following occurs: - The initializing expression is executed. This expression usually initializes one or more loop counters, but the syntax allows an expression of any degree of complexity. - The condition expression is evaluated. If the value of condition is true, the loop statements execute. If the value of condition is false, the for loop terminates. - The update expression increment executes. - The statements execute, and control returns to step 2. - The following example generates a multiplication table 2 through 9. Outer loop is responsible for generating a list of dividends, and inner loop will be responsible for generating lists of dividers for each individual number: document.write("
" + i + " | "); for ( var j = 2; j <= 9; j++ ) { // inner loop document.write("" + i * j + " | "); } document.write(""); } document.write("