Presentation is loading. Please wait.

Presentation is loading. Please wait.

Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.

Similar presentations


Presentation on theme: "Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial."— Presentation transcript:

1 Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial Value is the starting number of the loop. If the condition(test) is true the code is executed. The initial value is changed by the step size. 1 For Loops

2 The expression is evaluated, if it is false, JavaScript moves to the next statement in the program If it is true then the statement is executed and expression is evaluated again Again if the expression is false the JavaScript moves on the next statement in script, otherwise it executes the statement that forms the body of the loop 2 For Loops

3 For for (i=0;i<= 5; i++) //the position of the ++ here does not have an impact on the output it will always start at 0 { document.write("The number is " + i);// change to i++ and ++i here to see the impact on the output document.write(" "); } 3 For Loops

4 Another Example For for (i=0;i<= 5; i++) //the position of the ++ here does not have an impact on the output it will always start at 00 { document.write("The number is " + i++ ); document.write(" "); } The number is 0 The number is 2 The number is 4 4

5 Another Example For for (i=0;i<= 5; i++) //the position of the ++ here does not have an impact on the output it will always start at 00 { document.write("The number is " + ++i);// document.write(" "); } The number is 1 The number is 3 The number is 5 5

6 While a condition is true execute one or more statements. “While Loops” are especially useful when you do not know how many times you have to loop but you know you should stop when you meet the condition, i.e. an indeterminate loop 6 While Loops:

7 The while Loop is the basic statement that allows JavaScript to perform repetitive actions. It has the following syntax. while (expression) { // Statement to be executed } … more code… 7 While Loop

8 This cycle continues until the expression evaluates to false It is important that a Loop counter that is used within the body of the Loop is declared outside it. 8 While Loops

9 i=0; while (i<=5) { document.write("The number is " + i); document.write(" "); i++; } 9

10 While Loops i=1; while (i<=5) { document.write("The number is " + i); document.write(" "); i++; } What happens in this example? i=6; while (i<=5) { document.write("The number is " + i); document.write(" "); i++; } Loop DOES not run 10

11 Do While Loops var i=0; do { document.write("Output= " + i); document.write(" "); i++; } while (i<=12); Will run once to evaluate condition – var i=13; do { document.write("Output= " + i); document.write(" "); i++; } while (i<=12); 11

12 Why use Loops? When you want the same block of code to run over and over again in a row Instead of adding several almost equal lines in a script we can use loops to perform a task like this While v For A while loop doesn't initialize or increment any fields automatically as part of the command, it just tests a condition and executes the loop for as long as the condition remains true A while loop can easily be substituted wherever you have a for loop by moving the initialization statement (e.g. i=1) in front of the loop and the increment statement (e.g. i++) to just inside the end of the loop This may make the loop easier to read 12


Download ppt "Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial."

Similar presentations


Ads by Google