130 likes | 414 Views
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.
E N D
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.
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
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>
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
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
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
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…
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.
<html> <body> <script type="text/javascript"> i=0; while (i<=5) { document.write("The number is " + i); document.write("<br />"); i++; } </script> </body> </html>
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
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>
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