| Looping | |||
| break and continue | |||
These statements are used with in a loop. Break as the name suggests is used to break or terminate the loop in between & continue is to continue the loop breaking it for current loop value. |
|||
| break: | |||
| This statement breaks the loop & shifts the control out of the loop. | |||
| continue: | |||
| This statement breaks the loop for current value and resumes the loop with new value. | |||
| Example for break: | |||
| <html> <head> </head> <body> <script type="text/javascript"> var count; for(count=0;count<100;count++) { if (count>10) { break } document.write(" "+count) } </script> </body> </html> |
|||
| Understanding program: | |||
| The loop gets terminated because we have kept a condition that if the count is more than 10 than break, break statement will end the loop at that point. | |||
| Output is: | |||
| 0 1 2 3 4 5 6 7 8 9 10 | |||
| Click here to view result of this program on browser | |||
| Example for continue: | |||
| <html> <head> </head> <body> <script type="text/javascript"> var count; for(count=0;count<20;count++) { if (count==10) { continue } document.write(" "+count) } </script> </body> </html> |
|||
| Understanding program: | |||
| As soon as the value of count becomes equals to 10 , the loop gets terminated for this value and the loop resumes with new value. | |||
| Output is: | |||
| 0 1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 | |||
| Click here to view result of this program on browser | |||
|
|||