1 / 12

For Loops

For Loops. 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.

kioko
Download Presentation

For Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. For Loops • 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.

  2. For Loops • 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

  3. For Loops <html> <head> <title> For</title> </head> <body> <script type="text/javascript"> 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("<br />"); } </script> </body> </html>

  4. Another Example <html> <head> <title> For</title> </head> <body> <script type="text/javascript"> 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("<br />"); } </script> </body> </html> The number is 0The number is 2The number is 4

  5. Another Example <html> <head> <title> For</title> </head> <body> <script type="text/javascript"> 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("<br />"); } </script> </body> </html> The number is 1The number is 3The number is 5

  6. While Loops: • 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

  7. While Loop • 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…

  8. While Loops • 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.

  9. <html> <body> <script type="text/javascript"> i=0; while (i<=5) { document.write("The number is " + i); document.write("<br />"); i++; } </script> </body> </html>

  10. While Loops <html> <body> <script type="text/javascript"> i=1; while (i<=5) { document.write("The number is " + i); document.write("<br />"); i++; } </script> </body> </html> What happens in this example? <html> <body> <script type="text/javascript"> i=6; while (i<=5) { document.write("The number is " + i); document.write("<br />"); i++; } </script> </body> </html> Loop DOES not run

  11. Do While Loops <html> <body> <script type="text/javascript"> vari=0; do   { document.write("Output= " + i); document.write("<br />"); i++;   } while (i<=12); </script> </body> </html> Will run once to evaluate condition – <html> <body> <script type="text/javascript"> vari=13; do   { document.write("Output= " + i); document.write("<br />"); i++;   } while (i<=12); </script> </body> </html>

  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

More Related