| Looping | |||
| for | |||
This loop contains the initial value, termination value , increment/decrement value in a single line, which in on other looping are used in separate line, the scope of a for loop is inside the curly brackets used with it. The initialization segment is executed only once at the start of the loop and termination and increment/decrement segments are executed every time till the condition remains true. |
|||
| syntax: | |||
| for(initialization; termination; increment/decrement) { code to be executed } |
|||
| Example | |||
<html> <head> </head> <body> <script type="text/javascript"> var a; for(a=100;a>0;a-=5) { document.write(" "+a) } </script> </body> </html> |
|||
| Understanding program : | |||
| Value of a is initially 100 , the condition with for loop is while a is greater 0 the statements typed should be executed. a-=5 means a=a-5, so the a will get decremented with 5 every time. As soon as a becomes 0 the loop gets terminated. | |||
| Output: | |||
| 100 95 90 85 80 75 70 65 60 55 50 45 40 35 30 25 20 15 10 5 | |||
| Click here to view result of this program on browser | |||
|
|||