Presentation is loading. Please wait.

Presentation is loading. Please wait.

L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need.

Similar presentations


Presentation on theme: "L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need."— Presentation transcript:

1

2 L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need to write loop statements to reduce the number of lines. JavaScript supports all the necessary loops to help you on all steps of programming.

3 Kinds Of Loops The for loop is used when you know in advance how many times the script should perform. For example if you wanted it to create exactly 50 lines. The while loop is used when you want the loop to continue until a certain condition becomes true. For example, if you wanted to make a table comparing Celsius and Fahrenheit, stepping 15 degrees for each row, and you wanted the table to contain values up to 1200 degrees of Celsius.

4 FOR LOOPS SYNTAX: for (variable= startvalue; variable<=endvalue; variable = variable + incrementfactor) { // Here goes the script lines you want to loop. } The for loop is the most compact form of looping.

5 For Loop For Loop includes the following three important parts: 1.The loop initialization where we initialize our counter to a starting value. The initialization statement is executed before the loop begins. 2.The test statement which will test if the given condition is true or not. If condition is true then code given inside the loop will be executed otherwise loop will come out.

6 For Loop 3.The iteration statement where you can increase or decrease your counter. You can put all the three parts in a single line separated by a semicolon.

7 Code example var count; document.write("Starting Loop" +" "); for(count = 0; count < 10; count++) { document.write("Current Count : " + count ); document.write(" "); } document.write("Loop stopped!");

8 The while Loop The most basic loop in JavaScript is the while loop. Syntax: while (expression) { Statement(s) to be executed if expression is true } The purpose of a while loop is to execute a statement or code block repeatedly as long as expression is true. Once expression becomes false, the loop will be exited.

9 Example: Following example illustrates a basic while loop: var count = 0; document.write("Starting Loop" + " "); while (count < 10) { document.write("Current Count : " + count + " "); count++; } document.write("Loop stopped!");

10 The do...while Loop The do...while loop is similar to the while loop except that the condition check happens at the end of the loop. This means that the loop will always be executed at least once, even if the condition is false.

11 Syntax: do-while loop Do { Statement(s) to be executed; } while (expression); Note the semicolon used at the end of the do...while loop.

12 Example: Let us write above example in terms of do...while loop. var count = 0; document.write("Starting Loop" + " "); do{ document.write("Current Count : " + count + " "); count++; } while (count < 0); document.write("Loop stopped!");

13 For...In Loop There is one more loop supported by JavaScript. It is called for...in loop. This loop is used to loop through an object's properties. Because we have not discussed Objects yet, so you may not feel comfortable with this loop. But once you will have understanding on JavaScript objects then you will find this loop very useful.

14 Syntax: For...In Loop for (variablename in object) { statement or block to execute } In each iteration one property from object is assigned to variablename and this loop continues till all the properties of the object are exhausted.

15 Example: Here is the following example that prints out the properties of a Web browser's Navigator object: var aProperty; document.write(" Navigator Object Properties "); for (aProperty in navigator) { document.write(aProperty); document.write(" "); } document.write("Exiting from the loop!");

16 The break Statement The break statement, which was briefly introduced with the switch statement, is used to exit a loop early, breaking out of the enclosing curly braces.

17 Example: var x = 1; document.write("Entering the loop "); while (x < 20) { if (x == 5) { break; // breaks out of loop completely } x = x + 1; document.write( x + " "); } document.write("Exiting the loop! ");

18 The continue Statement The continue statement tells the interpreter to immediately start the next iteration of the loop and skip remaining code block. When a continue statement is encountered, program flow will move to the loop check expression immediately and if condition remain true then it start next iteration otherwise control comes out of the loop.


Download ppt "L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need."

Similar presentations


Ads by Google